Handling errors and warnings
This chapter describes how to handle errors raised by mandatory scripts. When an error occurs on a target (example : model not supported by the engine), the json report cannot be generated to return the results to the user.
To give explanation to the user, there are two log files (user.log and error.log) to write some information. The content log files will be displayed to the user on the website interface.
user.log
: This log file can be used to write some information or warnings to the user.error.log
: This log file must contain which errors occurred when the json report cannot be generated.
Specification
- Only write errors to error.log only if they are “functional” error. Such as an unsupported format of model given for the benchmark. Don’t write other errors to avoid information leaks, such as the IP target.
- If the json report can be generated, the error.log file must be empty.
- Write warnings to user.log or user information to user.log
exit_functions.sh
script exists to help developers handeling errors and wawnings. You can download the full exit_functions.sh
script here to understand the implementation and use it in your local tests.Usage
Inside mandatory scripts or others scripts executed by the dAIEdge-VLab CI/CD, it’s possible to source exit_functions.sh
. This script is hosted by the dAIEdge-VLab, and it implements some utile functions.
exit_functions.sh
at the beginning of your script to handle most of the errors automatically. Make sure that the warnings generated by the different command you use are not written on stderr. If they are, you can disable them or redirect them to the user.log
file.Simple use case
#!/bin/bash
#########################
# Script used to benchmark a model on the target.
#########################
# Handle exit and error with exported function in exit_functions.sh
# By default the script exit on error and write all stderr messages into error.log
source $FUNCTIONS_PATH/exit_functions.sh
...
Local tests
To test your script locally, you can run the following command to set the variable $FUNCTIONS_PATH
to the local copy of the exit_functions.sh
file. You should run this command before executing your script.
export FUNCTIONS_PATH=/path/to/local/exit_functions.sh
General behavior
When exit_functions.sh
is sourced, automatically the function init
is executed. This function configures the parent script as follows :
- Exit by default on error, because
set -e
is sett byexit_functions.sh
. - The files
error.log
anduser.log
are created. There location is :$LOG_PATH/error.log
$LOG_PATH/user.log
- Any print on stderr is redirected to the file
error.log
. - Any errors raised is caught and the function
on_error_print
is called. The function prints in red in the console (stdout) some information about the error, like the line number, the error code, the script name and the command. Example :
sshpass -p $BOARD_PASSWORD scp -o stricthostkeychecking=no $MODEL_FILENAME $BOARD_USER@$BOARD_IP:/home/$BOARD_USER/
- On exit, a function
on_exit
is called. It used to deinit the scriptexit_functions.sh
and the print the exit code in red or green in the console (stdout).
Functions available
trap_signal_disable()
trap_signal_disable
can be called to deactivate the “callback” function on_error_print
and on_exit
.
info_print()
info_print
is called to print some text to stdout in blue.
Example
info_print "Move the model to AI_Support"
stdout output
user_log_print()
Can be called to write text into user.log.
Example
user_log_print "\e[33mWarning : First benchmarking failed due to insufficient memory. Try with reduced workspace size (--memPoolSize=workspace:3072).\e[0m"
user.log output
error_log_ignore_pattern_add()
This function registers some patterns ignored by the redirection of stderr into error.log.
Lines matching with pattern registered with error_log_ignore_pattern_add
are written into user.log in yellow. Also, an info is printed in the console (stdout) when a line is ignored and placed into user.log.
Example
error_log_ignore_pattern_add "Device memory is insufficient to use tactic\.$"
user.log output
info message in stdout
[08/26/2024-11:12:39] [w] [TRT] Tactic Device request: 4897MB Available: 3168MB. Device memory is insufficient to use tactic.
error_log_ignore_pattern_reset()
It’s possible to reset pattern registered with error_log_ignore_pattern_add
. All pattern registered are reset.
error_log_enable()
Called by the init
function and can be called after error_log_disable
. After execution, messages written into stderr are redirected into error.log if the line doesn’t match with pattern registered by error_log_ignore_pattern_add
.
error_log_disable()
This function can be called to disable the redirection of stderr into error.log. The messages printed on stderr stay on stderr (in the console).
error_log_print()
It’s possible to write manually into error.log with the function error_log_print
. Even the error log is disable, this function write into error.log.
error_log_ignore_pattern_add
, the function error_log_print
cannot bypass the rules and your log will be written into user.log. To solve it, use error_log_ignore_pattern_reset
and add again your pattern with error_log_ignore_pattern_add
after error_log_print
.Example
error_log_print "Critical error : An error occured during the benchmarking."
error.log output