What is Raise Fault Policy..?
The
RaiseFault policy allows you to create custom messages in case of error
conditions. This policy returns a FaultResponse to the requesting application
if it encounters an error condition.
When
the message flow switches to the default ErrorFlow, no further policy processing
occurs. All remaining processing steps are bypassed, and the FaultResponse is
returned directly to the requesting app.
Example:
We will create a API Proxy and specify end-point URL and then try to access it from Postman by using username/password.
If login credentials are correct then we will get response from the end-point, else we will get custom error message from RaiseFault policy.
API Proxy Creation:
End Point URL: https://dummy.restapiexample.com/api/v1/employees
(replace it with your URL)
Save it and go to Policies
Keep Basic Authentication Policy and Raise Fault Policy as shown below..
For Basic Authentication Policy:
<BasicAuthentication async='true' continueOnError='false' enabled='true' xmlns='http://www.sap.com/apimgmt'>
<Operation>Decode</Operation>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<User ref='current.username'></User>
<Password ref='current.password'></Password>
<Source>request.header.Authorization</Source>
</BasicAuthentication>
For Raise Fault Policy:
<RaiseFault async="true" continueOnError="false" enabled="true" xmlns="http://www.sap.com/apimgmt">
<FaultResponse>
<Set>
<Headers/>
<Payload contentType="application/json">{"status" : "Error", "messege" : "401 Invalid User or Password", "Suggestion" : "Try with correct user name / password " } </Payload>
<StatusCode>401</StatusCode>
<ReasonPhrase>Unauthorized</ReasonPhrase>
</Set>
</FaultResponse>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>
Save it and deploy it.
Now test it from Postman..with Correct login credentials
With incorrect logins
Based on your requirement, you can customize RaiseFault Policy.
That's it.
Source: SAP Community
No comments:
Post a Comment