Good APIs should properly report errors when error occurs. There are many scenarios that might cause error to occur. Wrong configuration setting, failed connection to a 3rd party API and invalid input are a few examples. There are two types of errors in AlchemyJ. They are System error and Model error.
System errors are errors raised by AlchemyJ Extended Functions. When an error occurs in an AlchemyJ Extended Function, it creates an error record in the System Error List.
Model errors are raised by formula ajRaiseError which defined by user. There may be times when you want to manually handle an error. For example, when you catch a bad input or want to notify user for some unexpected result.
Using Exception Handling Component
In Data Validation (Backend) recipe book, it has set up a data dictionary and use ajCheckDataItem to validate and transform the input. This recipe is to configure the exception handling component to notify user when the input cannot pass the validation.
- Click Add Component button in AlchemyJ ribbon, select Exception Handling to add the @@ExceptionHandling worksheet.
We will use the last column result in F11 as the flag in Exception Detection to determine whether to raise error. The exception message is the concatenate values form E7:E11 whose values are the message returned by ajCheckDataItem.
The following are the value that would be set in this example. For the details of each column, refer to Exception Handling Worksheet.
|Exception Description||Validate Customer Info-Input Error|
|Function Point||Validate Customer Info|
Include the exception handling worksheet to Related Worksheets in ##RestEndpointGroup worksheet.
Click Generate API button in AlchemyJ ribbon and launch the API.
Use Postman to send a request with invalid input, the error message defined in the exception component returned instead of the value in the output table.
Use Postman to send a request with valid input, the transformed value was returned.
Configuring REST API error handling behavior
The following properties in REST API worksheet can control how a REST API behaves when errors occur:
Refer to the REST API Worksheet for more details.