forked from wso2/docs-mi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ThrowError mediator docs Related to wso2/product-micro-integrator/issues/3843
- Loading branch information
1 parent
eb2eed4
commit d3b6b8d
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# ThrowError Mediator | ||
|
||
The ThrowError mediator allows you to raise an error from the mediation sequence. The error type and message can be customized as required. | ||
|
||
!!! Info | ||
Errors thrown by the ThrowError mediator is handled by the onError sequence (if defined) or the default fault sequence. | ||
|
||
## Syntax | ||
|
||
``` java | ||
<!-- Error message as string --> | ||
<throw-error type="string" errorMessage="string"></throw-error> | ||
<!-- Dynamic error message --> | ||
<throw-error type="string" errorMessage="{string}"></throw-error> | ||
``` | ||
|
||
## Examples | ||
|
||
Following examples demonstrate the use of Throw Error mediator. | ||
|
||
### Example 1 | ||
|
||
The following API configuration demonstrates how to use the Throw Error mediator to raise an error when some required field is not present in the incoming payload. | ||
|
||
``` xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<api context="/testThrowError" name="TestThrowErrorMediatorAPI" xmlns="http://ws.apache.org/ns/synapse"> | ||
<resource methods="POST"> | ||
<inSequence> | ||
<filter xpath="${exists(payload.required)}"> | ||
<then> | ||
<log level="full"/> | ||
<respond/> | ||
</then> | ||
<else> | ||
<variable name="ERROR_MSG" value="Required field does not exist"/> | ||
<throwError type="PAYLOAD_ERROR" errorMessage="{${var.ERROR_MSG}}"/> | ||
</else> | ||
</filter> | ||
</inSequence> | ||
<faultSequence> | ||
<log level="custom"> | ||
<property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/> | ||
<property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/> | ||
</log> | ||
<drop/> | ||
</faultSequence> | ||
</resource> | ||
</api> | ||
``` | ||
|
||
Invoking the above API with a payload that does not contain the required field will raise an error. | ||
``` | ||
curl --location 'http://localhost:8290/testThrowError' \ | ||
--header 'Content-Type: application/json' \ | ||
--data '{ | ||
"Hello": "World" | ||
}' | ||
``` | ||
|
||
Response log will contain the error message as follows. | ||
``` | ||
INFO {LogMediator} - {api:TestThrowErrorMediatorAPI} ERROR_CODE = PAYLOAD_ERROR, ERROR_MESSAGE = Required field does not exist | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters