diff --git a/samples/steps/http-get.md b/samples/steps/http-get.md index 49ad3536..c46d4eef 100644 --- a/samples/steps/http-get.md +++ b/samples/steps/http-get.md @@ -141,4 +141,26 @@ return_value: return: ${the_message.request} ``` +### HTTP Error handling + +It is possible to specify the DSL step to follow when GET request gets a +non-OK response. For that the `error` field can be used +``` +get_message: + call: http.get + args: + url: http://localhost:8080/guards/fail/mock-response #step that always gives 403 + query: + var: "value" + result: the_message + error: error_step + +return_value: + return: ${the_message.request} + +error_step: + return: "Request failed" + status: 500 +``` + [Back to Guide](../GUIDE.md#Writing-DSL-files) diff --git a/samples/steps/http-post.md b/samples/steps/http-post.md index 60e375b5..37892422 100644 --- a/samples/steps/http-post.md +++ b/samples/steps/http-post.md @@ -75,4 +75,26 @@ return_value: return: ${the_message.response} ``` +### HTTP Error handling + +It is possible to specify the DSL step to follow when POST request gets a +non-OK response. For that the `error` field can be used +``` +post_step: + call: http.post + args: + url: http://localhost:8080/nonexistant + contentType: plaintext + plaintext: + "byrokratt" + result: the_message + +return_value: + return: ${the_message.request} + +error_step: + return: "Request failed" + status: 500 +``` + [Back to Guide](../GUIDE.md#Writing-DSL-files) diff --git a/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java b/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java index 87ee1209..9ecf91c1 100644 --- a/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java +++ b/src/main/java/ee/buerokratt/ruuter/domain/steps/http/HttpStep.java @@ -37,6 +37,9 @@ public abstract class HttpStep extends DslStep { protected DefaultHttpDsl localHttpExceptionDsl; protected Logging logging; + @JsonAlias("error") + protected String onErrorStep; + @Override protected void executeStepAction(DslInstance di) { args.checkUrl(di); @@ -44,7 +47,11 @@ protected void executeStepAction(DslInstance di) { di.getContext().put(resultName, new HttpStepResult(args, response, MDC.get("spanId"))); if (!isAllowedHttpStatusCode(di, response.getStatusCodeValue())) { - throw new IllegalArgumentException(); + if (getOnErrorStep() != null) { + setNextStepName(getOnErrorStep()); + } + else + throw new IllegalArgumentException(); } }