class ResponseBuilder implements java.io.Serializable {

    public WorkflowException buildWorkflowException(DelegateExecution execution) {

        String method =
                getClass().getSimpleName() + ".buildWorkflowException(" + "execution=" + execution.getId() + ")";

        logger.debug("Entered " + method);

        String prefix = (String) execution.getVariable("prefix");
        String processKey = getProcessKey(execution);

        logger.debug("processKey=" + processKey);

        // See if there"s already a WorkflowException object in the execution.
        WorkflowException theException = (WorkflowException) execution.getVariable(WORKFLOWEXCEPTION);

        if (theException != null) {
            logger.debug("Exited " + method + " - propagated " + theException);
            return theException;
        }

        // Look in the legacy variables: ErrorResponse and ResponseCode

        String errorResponse = trimString(execution.getVariable(prefix + "ErrorResponse"), null);
        String responseCode = trimString(execution.getVariable(prefix + "ResponseCode"), null);
        logger.debug("errorResponse=" + errorResponse);
        logger.debug("responseCode=" + responseCode);
        if (errorResponse != null || !isOneOf(responseCode, null, "0", "200", "201", "202", "204")) {
            // This is an error condition. We need to return a WorkflowExcpetion

            if (errorResponse == null) {
                // No errorResponse string. See if there"s something in the Response variable
                String response = trimString(execution.getVariable(processKey + "Response"), null);
                if (response == null) {
                    errorResponse = "Received response code " + responseCode + " from " + processKey;
                } else {
                    errorResponse = response;
                }
            }

            // Some subflows may try to return a WorkflowException as XML in the
            // errorResponse. If provided, use the errorCode and errorMessage
            // from the XML

            String maybeXML = removeXMLNamespaces(errorResponse);

            String xmlErrorMessage = trimString(getXMLTextElement(maybeXML, "ErrorMessage"), null);
            String xmlErrorCode = trimString(getXMLTextElement(maybeXML, "ErrorCode"), null);

            if (xmlErrorMessage != null || xmlErrorCode != null) {
                logger.debug("xmlErrorMessage=" + xmlErrorMessage);
                logger.debug("xmlErrorCode=" + xmlErrorCode);

                if (xmlErrorMessage == null) {
                    errorResponse = "Received error code " + xmlErrorCode + " from " + processKey;
                } else {
                    errorResponse = xmlErrorMessage;
                }

                if (xmlErrorCode != null) {
                    responseCode = xmlErrorCode;
                }
            }

            // Convert the responseCode to an integer

            int intResponseCode;

            try {
                intResponseCode = Integer.valueOf(responseCode);
            } catch (NumberFormatException e) {
                // Internal Error
                intResponseCode = 2000;
            }

            // Convert 3-digit HTTP response codes (we should not be using them here)
            // to appropriate 4-digit response codes

            if (intResponseCode < 1000) {
                if (intResponseCode >= 400 && intResponseCode <= 499) {
                    // Invalid Message
                    intResponseCode = 1002;
                } else {
                    // Internal Error
                    intResponseCode = 2000;
                }
            }

            // Create a new WorkflowException object

            theException = new WorkflowException(processKey, intResponseCode, errorResponse);
            execution.setVariable(WORKFLOWEXCEPTION, theException);
            logger.debug("Exited " + method + " - created " + theException);
            return theException;
        }

        logger.debug("Exited " + method + " - no WorkflowException");
        return null;
    }

}