Skip to content

Commit e513ed4

Browse files
Added errorLog feature and updated docs
1 parent 206470d commit e513ed4

File tree

9 files changed

+618
-69
lines changed

9 files changed

+618
-69
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ out/
6666
# OS files #
6767
.DS_Store
6868
.svn
69-
._*
69+
._*
70+
/bin/

README.md

Lines changed: 116 additions & 49 deletions
Large diffs are not rendered by default.

examples/customErrors.dwl

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,44 @@ var previousError = getPreviousErrorMessage(error)
1212
This catches custom service unauthorized error from app and formats the response accordingly.
1313
*/
1414
"APP:UNAUTHORIZED": {
15-
"code":401,
16-
"reason": "Unauthorized",
17-
"message": error.description
15+
code: 401,
16+
reason: "Unauthorized",
17+
message: error.description
1818
},
1919

2020
/*
2121
APP 503 Service Unavailable
2222
This catches custom service unavailable error from app and formats the response accordingly.
2323
*/
2424
"APP:SERVICE_UNAVAILABLE": {
25-
"code":503,
26-
"reason": "Service Unavailable",
27-
"message": error.description
25+
code: 503,
26+
reason: "Service Unavailable",
27+
message: error.description
2828
},
2929

3030
/*
3131
HTTP 500 Pass Through
3232
This catches HTTP 500 errors and propagates the detailed reason for failure.
33-
It uses the error.message field from the response of the HTTP call that failed, which conforms to the API Error Handler responses.
33+
It uses the error.message field from the response of the HTTP call that failed for the message, which conforms to the API Error Handler responses.
3434
If not found, the error.description will be returned, which generally says an internal server error occurred.
3535
This useful for process or experience APIs to pass through system API errors.
3636
*/
3737
"HTTP:INTERNAL_SERVER_ERROR": {
38-
"code":500,
39-
"reason": "Internal Server Error",
40-
"message": if (!isEmpty(previousError)) previousError else error.description
38+
code: 500,
39+
reason: "Internal Server Error",
40+
message: if (!isEmpty(previousError)) previousError else error.description
41+
},
42+
43+
/*
44+
Unknown Errors
45+
This catches unknown errors, which includes any non-standard HTTP error status code and propagates the detailed reason for failure.
46+
It tries to use the called API's error response code and phrase if available in the error. If not, it uses the default 500 response.
47+
It uses the error.message field from the response of the HTTP call that failed for the message, which conforms to the API Error Handler responses.
48+
If not found, the error.description will be returned, which generally says an internal server error occurred.
49+
*/
50+
"MULE:UNKNOWN": {
51+
code: error.exception.errorMessage.attributes.statusCode default 500,
52+
reason: error.exception.errorMessage.attributes.reasonPhrase default "Internal Server Error",
53+
message: if (!isEmpty(previousError)) previousError else error.description
4154
}
4255
}

exchange-docs/home.md

Lines changed: 438 additions & 0 deletions
Large diffs are not rendered by default.

handlerFlow.png

-5.23 KB
Loading

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>ORG_ID_TOKEN</groupId>
@@ -16,7 +16,7 @@
1616

1717
<!-- XML SDK properties -->
1818
<mule.version>4.3.0</mule.version>
19-
<mule.extensions.maven.plugin.version>1.4.0</mule.extensions.maven.plugin.version>
19+
<mule.extensions.maven.plugin.version>1.4.1</mule.extensions.maven.plugin.version>
2020
<app.runtime>4.3.0</app.runtime>
2121
</properties>
2222

@@ -71,6 +71,12 @@
7171
<url>https://repository.mulesoft.org/snapshots/</url>
7272
<layout>default</layout>
7373
</repository>
74+
<repository>
75+
<id>anypoint-exchange-v3</id>
76+
<name>Anypoint Exchange V3</name>
77+
<url>https://maven.anypoint.mulesoft.com/api/v3/maven</url>
78+
<layout>default</layout>
79+
</repository>
7480
</repositories>
7581

7682
<pluginRepositories>

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,18 @@
146146
<ee:set-attributes><![CDATA[%dw 2.0
147147
import * from module_error_handler_plugin::common
148148
output application/java
149-
var code = getError(vars.errorType, vars.defaultErrors, vars.customErrors default {}).code default 500
149+
var error = getError(vars.errorType, vars.defaultErrors, vars.customErrors default {})
150+
var code = error.code default 500
151+
var message = toString(error.message, "Internal Server Error")
152+
var previousMessage = toString(vars.previousError)
153+
var description = toString(vars.error.description)
154+
155+
// Concatenate current error message, previous error message, and error description without duplicates or empties
156+
var log = (([message, previousMessage, description] filter !isEmpty($)) distinctBy $) joinBy " | "
150157
---
151158
{
152-
httpStatus: code as Number
159+
httpStatus: code as Number,
160+
errorLog: log
153161
}]]>
154162
</ee:set-attributes>
155163
</ee:message>

src/main/resources/module_error_handler_plugin/common.dwl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,36 @@
55
This file must be in the ./resources/<module name> folder in order to be exported in the META-INF/mule-artifact/mule-artifact.json file. This allows the functions to be used in the module as they are executed in the app's context (without the mule message context).
66
*/
77

8-
import mergeWith from dw::core::Objects
9-
108
// Downstream error pulled off the Mule error object, which conforms to the API Error Handler responses.
119
fun getPreviousErrorMessage(error) = error.exception.errorMessage.typedValue.error.message default ""
1210

13-
// Get the error type string
11+
/*
12+
* Get the error type as a String
13+
*/
1414
fun getErrorTypeAsString(errorType) =
1515
if (!isBlank(errorType.namespace))
1616
errorType.namespace ++ ":" ++ (errorType.identifier default "")
1717
else
1818
"UNKNOWN"
1919

20-
// Get the proper error from the merged default and custom error lists. Provide a standard error if none found.
20+
/*
21+
* Get the proper error from the merged default and custom error lists. Provide a standard error if none found.
22+
*/
2123
fun getError(errorType, defaultErrors, customErrors = {}) = do {
22-
var errorList = (defaultErrors mergeWith customErrors)
24+
import mergeWith from dw::core::Objects
25+
var errorList = (defaultErrors mergeWith (customErrors default {}))
2326
var foundError = errorList[errorType]
2427
var error = if ( !isEmpty(foundError) ) foundError else errorList["UNKNOWN"]
2528
---
2629
error
2730
}
31+
32+
/*
33+
* Convert non-empty items to String. If not a string, then writes in Java format.
34+
* If empty, then returns the default value provided
35+
*/
36+
fun toString(obj, def="") = obj match {
37+
case item if (item is String and !isEmpty(item)) -> item
38+
case item if !isEmpty(item) -> write(item, "application/java")
39+
else -> def
40+
}

src/main/resources/output-attribute-schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"properties": {
44
"httpStatus": {
55
"type": "string"
6+
},
7+
"errorLog": {
8+
"type": "string"
69
}
710
},
811
"additionalProperties": false

0 commit comments

Comments
 (0)