default fault rule is changing the status code and reason phrase

In an oauth policy (operation: generate token), set generate response property to true and hit it without grant_type then it's giving me status code 400 (Bad Request) which is correct. But if I set generate response to false and catch the error using default fault rule and set the "{message.status.code}" as StatusCode and "{message.reason.phrase}" as ReasonPhrase in assign message policy then it's giving me status code 500 (Internal Server Error) instead of 400 (Bad Request). I am not sure from where 500 is getting populated.

0 3 603
3 REPLIES 3

@Mahammad Feroz , Can you please post assign message policy to understand same ?

I believe, by default it's 500. You need to override same in AssignMessage policy using set status code option. For more details check policy documentation here.

<Set>     
    <StatusCode>400</StatusCode>      
</Set>

@Anil Sagar, PFB assign message policy.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="AM_FaultHandling"> <DisplayName>AM_FaultHandling</DisplayName> <Properties/> <Set> <Headers/> <Payload contentType="application/json" variablePrefix="@" variableSuffix="#"> { "errorCode": "@errorCode#", "errorMessage": "@errorMessage#", "contexts":["@errorContext#"], } </Payload> <StatusCode>{message.status.code}</StatusCode> <ReasonPhrase>{message.reason.phrase}</ReasonPhrase> </Set> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> <AssignTo createNew="false" transport="http" type="request"/> </AssignMessage>

@Mahammad Feroz

When generateResponse is set to true, the error response generated by the policy is like below:

{"ErrorCode" : "invalid_request", "Error" :"Required param : grant_type"}

with status code 400 and reason phrase Bad Request.

Also the oauthV2 policy does not contain the failure symbol capture.jpg.

But when generateResponse is set to false. The policy itself fails capture.jpg with error message structure:

{"fault":{"detail":{"errorcode":"invalid_request"},"faultstring":"invalid_request"}}

and in Apigee I have observed that whenever a policy fails the fault message structure by default is like below with 500 Internal Server Error.

{"fault":{"detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"},"faultstring":"Execution of JavaScript-1 failed with error: Javascript runtime error: \"ReferenceError: \"jhj\" is not defined. (JavaScript-1.js:1)\""}}

This could be the reason why you are seeing different error codes.