Advanced Octave A05 — Bridge errors and controlled recovery
Advanced GNU Octave with GX-TXT · Module 01
A05 — Errori del Bridge e recupero controllato
An non-existent API, an unregistered operation and an error of the provider require different answers. Octave allows to intercept an error, but the choice to continue or interrupt the analysis depends on its meaning.
Objectives and prerequisites
After the introductory course and the previous lessons, we can control dependencies. Now we examine the identification and message of a real error, intentionally provoked without changing data. The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0.
A deliberately non-existent operation
Import errori_bridge.m, save it in the Session and run it with the Runner.
% Advanced Octave course A05: handle API discovery and call errors.
% SPDX-License-Identifier: GPL-3.0-or-later
api = 'CANONICAL_DATASET_API_v2';
operation = 'operazione_inesistente';
fprintf('Known API: %d\n', gxtxt_api_has(api));
fprintf('Requested operation available: %d\n', gxtxt_api_has_operation(api, operation));
try
gxtxt_api_call(api, operation, {});
error('Unexpected success in the demonstration.');
catch err
fprintf('Caught error identifier: %s\n', err.identifier);
fprintf('Caught error message: %s\n', err.message);
end_try_catch
fprintf('Demonstration completed; no data were changed.\n');
The example first controls the existence of the API and then that of the invented operation. The next call is intentionally wrong to show what the Bridge returns: catch err receives the error and prints and messages.
In the test the API is present (1 ), the operation is not (0 ) and the call signals GXTXT:ScriptBridge:UnknownOperation. The Job completes the demonstration because the expected error has been intercepted; this does not mean that the call has succeeded.
Manage errors without hiding them
In a real pipeline it is preferable to check the requirements before the call and stop if a necessary operation is missing. The Bridge also distinguishes other documented cases, such as unknown APIs, invalid arguments, transport error and provider failure. It preserves identification and message, and reports the context necessary to understand which operation has failed.
This small catch prints any error, including the one raised by the branch “unexpected success”: it is suitable for an experiment showing diagnostics, but it is not a selective manager for an analysis. In the production code verify the expected identifier and relaunch unexpected errors with rethrow(err). Do not replace an error with an empty or zero array: it would be impossible to distinguish a fault from a valid result.
Exercise
- Why the Job shown can be completed even if the call raises an error?
- How would you distinguish an operation not recorded by an empty numerical result?
- What should a pipeline do if an indispensable operation is not available?
Worked solution
- Because the
catchlock intercepts the error and the demonstration ends after printing it. - In the first case the Bridge produces an error with an identification; in the second one it is necessary to interpret a result actually returned according to the contract.
- Interrupt and indicate what dependence is missing, without presenting a partial analysis as a whole.
Other steps of this project
- Advanced Octave A75 — Dynamical systems in phase space
- Advanced Octave A81 — Geography of the waves and a 3D surface
- Advanced Octave A82 — Age, sex and demographic inequalities
- Advanced Octave A83 — All-cause mortality, certified COVID deaths and surveillance
- Advanced Octave A84 — A reproducible public account
- Advanced Octave A73 — Optimization landscapes and 3D trajectories