Uploaded image for project: 'CDAP'
  1. CDAP
  2. CDAP-11699

Exception handlers should never rethrow the root cause of a caught exception

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Critical
    • Resolution: Unresolved
    • Affects Version/s: 4.2.0
    • Fix Version/s: 6.1.0
    • Component/s: App Fabric, Log
    • Labels:
    • Rank:
      1|i002kf:

      Description

      When handling an exception, we should always preserve the entire exception - except if we know for sure that the outermost exception is only used to wrap a checked exception.

      Even then it can be bad practice to call getRootCause(). That will often discard too much context.

      For example, the WorkflowDriver catches exceptions from executing workflow nodes:

            try {
              blockIfSuspended();
              WorkflowNode node = iterator.next();
              executeNode(appSpec, node, instantiator, classLoader, token);
            } catch (Throwable t) {
              Throwable rootCause = Throwables.getRootCause(t);
              if (rootCause instanceof InterruptedException) {
                LOG.debug("Workflow '{}' with run id '{}' aborted", workflowSpec.getName(),
                          workflowRunId.getRun());
                basicWorkflowContext.setState(new ProgramState(ProgramStatus.KILLED, rootCause.getMessage()));
                break;
              }
              basicWorkflowContext.setState(new ProgramState(ProgramStatus.FAILED, rootCause.getMessage()));
              throw Throwables.propagate(rootCause);
            }
      

      Here, getRootCause() discards the outer stack trace, and we won't know where in the code (what action, what type of action, user code or system code) it was thrown. Instead we should always rethrow the original exception. If unwrapping is necessary it can be done at the top most level where the exception is handled or logged.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ashau Albert Shau
                Reporter:
                andreas Andreas Neumann
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated: