Skip to content

Commit 688e889

Browse files
authored
Merge pull request #21 from titans-of-code/feature/customNode
Added feature to customize the error response field name
2 parents 5e3d62c + 517cc12 commit 688e889

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The error is converted by this module into the items below.
1212
- **Error reason**: *Reason phrase* from the [HTTP RFC 7231 Response Status Codes][http-rfc-7231-6] for [Client Error 4xx][http-rfc-7231-6.5] and [Server Error 5xx][http-rfc-7231-6.6].
1313
- **Error message**: This can be any text description, array, or object. It is usually the Mule `error.description` or developer-defined dataweave or text.
1414

15-
A JSON body response example is shown below.
15+
A JSON body response example is shown below. Note that the top-level field name, `error`, is customizable.
1616

1717
**StatusCode: 400**
1818

@@ -332,7 +332,41 @@ The dataweave should resolve to a string but any type is allowed. The default v
332332
error.exception.errorMessage.payload.error.message default ''
333333
```
334334

335-
**Note: Leave this field empty if you do not want to propagate previous errors.**
335+
**Set this field to an empty string if you do not want to propagate previous errors.**
336+
337+
### Response Key Name for Payload
338+
This field allows you to customize the JSON key name where the error payload is set. This defaults to `error`, which is shown in the examples. This only supports changing the name of the top-level key; it does not change any other format. If you want the error payload to be the top-level element in the response, then set this field to empty string.
339+
340+
**Default Response**: error
341+
```
342+
{
343+
"error": {
344+
"code": 400,
345+
"reason": "Bad Request",
346+
"message": "Error validating response"
347+
}
348+
}
349+
```
350+
351+
**Custom Response**: errorDetails
352+
```
353+
{
354+
"errorDetails": {
355+
"code": 400,
356+
"reason": "Bad Request",
357+
"message": "Error validating response"
358+
}
359+
}
360+
```
361+
362+
**Custom Response**: empty string
363+
```
364+
{
365+
"code": 400,
366+
"reason": "Bad Request",
367+
"message": "Error validating response"
368+
}
369+
```
336370

337371
## Error Handling Tips
338372

advanced.png

5.4 KB
Loading

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>ORG_ID_TOKEN</groupId>
66
<artifactId>api-error-handler</artifactId>
7-
<version>6.0.0</version>
7+
<version>6.1.0</version>
88
<packaging>mule-extension</packaging>
99

1010
<name>API Error Handler</name>

src/main/resources/module-error-handler-plugin.xml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@
9898

9999
<parameter name="previousError" type="any" use="OPTIONAL"
100100
displayName="Use Previous Error"
101-
defaultValue="#[error.exception.errorMessage.typedValue.error.message default '']"
101+
defaultValue="#[error.exception.errorMessage.payload.error.message default '']"
102+
summary="Script to generate the previous error message. Usually pulled from the external system's response in the error object. Set empty string to ignore previous errors."
103+
role="BEHAVIOUR" tab="Advanced" />
104+
105+
<parameter name="responseKey" type="any" use="OPTIONAL"
106+
displayName="Response Key Name for Payload"
107+
defaultValue="#['error']"
108+
summary="The response key name to which to add the error payload. Set empty string to set the payload as the root element."
102109
role="BEHAVIOUR" tab="Advanced" />
103110

104111
</parameters>
@@ -134,13 +141,18 @@
134141
import * from module_error_handler_plugin::common
135142
output application/json
136143
var error = getError(vars.errorType, vars.defaultErrors, vars.customErrors default {})
137-
---
138-
{
139-
error: error update {
144+
var responseKey = vars.responseKey
145+
var responseError = error update {
140146
// Use the previous error message if it exists, otherwise use the provided message.
141147
case message at .message -> if (!isEmpty(vars.previousError)) vars.previousError else message
142-
}
143-
}
148+
}
149+
---
150+
if (!isEmpty(responseKey) and (responseKey is String))
151+
{
152+
(responseKey): responseError
153+
}
154+
else
155+
responseError
144156
]]>
145157
</ee:set-payload>
146158
<ee:set-attributes><![CDATA[%dw 2.0

0 commit comments

Comments
 (0)