conditionally error handling

Showcases that you can use internal var up_runtime_shell_exec_result to conditionally do your own customised cleanup or business

Demo

source

Main task yaml file
    tasks:
    - name: task
      task:
      - func: shell
        do:
        - echo "opening file"
        - echo "hello"|grep "world"
        flags:
        - ignoreError
        finally:
        - func: shell
          desc: |
            ensure the opened file is closed
          do:
          - echo "close the file ....."
          - |
            echo """
            exec command: {{.up_runtime_shell_exec_result.Cmd}}
            error code: {{.up_runtime_shell_exec_result.Code}}
            error message: {{.up_runtime_shell_exec_result.ErrMsg}}
            error output: {{.up_runtime_shell_exec_result.Output}}
            """
        - func: cmd
          desc: |
            see if the exec context result: up_runtime_shell_exec_result is still availabe
            conditional do something about the error etc
          do:
          - name: print
            cmd: |
              error message: {{.up_runtime_shell_exec_result.ErrMsg}}
          if: '{{ne .up_runtime_shell_exec_result.Code 0}}'
      - func: shell
        desc: |
          this step will not be reached if the ignoreError flag is not set
          try it yourself to remove the ignoreError flag and see difference
        do:
        - echo "extra step ..."
    
Main log file
    loading [Config]:  ./tests/functests/upconfig.yml
    Main config:
                 Version -> 1.0.0
                  RefDir -> ./tests/functests
                 WorkDir -> cwd
              AbsWorkDir -> /up_project/up
                TaskFile -> c0174
                 Verbose -> vvv
              ModuleName -> self
               ShellType -> /bin/sh
           MaxCallLayers -> 8
                 Timeout -> 3600000
     MaxModuelCallLayers -> 256
               EntryTask -> task
      ModRepoUsernameRef -> 
      ModRepoPasswordRef -> 
    work dir: /up_project/up
    -exec task: task
    loading [Task]:  ./tests/functests/c0174
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    Task1: [task ==> task:  ]
    -Step1:
    self: final context exec vars:
    
    (*core.Cache)({
      "up_runtime_task_layer_number": 0
    })
    
    cmd( 1):
    echo "opening file"
    
    -
    opening file
    
    -
     .. ok
    cmd( 2):
    echo "hello"|grep "world"
    
    -
    
    -
     .. failed(suppressed if it is not the last step)
    Step Finally:
    -Step1: [
    ensure the opened file is closed
    ]
    self: final context exec vars:
    
    (*core.Cache)({
      "up_runtime_task_layer_number": 0,
      "up_runtime_shell_exec_result": (*utils.ExecResult)({
        Cmd: "echo \"hello\"|grep \"world\"",
        Code: 1,
        Output: "",
        ErrMsg: "exit status 1"
      })
    })
    
    cmd( 1):
    echo "close the file ....."
    
    -
    close the file .....
    
    -
     .. ok
    cmd( 2):
    echo """
    exec command: {{.up_runtime_shell_exec_result.Cmd}}
    error code: {{.up_runtime_shell_exec_result.Code}}
    error message: {{.up_runtime_shell_exec_result.ErrMsg}}
    error output: {{.up_runtime_shell_exec_result.Output}}
    """
    
    
    -
    
    exec command: echo hello|grep world
    error code: 1
    error message: exit status 1
    error output: 
    
    
    -
     .. ok
    . ok
    -Step2: [
    see if the exec context result: up_runtime_shell_exec_result is still availabe
    conditional do something about the error etc
    ]
    self: final context exec vars:
    
    (*core.Cache)({
      "last_result": (*utils.ExecResult)({
        Cmd: "echo \"\"\"\nexec command: echo \"hello\"|grep \"world\"\nerror code: 1\nerror message: exit status 1\nerror output: \n\"\"\"\n",
        Code: 0,
        Output: "exec command: echo hello|grep world\nerror code: 1\nerror message: exit status 1\nerror output:",
        ErrMsg: ""
      }),
      "up_runtime_task_layer_number": 0,
      "up_runtime_shell_exec_result": (*utils.ExecResult)({
        Cmd: "echo \"hello\"|grep \"world\"",
        Code: 1,
        Output: "",
        ErrMsg: "exit status 1"
      })
    })
    
    ~SubStep1: [print:  ]
    error message: exit status 1
    
     WARN: [ignoreError:] - [Error ignored!!!]
    -Step2: [
    this step will not be reached if the ignoreError flag is not set
    try it yourself to remove the ignoreError flag and see difference
    ]
    self: final context exec vars:
    
    (*core.Cache)({
      "last_result": (*utils.ExecResult)({
        Cmd: "echo \"hello\"|grep \"world\"",
        Code: 1,
        Output: "",
        ErrMsg: "exit status 1"
      }),
      "up_runtime_task_layer_number": 0
    })
    
    cmd( 1):
    echo "extra step ..."
    
    -
    extra step ...
    
    -
     .. ok
    . ok
    
Logs with different verbose level
Raw logs with different verbose level

What if I want to handle a specific error

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.

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

Other references: