ErrorResponse Class
Namespace
Example
This example snippet of a mock tax adapter shows a hypothetical scenario to demo an error response. The adapter receives request information from TaxEngineContext and stores it in an instance of CalculateTaxRequest. If the request's documentCode property is null or indicates an error, the adapter returns an error response with information about the error.
1global virtual class MockAdapter implements commercetax.TaxEngineAdapter {
2
3 global commercetax.TaxEngineResponse processRequest(commercetax.TaxEngineContext taxEngineContext) {
4 commercetax.RequestType requestType = taxEngineContext.getRequestType();
5 commercetax.CalculateTaxRequest request = (commercetax.CalculateTaxRequest)taxEngineContext.getRequest();
6
7if(request.documentCode == null) {
8 return new commercetax.ErrorResponse(commercetax.resultcode.TaxEngineError, '404', 'documentCode is mandatory');
9 }
10 if(request.documentCode == 'TaxEngineError') {
11 return new commercetax.ErrorResponse(commercetax.resultcode.TaxEngineError, '504', 'documentCode - not supported');
12 }
13 if(request.documentCode == 'simulateValidationFailureInAdapter') {
14 return new commercetax.ErrorResponse(commercetax.resultcode.TaxEngineError, '400', 'validations for documentCode failed in adapter');
15 }
16 if(request.documentCode == 'simulateMalformedErrorInAdapter') {
17 return new commercetax.ErrorResponse(commercetax.resultcode.TaxEngineError, null, 'malformed adapter error response');
18 }
19 if(request.documentCode == 'simulateTaxEngineProcessFailure') {
20 return new commercetax.ErrorResponse(commercetax.resultcode.TaxEngineError, '500', 'Tax Engine couldnt process your request');
21 }ErrorResponse Constructors
The ErrorResponse class includes these constructors.
ErrorResponse(resultCode, errorCode, errorMessage)
Signature
global ErrorResponse(commercetax.ResultCode resultCode, String errorCode, String errorMessage)
Parameters
-
resultCode:
Type: ResultCode
Code for the type of result sent by the tax engine.
-
errorCode:
Type: String
Code for the type of error sent by the tax engine. Codes must match the HTTP status codes to be returned to the user. Here are a few examples:
- If the status code is for a bad request, set errorCode to 400.
- If the status code is for a forbidden request, set errorCode to 403.
- If errorCode isn't a valid HTTP status code, a 500 internal server error is returned.
-
errorMessage:
Type: String
The error message sent by the tax engine.