Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick status code from ErrorResponse interfaces #942

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.zalando.problem.spring.common;

import org.apiguardian.api.API;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand Down Expand Up @@ -41,11 +43,19 @@ public interface AdviceTrait {
default ThrowableProblem toProblem(final Throwable throwable) {
final StatusType status = Optional.ofNullable(resolveResponseStatus(throwable))
.<StatusType>map(ResponseStatusAdapter::new)
.or(() -> Optional.ofNullable(resolveStatusFromErrorResponse(throwable)))
.orElse(Status.INTERNAL_SERVER_ERROR);

return toProblem(throwable, status);
}

default StatusType resolveStatusFromErrorResponse(final Throwable type) {
if (!(type instanceof ErrorResponse)) return null;

final HttpStatusCode code = ((ErrorResponse) type).getStatusCode();
return Status.valueOf(code.value());
}

@API(status = MAINTAINED)
default ResponseStatus resolveResponseStatus(final Throwable type) {
@Nullable final ResponseStatus candidate = findMergedAnnotation(type.getClass(), ResponseStatus.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.zalando.problem.Problem;
import org.zalando.problem.Status;
Expand All @@ -29,6 +32,24 @@ public TestException(String message) {
}
}

class SpringExpception extends Throwable implements ErrorResponse {
final HttpStatus status;

SpringExpception(HttpStatus status) {
this.status = status;
}

@Override
public HttpStatusCode getStatusCode() {
return status;
}

@Override
public ProblemDetail getBody() {
return ProblemDetail.forStatusAndDetail(status, status.getReasonPhrase());
}
}

@ResponseStatus(value = HttpStatus.RESET_CONTENT, reason = "reason")
class TestExceptionWithReason extends RuntimeException {
public TestExceptionWithReason(String message) {
Expand Down Expand Up @@ -56,6 +77,15 @@ void buildsOnResponseStatusThrowable() {
assertThat(result.getDetail(), is("Message"));
}

@Test
void buildsOnErrorResponseThrowable() {
final Problem result = unit.toProblem(new SpringExpception(HttpStatus.NOT_FOUND));

assertThat(result.getStatus().getStatusCode(), is(404));
assertThat(result.getType().toString(), is("about:blank"));
assertThat(result.getTitle(), is("Not Found"));
}

@Test
void buildsOnResponseStatusWithReasonThrowable() {
final Problem result = unit.toProblem(new TestExceptionWithReason("Message"));
Expand Down Expand Up @@ -181,4 +211,4 @@ void processDoesNothing() {
assertThat(result.getStatusCode(), is(HttpStatus.OK));
assertThat(result.getBody().getStatus(), is(Status.RESET_CONTENT));
}
}
}
Loading