Friday, July 25, 2025

Raise Fault Policy | API Management

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.

 A FaultResponse can consist of HTTP headers, query parameters, and a message payload. These elements can be populated using variables. This enables you to send customized FaultResponses that are specific to the error conditions.

During execution, the RaiseFault policy transfers the message flow to the default ErrorFlow, which in turn returns the designated FaultResponse to the requesting application.

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>



Specify Condition String:
(current.username != "YOUR_USERNAME")  OR (current.password != "YOUR_PASSWORD")


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

Thanks for reading :-)

No comments:

Post a Comment

Raise Fault Policy | API Management

What is Raise Fault Policy..? The RaiseFault policy allows you to create custom messages in case of error conditions. This policy returns a ...