Error Handling
Error Handling
Service classes should be prepared to handle exceptional conditions, such as programming errors or system failures.
All Web Central API methods throw ExceptionBase exception in case of a failure. The workflow rule container will handle these exceptions automatically: it will log the exception message and the stack trace to \WEB-INF\config\archibus.log , convert the low-level error code to Archibus user-friendly error message, localize the message text for the user's locale, and return the exception to the client.
In many cases the default error handling is sufficient, and you do not need to write any additional code to handle exceptions.
You will need to add the try/catch block to your service code if:
- You call code that throws non-ExceptionBase exceptions, such as Java library methods.
- You can provide more useful error message for the user.
When adding the try/catch block, the recommended practice is to:
- Prepare user-friendly error message (such as, "the requested project X could not be approved due to system error") and attach it to the WFR response.
- Throw the exception and let the Workflow Rule Container finalize the exception processing (roll back the database transaction, log the detailed exception information, format the service response and send it back to the view).
Here is an example of the service code that handles the exceptional conditions:
try {
// perform service operations as required
...
} catch (ExceptionBase originalException) { // catch only ExceptionBase
// re-throw the exception for the Workflow Rule Container
throw originalException;
} catch (Throwable originalException) { // catch all other errors
// prepare the error message
// @translatable
String errorMessage = "The end user will see this error message";
// re-throw the exception for the Workflow Rule Container
ExceptionBase newException = new ExceptionBase(null, message, originalException);
throw newException;
}
If you have many services that use the same exception handling block, we recommend encapsulating the exception handling details (the code inside the catch block) into a separate method, and reusing this method from all service. This way the exception handling block will be much simpler:
try {
// perform service operations as required
...
} catch (Throwable t) {
handleException(context, t);
}