Handling errors and warnings

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

  1. 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.
  2. If the json report can be generated, the error.log file must be empty.
  3. Write warnings to user.log or user information to user.log
ℹ️
A 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.

👾
Tip: Simply include the file 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

manager.sh
#!/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 :

  1. Exit by default on error, because set -e is sett by exit_functions.sh.
  2. The files error.log and user.log are created. There location is :
    • $LOG_PATH/error.log
    • $LOG_PATH/user.log
  3. Any print on stderr is redirected to the file error.log.
  4. 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 :
manager.sh script error code 255 occured at line 15 :
sshpass -p $BOARD_PASSWORD scp -o stricthostkeychecking=no $MODEL_FILENAME $BOARD_USER@$BOARD_IP:/home/$BOARD_USER/
  1. On exit, a function on_exit is called. It used to deinit the script exit_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

ℹ️ deploy.sh Move model to AU_Support

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

Warning : First benchmarking failed due to insufficient memory. Try with reduced workspace size (--memPoolSize=workspace:3072).

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

[08/26/2024-11:12:39] [w] [TRT] Tactic Device request: 4897MB Available: 3168MB. Device memory is insufficient to use tactic.

info message in stdout

⚠️ manager.sh The following stdrr message is ignored and placed into user.log :
[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.

⚠️
Warning: If some patterns are added to be ignored with 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

Critical error : An error occured during the benchmarking.