github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/functests/c0174.yml (about)

     1  doc_meta: |
     2    folder: flow-controll
     3    title: conditionally error handling
     4    head: |
     5      Showcases that you can use internal var up_runtime_shell_exec_result to conditionally do your own customised cleanup or business
     6  
     7    sections:
     8      - title: Demo
     9        log: yes
    10  
    11      - title: What if I want to handle a specific error
    12        content: |
    13          Note that in the finally steps, it uses up_runtime_shell_exec_result to obtain the shell exec exit code, error message and regular output.
    14  
    15          In case you need to deal with a specific error, eg, by exit code, or error message, you can use if condition to implement that
    16  
    17    related:
    18      refs:
    19        - title: shell func
    20          link: ../../quick-start/c0002/
    21        - title: error handling
    22          link: ../../test-debug/error_handling/
    23        - title: finally and rescue
    24          link: ../../flow-controll/c0172/
    25  
    26  notes:
    27    - all finally block/task to access the failed exec contextual info and conditionally handle the error
    28  
    29  tasks:
    30  
    31    -
    32      name: task
    33      task:
    34  
    35        -
    36          func: shell
    37          do:
    38            - echo "opening file"
    39            - echo "hello"|grep "world"
    40          flags:
    41            - ignoreError
    42          finally:
    43            -
    44              func: shell
    45              desc: |
    46                ensure the opened file is closed
    47              do:
    48                - echo "close the file ....."
    49                - |
    50                  echo """
    51                  exec command: {{.up_runtime_shell_exec_result.Cmd}}
    52                  error code: {{.up_runtime_shell_exec_result.Code}}
    53                  error message: {{.up_runtime_shell_exec_result.ErrMsg}}
    54                  error output: {{.up_runtime_shell_exec_result.Output}}
    55                  """
    56  
    57            -
    58              func: cmd
    59              desc: |
    60                see if the exec context result: up_runtime_shell_exec_result is still availabe
    61                conditional do something about the error etc
    62              do:
    63                - name: print
    64                  cmd: |
    65                    error message: {{.up_runtime_shell_exec_result.ErrMsg}}
    66              if: '{{ne .up_runtime_shell_exec_result.Code 0}}'
    67  
    68        -
    69          func: shell
    70          desc: |
    71            this step will not be reached if the ignoreError flag is not set
    72            try it yourself to remove the ignoreError flag and see difference
    73          do:
    74            - echo "extra step ..."