Skip to content

Commit

Permalink
Problem details production 2 (#1428)
Browse files Browse the repository at this point in the history
* - Allow exchange besides exc in Groovy and Javascript
- More details in script errors

* Cleanup

* Add method and URI in routing ProblemDetails message
  • Loading branch information
predic8 authored Dec 19, 2024
1 parent 248fc2e commit 99fbf50
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 30 deletions.
21 changes: 0 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,27 +478,6 @@ The following example generates a JSON response and sends it directly to the cli
#### Learn More
For more details about using JavaScript with Membrane, check the [JavaScript Plugin documentation](https://www.membrane-api.io/docs/current/javascript.html).

### Javascript Extenstion

Besides Groovy you can realize custom behavior with Javascript.

```xml
<api port="2020">
<javascript>
console.log("------------ Headers: -------------")

var fields = header.getAllHeaderFields();

for(i=0;i &lt; fields.length;i++) {
console.log(fields[i]);
}
CONTINUE
</javascript>
<target url="https://api.predic8.de"/>
</api>
```


## Message Transformation

### Manipulating HTTP Headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public Response build() {
root.put("message",exception.getMessage());
root.put("stackTrace", exception.getStackTrace());
}
root.put("attention", """
Membrane is running in development mode. For production set <router production="true"> to reduce the details in error message!""");
root.putAll(extensions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ public Outcome handleRequest(Exchange exc) throws Exception {

if (rule instanceof NullRule) {
// Do not log. 404 is too common
exc.setResponse(ProblemDetails.user(router.isProduction())
.statusCode(404)
.title("Wrong path or method")
.detail("This request was not accepted by Membrane. Please check HTTP method and path.")
.build());
ProblemDetails pd = ProblemDetails.user(router.isProduction())
.statusCode(404)
.title("Wrong path or method")
.detail("This request was not accepted by Membrane. Please check HTTP method and path.");

if (!router.isProduction()) {
pd.extension("method", exc.getRequest().getMethod());
pd.extension("uri", exc.getRequest().getUri());
}

exc.setResponse(pd.build());
return ABORT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,7 @@ protected void handleScriptExecutionException(Exchange exc, Exception e) {

if (!router.isProduction()) {
pd.extension("message", e.getMessage())
.extension("source", trim(src))
.extension("note", """
To hide error details set Membrane into production mode. In proxies.xml use <router production="true">..</router>.");
""");
.extension("source", trim(src));
} else {
pd.detail("See logs for details.");
}
Expand Down

0 comments on commit 99fbf50

Please sign in to comment.