From 62e33485f1a3c61484b2bc3e241ff5bc7b6d6b9d Mon Sep 17 00:00:00 2001 From: jacob-wieland-gematik <168740605+jacob-wieland-gematik@users.noreply.github.com> Date: Wed, 29 Jan 2025 16:06:50 +0100 Subject: [PATCH] unwrap runtime exceptions created by rest when reporting (#3609) --- .../main/java/net/serenitybdd/rest/SerenityRest.java | 3 ++- .../rest/filters/FieldsRecordingFilter.java | 5 +++-- .../rest/filters/UpdatingContextFilter.java | 3 ++- .../serenitybdd/rest/utils/RestReportingHelper.java | 9 +++++++-- .../rest/utils/RestResponseRecordingHelper.java | 4 ++-- .../serenitybdd/rest/utils/RestRuntimeException.java | 11 +++++++++++ 6 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestRuntimeException.java diff --git a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/SerenityRest.java b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/SerenityRest.java index 136d6bd436..35a678c655 100644 --- a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/SerenityRest.java +++ b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/SerenityRest.java @@ -29,6 +29,7 @@ import net.serenitybdd.rest.decorators.ResponseSpecificationDecorated; import net.serenitybdd.rest.decorators.request.RequestSpecificationDecorated; import net.serenitybdd.rest.utils.RestDecorationHelper; +import net.serenitybdd.rest.utils.RestRuntimeException; import net.serenitybdd.rest.utils.RestSpecificationFactory; import java.io.File; @@ -372,7 +373,7 @@ public static ProxySpecification proxy(final String host) { try { return setDefaultProxy(new URI(host)); } catch (URISyntaxException e) { - throw new RuntimeException("Internal error in REST Assured when constructing URI for Proxy.", e); + throw new RestRuntimeException("Internal error in REST Assured when constructing URI for Proxy.", e); } } else { return setDefaultProxy(host(host)); diff --git a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/FieldsRecordingFilter.java b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/FieldsRecordingFilter.java index d94f5cb6b0..3d86a902cf 100644 --- a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/FieldsRecordingFilter.java +++ b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/FieldsRecordingFilter.java @@ -7,6 +7,7 @@ import io.restassured.response.Response; import io.restassured.specification.FilterableRequestSpecification; import io.restassured.specification.FilterableResponseSpecification; +import net.serenitybdd.rest.utils.RestRuntimeException; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -48,9 +49,9 @@ public Response filter(final FilterableRequestSpecification requestSpec, this.recorded = this.recorded.replaceAll("\n$", ""); return response; } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Incorrect implementation, should be used correct charset", e); + throw new RestRuntimeException("Incorrect implementation, should be used correct charset", e); } catch (IOException e) { - throw new RuntimeException("Some exception during recording fields", e); + throw new RestRuntimeException("Some exception during recording fields", e); } } diff --git a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/UpdatingContextFilter.java b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/UpdatingContextFilter.java index d8731022c2..066349b747 100644 --- a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/UpdatingContextFilter.java +++ b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/filters/UpdatingContextFilter.java @@ -6,6 +6,7 @@ import io.restassured.response.Response; import io.restassured.specification.FilterableRequestSpecification; import io.restassured.specification.FilterableResponseSpecification; +import net.serenitybdd.rest.utils.RestRuntimeException; import net.serenitybdd.rest.stubs.ResponseStub; import net.serenitybdd.rest.utils.ReflectionHelper; import net.serenitybdd.rest.utils.RestExecutionHelper; @@ -39,7 +40,7 @@ public Response filter(final FilterableRequestSpecification requestSpec, if (RestExecutionHelper.restCallsAreDisabled()) { return stubbed(); } - throw new RuntimeException("Incorrect implementation, should update field without any problem", e); + throw new RestRuntimeException("Incorrect implementation, should update field without any problem", e); } } diff --git a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestReportingHelper.java b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestReportingHelper.java index e83dece182..0cff6c4236 100644 --- a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestReportingHelper.java +++ b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestReportingHelper.java @@ -27,7 +27,6 @@ import java.util.Set; import static net.thucydides.core.steps.StepEventBus.getEventBus; -import static net.thucydides.core.steps.StepEventBus.getParallelEventBus; import static org.apache.commons.lang3.ObjectUtils.firstNonNull; @@ -93,7 +92,13 @@ public void registerCall(final RestMethod method, final RequestSpecificationDeco final RuntimeException throwable, final Object... params) { RestQuery restQuery = recordRestSpecificationData(method, spec, path, params); ExecutedStepDescription description = ExecutedStepDescription.withTitle(restQuery.toString()); - StepFailure failure = new StepFailure(description, throwable); + Throwable exception; + if (throwable instanceof RestRuntimeException) { + exception = throwable.getCause(); + } else { + exception = throwable; + } + StepFailure failure = new StepFailure(description, exception); if (TestSession.isSessionStarted()) { TestSession.addEvent(new StepStartedEvent(description)); TestSession.addEvent(new RecordRestQueryEvent(restQuery)); diff --git a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestResponseRecordingHelper.java b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestResponseRecordingHelper.java index 30e7618bdd..941fcbaa20 100644 --- a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestResponseRecordingHelper.java +++ b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestResponseRecordingHelper.java @@ -56,9 +56,9 @@ public Map print(final Response response) { } } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Incorrect implementation, should be used correct charset", e); + throw new RestRuntimeException("Incorrect implementation, should be used correct charset", e); } catch (IOException e) { - throw new RuntimeException("Some exception during recording fields", e); + throw new RestRuntimeException("Some exception during recording fields", e); } } return result; diff --git a/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestRuntimeException.java b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestRuntimeException.java new file mode 100644 index 0000000000..4b473262bc --- /dev/null +++ b/serenity-rest-assured/src/main/java/net/serenitybdd/rest/utils/RestRuntimeException.java @@ -0,0 +1,11 @@ +package net.serenitybdd.rest.utils; + +public class RestRuntimeException extends RuntimeException { + public RestRuntimeException(String message) { + super(message); + } + + public RestRuntimeException(String message, Throwable cause) { + super(message, cause); + } +}