-
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 12 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
34 changes: 34 additions & 0 deletions
34
...main/java/com/devonfw/quarkus/general/service/exception/ApplicationBusinessException.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,34 @@ | ||
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; | ||
} | ||
|
||
public String getCode() { | ||
|
||
return getClass().getSimpleName(); | ||
} | ||
|
||
public Integer getStatusCode() { | ||
|
||
return null; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/devonfw/quarkus/general/service/exception/InvalidParameterException.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,20 @@ | ||
package com.devonfw.quarkus.general.service.exception; | ||
|
||
public class InvalidParameterException extends ApplicationBusinessException { | ||
|
||
public InvalidParameterException() { | ||
|
||
super(); | ||
} | ||
|
||
public InvalidParameterException(String message) { | ||
|
||
super(message); | ||
} | ||
|
||
@Override | ||
public Integer getStatusCode() { | ||
|
||
return Integer.valueOf(422); | ||
} | ||
} |
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.
Show resolved
Hide 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.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public Response toResponse(UnauthorizedException exception) { | ||
|
||
logError(exception); | ||
|
||
return createResponse(Status.UNAUTHORIZED.getStatusCode(), exception.getClass().getSimpleName(), exception); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../java/com/devonfw/quarkus/general/service/exception/mapper/ValidationExceptionMapper.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.validation.ValidationException; | ||
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 ValidationExceptionMapper extends AbstractExceptionMapper implements ExceptionMapper<ValidationException> { | ||
|
||
@Override | ||
public Response toResponse(ValidationException exception) { | ||
|
||
logError(exception); | ||
|
||
return createResponse(Status.BAD_REQUEST.getStatusCode(), exception.getClass().getSimpleName(), exception); | ||
} | ||
} |
8 changes: 5 additions & 3 deletions
8
src/main/java/com/devonfw/quarkus/productmanagement/domain/repo/ProductRepository.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 |
---|---|---|
@@ -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 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
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.