API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Gateway script error and exception handling

    Posted Thu April 06, 2023 07:06 AM
    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
    ------------------------------


  • 2.  RE: Gateway script error and exception handling

    Posted Thu April 06, 2023 11:26 AM

    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
    ------------------------------



  • 3.  RE: Gateway script error and exception handling

    Posted Thu September 14, 2023 05:38 AM

    Hi Steve Linn

    We are trying to send custom Error, as suggested we configured Global-Error-Policy, we have faced issued -- 

    1. 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.
    2.   if we put throw action, then in context.get('error'); -- we are getting error code as 500 internal error instead of 401 or 400
    3.  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
    ------------------------------



  • 4.  RE: Gateway script error and exception handling

    Posted Mon September 18, 2023 10:01 AM

    Hi Pratap,
    Q1:) Yes, it should catch any exception.  That could be from a Throw policy, a context.reject within a GatewayScript policy, or an exception thrown by the policy itself such as an Invoke policy with a Stop on Error, a parse policy will throw BadRequestError normally, etc.
    Q2:) The throw policy allows you to specify your own exception name and also the HTTP status code and reason phrase you want to have. Are you saying you're always getting a 500 Internal Server Error even when you specify something else? Odds are you didn't specify that and 500 Internal Server Error is  the default.
    Q3:) Answered in another thread.
    Best Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------