-
Notifications
You must be signed in to change notification settings - Fork 20
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
Exception mapping #24
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 4 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 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 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
24 changes: 24 additions & 0 deletions
24
...main/java/com/devonfw/quarkus/general/service/exception/ApplicationBusinessException.java
This file contains 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,24 @@ | ||
package com.devonfw.quarkus.general.service.exception; | ||
|
||
public abstract class ApplicationBusinessException extends RuntimeException { | ||
|
||
public ApplicationBusinessException() { | ||
|
||
super(); | ||
} | ||
|
||
public ApplicationBusinessException(String message) { | ||
|
||
super(message); | ||
} | ||
|
||
public ApplicationBusinessException(String message, Exception e) { | ||
|
||
super(message, e); | ||
} | ||
|
||
public boolean isTechnical() { | ||
|
||
return false; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/devonfw/quarkus/general/service/exception/InvalidParameterException.java
This file contains 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,14 @@ | ||
package com.devonfw.quarkus.general.service.exception; | ||
|
||
public class InvalidParameterException extends ApplicationBusinessException { | ||
|
||
public InvalidParameterException() { | ||
|
||
super(); | ||
} | ||
|
||
public InvalidParameterException(String message) { | ||
|
||
super(message); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/devonfw/quarkus/general/service/exception/RestServiceExceptionFacade.java
This file contains 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,93 @@ | ||
package com.devonfw.quarkus.general.service.exception; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.Status; | ||
import javax.ws.rs.core.UriInfo; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Provider | ||
@Slf4j | ||
public class RestServiceExceptionFacade implements ExceptionMapper<RuntimeException> { | ||
|
||
private boolean exposeInternalErrorDetails = false; | ||
|
||
@Context | ||
UriInfo uriInfo; | ||
|
||
@Override | ||
public Response toResponse(RuntimeException exception) { | ||
|
||
log.error("Exception:{},URL:{},ERROR:{}", exception.getClass().getCanonicalName(), this.uriInfo.getRequestUri(), | ||
exception.getMessage()); | ||
|
||
if (exception instanceof ApplicationBusinessException) { | ||
return createResponse((ApplicationBusinessException) exception); | ||
} | ||
if (exception instanceof WebApplicationException) { | ||
return createResponse((WebApplicationException) exception); | ||
} | ||
return createResponse(exception); | ||
} | ||
|
||
private Response createResponse(ApplicationBusinessException exception) { | ||
|
||
return createResponse(Status.BAD_REQUEST, Error.APPLICATION_BUSINESS_EXCEPTION, exception); | ||
} | ||
|
||
private Response createResponse(WebApplicationException exception) { | ||
|
||
Status status = Status.fromStatusCode(exception.getResponse().getStatus()); | ||
return createResponse(status, Error.WEB_APPLICATION_EXCEPTION, exception); | ||
} | ||
|
||
private Response createResponse(Exception exception) { | ||
|
||
return createResponse(Status.INTERNAL_SERVER_ERROR, Error.UNDEFINED_ERROR_CODE, exception); | ||
} | ||
|
||
private Response createResponse(Status status, Error errorCode, Exception exception) { | ||
|
||
Map<String, Object> jsonMap = new HashMap<>(); | ||
jsonMap.put("errorCode", errorCode.name()); | ||
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(); | ||
} | ||
|
||
private 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(); | ||
} | ||
|
||
public enum Error { | ||
|
||
APPLICATION_BUSINESS_EXCEPTION, WEB_APPLICATION_EXCEPTION, UNDEFINED_ERROR_CODE; | ||
} | ||
GuentherJulian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} |
8 changes: 5 additions & 3 deletions
8
src/main/java/com/devonfw/quarkus/productmanagement/domain/repo/ProductRepository.java
This file contains 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,16 +1,18 @@ | ||
package com.devonfw.quarkus.productmanagement.domain.repo; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import com.devonfw.quarkus.productmanagement.domain.model.ProductEntity; | ||
|
||
public interface ProductRepository extends CrudRepository<ProductEntity, Long>, ProductFragment { | ||
public interface ProductRepository extends JpaRepository<ProductEntity, Long>, ProductFragment { | ||
|
||
@Query("select a from ProductEntity a where title = :title") | ||
ProductEntity findByTitle(@Param("title") String title); | ||
Optional<ProductEntity> findByTitle(@Param("title") String title); | ||
|
||
Page<ProductEntity> findAllByOrderByTitle(); | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else?
also we should add support for JAX-RS Exceptions (that we have been lacking in devon4j) with e.g. NotFoundException procuding 404.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Quarkus RESTEasy extension provides a mapper for
NotFoundException
out of the box, that produces a 404 response (https://github.com/quarkusio/quarkus/blob/main/extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/NotFoundExceptionMapper.java).Should we anyway implement and use an own mapper for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that already plays together with out exception mapper, it is fully sufficient. But then you could add a nice inline comment to the code giving this valuable information including that link so people can easily understand what is going on. I also was not aware of that so thanks for making this clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But comming back to my original concern:
How about permission denied? How about validation failures (from BV)?
We do not want to report this as 500 (internal server error)!
This is IMHO only complete if we have all these cases covered.
Ideally we even add a JUnit that is testing all these scenarios to make them transparent and to document our features and behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the Quarkus ExceptionMapper implementations do not return any further information (only status code) I decided to create own implementations. So now we have exception mapper for NotFoundException, UnauthorizedException and ValidationException + our ApplicationBusinessException. Other exceptions are handled as WebApplicationException or as general Exception with status 500
@hohwille is this what you had in mind?