Skip to content

Commit

Permalink
[Minor] use root cause in ResponseUtil.toResponse()
Browse files Browse the repository at this point in the history
  • Loading branch information
eitch committed Jul 4, 2024
1 parent 6385317 commit fd12410
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions web-rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import li.strolch.service.api.ServiceResult;
import li.strolch.utils.I18nMessage;
import li.strolch.utils.collections.Paging;
import li.strolch.utils.helper.ExceptionHelper;
import li.strolch.utils.helper.StringHelper;

import java.util.List;
Expand Down Expand Up @@ -140,7 +141,8 @@ public static Response toResponse(ServiceResult svcResult) {
if (svcResult.isOk())
return Response.ok().entity(json).type(MediaType.APPLICATION_JSON).build();

Status status = switch (t) {
Throwable rootCause = getRootCause(t);
Status status = switch (rootCause) {
case AccessDeniedException ignored -> Status.FORBIDDEN;
case PrivilegeException ignored -> Status.UNAUTHORIZED;
case StrolchElementNotFoundException ignored -> Status.NOT_FOUND;
Expand All @@ -151,12 +153,13 @@ public static Response toResponse(ServiceResult svcResult) {
}

public static Response toResponse(Throwable t) {
return switch (t) {
case StrolchNotAuthenticatedException ignored -> toResponse(Status.UNAUTHORIZED, t);
case AccessDeniedException ignored -> toResponse(Status.FORBIDDEN, t);
case StrolchElementNotFoundException ignored -> toResponse(Status.NOT_FOUND, t);
case PrivilegeException ignored -> toResponse(Status.FORBIDDEN, t);
case null, default -> toResponse(Status.INTERNAL_SERVER_ERROR, t);
Throwable rootCause = getRootCause(t);
return switch (rootCause) {
case StrolchNotAuthenticatedException ignored -> toResponse(Status.UNAUTHORIZED, rootCause);
case AccessDeniedException ignored -> toResponse(Status.FORBIDDEN, rootCause);
case StrolchElementNotFoundException ignored -> toResponse(Status.NOT_FOUND, rootCause);
case PrivilegeException ignored -> toResponse(Status.FORBIDDEN, rootCause);
case null, default -> toResponse(Status.INTERNAL_SERVER_ERROR, rootCause);
};
}

Expand Down

0 comments on commit fd12410

Please sign in to comment.