Original Message:
Sent: Thu September 14, 2023 05:37 AM
From: pratap vadlapati
Subject: Gateway script error and exception handling
Hi Steve Linn
We are trying to send custom Error, as suggested we configured Global-Error-Policy, we have faced issued --
- Global-error-policy is not triggered until we put throw action in the API Assembly. Is this how it works ? I assume it should trigger for all errors without a throw in API flow.
- if we put throw action, then in context.get('error'); -- we are getting error code as 500 internal error instead of 401 or 400
- How to handle below error (this is not available in assembly to configure throw action) and send custom error{
"httpCode": "400",
"httpMessage": "Bad Request",
"moreInformation": "One or more required API parameters are missing in the API request."
}
------------------------------
pratap vadlapati
Original Message:
Sent: Thu April 06, 2023 11:26 AM
From: Steve Linn
Subject: Gateway script error and exception handling
Hi Ashok,
In the API Gateway, use context.reject not session.reject. See https://www.ibm.com/docs/en/datapower-gateway/10.5?topic=gateway-apis-manage-messages#context__context.reject for examples, but context.reject will stop API execution. As for form a generic JSON response and return it back to the consumer
if you want something more than the standard default error, you'll need to implement a post-error global policy. All errors will execute this policy so you'll need to at a minimum a default catch and then an explicit catch for ForbiddenError, UnauthorizedError, and BadRequestError as these require explicit catches to be caught. That code will still have the context.get('error') object so you can get the details of the error and use whatever else you want to get from the context to augment your response in message.body. Finally, any catch of an error, even in a post-error global policy, will clear the HTTP status code and reason phrase. This information should be in the context error object, so you'll need to set those back into message.status or the client will get a HTTP 200 OK. For more on post-error global policies, see https://www.ibm.com/docs/en/api-connect/10.0.1.x?topic=applications-working-global-policies as a starting point.
Best Regards,
Steve
------------------------------
Steve Linn
Senior Consulting I/T Specialist
IBM
Original Message:
Sent: Thu April 06, 2023 07:06 AM
From: Ashok Beshra
Subject: Gateway script error and exception handling
Hi Team...
Please suggest best practises for handling error / exceptions inside the Gateway script code and stop processing the assembly. We have tried session.reject and it is returning 500 Internal server error with default JSON as below. But in such cases, we wanted to form a generic JSON response and return it back to the consumer. Please advise how this can be achieved. Thanks
Code during user exception in Gateway script
if(apicTimestamp > consentExpiry){
console.error('Consent Expired' );
var respRoot = 'GetAccountListRes';
var resp={};
resp[respRoot] = {};
resp[respRoot].ResponseHeader = {};
resp[respRoot].ResponseHeader.ErrorDetails = {};
resp[respRoot].ResponseHeader.ErrorDetails.ErrorInfo = {};
resp[respRoot].ResponseHeader.ErrorDetails.ErrorInfo.ErrorCode = 'SYS_ERR';
resp[respRoot].ResponseHeader.ErrorDetails.ErrorInfo.ErrorShortDesc = 'Consent Expired.';
session.reject("Consent Expired");
context.message.body.write(resp);
context.message.header.set('Content-Type', "text/JSON");
return;
}
else{
console.error('Consent Valid' );
}
Response received during session.reject is below by default, but please advise how can we return entire Response object and stop further processing.
{
"httpCode": "500",
"httpMessage": "Internal Server Error",
"moreInformation": "Consent Expired"
}
------------------------------
Ashok Beshra
------------------------------