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.  How do I change the response format of context.reject?

    Posted Tue July 18, 2023 12:30 AM
    Edited by dalsu Byun Tue July 18, 2023 08:12 PM

    Hi

    I want to change the response Message Format of context.reject in Gatewayscript.


    1. context.reject 

    #GatewayScript

    context.message.statusCode = "501 Custom Error";

    context.reject("GWSError","Error Message"); 

    #Response

    {
        "httpCode""501",
        "httpMessage""Custom Error",
        "moreInformation""Error Message"
    }
    2. In case of trying to process in Global Error Policy after handling as throw Exception
    #GatewayScript
    throw Exception("Error Message");
    #Response : Always handled as a JavaScriptError
    {
        "name""JavaScriptError",
        "message""Internal Error",
        "status": {
            "code"500,
            "reason""Internal Server Error"
        },
        "policyTitle"null
    }
    ---------------------------------------------------
    #Desired Response Format
    {
        "name""Error Title",
        "message""message",
        "status": {
            "code": 501,
            "reason""Custom Error"
        },
        "policyTitle"null
    }



    ------------------------------
    dalsu Byun
    ------------------------------



  • 2.  RE: How do I change the response format of context.reject?

    Posted Wed July 19, 2023 02:52 PM

    Hi Dalsu,
    Changing the default error response will require a global error policy.  Note in this policy you will need a default catch at a minimum, but some errors (BadRequestError, UnauthorizedError, and ForbiddenError) will require an explicit catch.  When the error is caught, the HTTP status code and reason phrase are reset to 200 OK, so you'll need to use the error object context.get('error') and reset the status code and phrase using context.message.statusCode and the values from the error object.  At for the payload you can do whatever you wish.  Please let me know if there are more specific questions on the message content I'm missing.

    Best Regards,
    Steve Linn



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