-
Notifications
You must be signed in to change notification settings - Fork 20
Exception mapping #24
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
Open
GuentherJulian
wants to merge
19
commits into
devonfw-sample:master
Choose a base branch
from
GuentherJulian:exception_mapping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
befa7a8
added exception mapper
GuentherJulian 02f96a7
adapted tests
GuentherJulian 64565ae
changed ApplicationPersistenceEntity
GuentherJulian f7e0fb9
changed test resources
GuentherJulian af097e4
minor changes for exception mapping
GuentherJulian 230b014
added NotFoundException to delete method
GuentherJulian b22857a
Merge branch 'master' into exception_mapping
GuentherJulian 3d5902d
Merge branch 'master' into exception_mapping
GuentherJulian 6e8b692
Merge branch 'master' into exception_mapping
GuentherJulian 16b1d0f
merge master
GuentherJulian 6f29056
added exceptionmapper for validation errors, unauthorized errors and …
GuentherJulian 7fc8787
changed visibility in AbstractExceptionMapper to protected
GuentherJulian 6fca128
added uuid
GuentherJulian 4ad6fd4
added javadoc to AbstractExceptionMapper
GuentherJulian 64a697e
added javadoc to AbstractExceptionMapper
GuentherJulian 9aa5a08
Merge branch 'master' into exception_mapping
GuentherJulian 7931e06
renamed package from service to rest
GuentherJulian f77c61c
Merge remote-tracking branch 'upstream/master' into exception_mapping
GuentherJulian 36bd04c
fixed tests
GuentherJulian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Handle line endings automatically for files detected as text | ||
| # and leave all files detected as binary untouched. | ||
| * text eol=lf | ||
| *.bat eol=crlf | ||
|
|
||
| # | ||
| # The above will handle all files NOT found below | ||
| # | ||
| # These files are text and should be normalized (Convert crlf => lf) | ||
| *.asciidoc text | ||
| *.adoc text | ||
| *.md text | ||
| *.css text | ||
| *.df text | ||
| *.htm text | ||
| *.html text | ||
| *.java text | ||
| *.js text | ||
| *.json text | ||
| *.jsp text | ||
| *.jspf text | ||
| *.jspx text | ||
| *.properties text | ||
| *.sh text | ||
| *.tld text | ||
| *.txt text | ||
| *.tag text | ||
| *.tagx text | ||
| *.xml text | ||
| *.yml text | ||
| *.ftl text | ||
|
|
||
| # These files are binary and should be left untouched | ||
| # (binary is a macro for -text -diff) | ||
| *.class binary | ||
| *.dll binary | ||
| *.ear binary | ||
| *.gif binary | ||
| *.ico binary | ||
| *.jar binary | ||
| *.jpg binary | ||
| *.jpeg binary | ||
| *.png binary | ||
| *.so binary | ||
| *.war binary | ||
| *.pdf binary | ||
| *.exe binary | ||
| *.zip binary |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| .* | ||
| !.github | ||
| !.gitignore | ||
| !.gitattributes | ||
| !.mvn | ||
| *.bak | ||
| *.log | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 0 additions & 90 deletions
90
src/main/java/com/devonfw/quarkus/general/service/exception/RestServiceExceptionFacade.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
...in/java/com/devonfw/quarkus/general/service/exception/mapper/AbstractExceptionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.devonfw.quarkus.general.service.exception.mapper; | ||
|
|
||
| import java.time.ZonedDateTime; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import javax.ws.rs.core.Context; | ||
| import javax.ws.rs.core.MediaType; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.core.UriInfo; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| public abstract class AbstractExceptionMapper { | ||
|
|
||
| protected boolean exposeInternalErrorDetails = false; | ||
|
|
||
| @Context | ||
| UriInfo uriInfo; | ||
|
|
||
| protected void logError(Exception exception) { | ||
|
|
||
| log.error("Exception:{},URL:{},ERROR:{}", exception.getClass().getCanonicalName(), this.uriInfo.getRequestUri(), | ||
| exception.getMessage()); | ||
| } | ||
|
|
||
| protected Response createResponse(int status, String errorCode, Exception exception) { | ||
|
|
||
| Map<String, Object> jsonMap = new HashMap<>(); | ||
| jsonMap.put("errorCode", errorCode); | ||
| if (this.exposeInternalErrorDetails) { | ||
| jsonMap.put("message", getExposedErrorDetails(exception)); | ||
| } else { | ||
| jsonMap.put("message", exception.getMessage()); | ||
| } | ||
| jsonMap.put("uri", this.uriInfo.getPath()); | ||
| jsonMap.put("timestamp", ZonedDateTime.now().toString()); | ||
| return Response.status(status).type(MediaType.APPLICATION_JSON).entity(jsonMap).build(); | ||
| } | ||
|
|
||
| protected String getExposedErrorDetails(Throwable error) { | ||
|
|
||
| StringBuilder buffer = new StringBuilder(); | ||
| Throwable e = error; | ||
| while (e != null) { | ||
| if (buffer.length() > 0) { | ||
| buffer.append(System.lineSeparator()); | ||
| } | ||
| buffer.append(e.getClass().getSimpleName()); | ||
| buffer.append(": "); | ||
| buffer.append(e.getLocalizedMessage()); | ||
| e = e.getCause(); | ||
| } | ||
| return buffer.toString(); | ||
| } | ||
|
|
||
| } | ||
19 changes: 19 additions & 0 deletions
19
...in/java/com/devonfw/quarkus/general/service/exception/mapper/NotFoundExceptionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.devonfw.quarkus.general.service.exception.mapper; | ||
|
|
||
| import javax.ws.rs.NotFoundException; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.core.Response.Status; | ||
| import javax.ws.rs.ext.ExceptionMapper; | ||
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| @Provider | ||
| public class NotFoundExceptionMapper extends AbstractExceptionMapper implements ExceptionMapper<NotFoundException> { | ||
|
maybeec marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public Response toResponse(NotFoundException exception) { | ||
|
|
||
| logError(exception); | ||
|
|
||
| return createResponse(Status.NOT_FOUND.getStatusCode(), exception.getClass().getSimpleName(), exception); | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
...ain/java/com/devonfw/quarkus/general/service/exception/mapper/RuntimeExceptionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.devonfw.quarkus.general.service.exception.mapper; | ||
|
|
||
| import javax.ws.rs.WebApplicationException; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.core.Response.Status; | ||
| import javax.ws.rs.ext.ExceptionMapper; | ||
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| import com.devonfw.quarkus.general.service.exception.ApplicationBusinessException; | ||
|
|
||
| @Provider | ||
| public class RuntimeExceptionMapper extends AbstractExceptionMapper implements ExceptionMapper<RuntimeException> { | ||
|
|
||
| @Override | ||
| public Response toResponse(RuntimeException exception) { | ||
|
|
||
| logError(exception); | ||
|
|
||
| if (exception instanceof ApplicationBusinessException) { | ||
| return createResponse((ApplicationBusinessException) exception); | ||
| } else if (exception instanceof WebApplicationException) { | ||
| return createResponse((WebApplicationException) exception); | ||
| } | ||
|
|
||
| return createResponse(exception); | ||
| } | ||
|
|
||
| private Response createResponse(ApplicationBusinessException exception) { | ||
|
|
||
| int status = exception.getStatusCode() == null ? Status.BAD_REQUEST.getStatusCode() : exception.getStatusCode(); | ||
| return createResponse(status, exception.getCode(), exception); | ||
| } | ||
|
|
||
| private Response createResponse(WebApplicationException exception) { | ||
|
|
||
| Status status = Status.fromStatusCode(exception.getResponse().getStatus()); | ||
| return createResponse(status.getStatusCode(), exception.getClass().getSimpleName(), exception); | ||
| } | ||
|
|
||
| private Response createResponse(Exception exception) { | ||
|
|
||
| return createResponse(Status.INTERNAL_SERVER_ERROR.getStatusCode(), exception.getClass().getSimpleName(), | ||
| exception); | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
...ava/com/devonfw/quarkus/general/service/exception/mapper/UnauthorizedExceptionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.devonfw.quarkus.general.service.exception.mapper; | ||
|
|
||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.core.Response.Status; | ||
| import javax.ws.rs.ext.ExceptionMapper; | ||
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| import io.quarkus.security.UnauthorizedException; | ||
|
|
||
| @Provider | ||
| public class UnauthorizedExceptionMapper extends AbstractExceptionMapper | ||
| implements ExceptionMapper<UnauthorizedException> { | ||
|
maybeec marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| public Response toResponse(UnauthorizedException exception) { | ||
|
|
||
| logError(exception); | ||
|
|
||
| return createResponse(Status.UNAUTHORIZED.getStatusCode(), exception.getClass().getSimpleName(), exception); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.