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

Exception mapping #24

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>quarkus-hibernate-orm</artifactId>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

<!-- JDBC driver dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
Expand Down Expand Up @@ -103,7 +108,7 @@
<scope>provided</scope>
<version>4.3.1</version>
</dependency>
<!-- Json logging / Enable this if you do not use tkit-json-log -->
<!-- Json logging -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-logging-json</artifactId>
Expand Down Expand Up @@ -136,8 +141,12 @@
<version>0.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>



<!--Testing -->
<dependency>
Expand Down Expand Up @@ -165,7 +174,7 @@
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -225,7 +234,6 @@
</execution>
</executions>
</plugin>

</plugins>
</build>
<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.devonfw.quarkus.general.rest.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.devonfw.quarkus.general.rest.exception;

public class InvalidParameterException extends ApplicationBusinessException {

public InvalidParameterException() {

super();
}

public InvalidParameterException(String message) {

super(message);
}

@Override
public Integer getStatusCode() {

return Integer.valueOf(422);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.devonfw.quarkus.general.rest.exception.mapper;

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

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;

/**
* Abstract super class for all specific exception mapper. To override the default ExceptionMapper of RESTEasy, own
* ExceptionMapper for specific exceptions (e.g. NotFoundException) have to be created. Just using
* ExcecptionMapper<Throwable> will not work, because the RESTEasy mappers are then more specific.
*
*
* @see <a href="https://github.com/quarkusio/quarkus/issues/7883">Quarkus Issue 7883</a>
*
*/
@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("code", errorCode);
if (this.exposeInternalErrorDetails) {
jsonMap.put("message", getExposedErrorDetails(exception));
} else {
jsonMap.put("message", exception.getMessage());
}
jsonMap.put("uri", this.uriInfo.getPath());
jsonMap.put("uuid", UUID.randomUUID());
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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.devonfw.quarkus.general.rest.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> {

@Override
public Response toResponse(NotFoundException exception) {

logError(exception);

return createResponse(Status.NOT_FOUND.getStatusCode(), exception.getClass().getSimpleName(), exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.devonfw.quarkus.general.rest.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.rest.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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.devonfw.quarkus.general.rest.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> {

@Override
public Response toResponse(UnauthorizedException exception) {

logError(exception);

return createResponse(Status.UNAUTHORIZED.getStatusCode(), exception.getClass().getSimpleName(), exception);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.devonfw.quarkus.general.rest.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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.devonfw.quarkus.productmanagement.domain.repo;

import static com.devonfw.quarkus.productmanagement.utils.StringUtils.isEmpty;
import static java.util.Objects.isNull;

import java.util.ArrayList;
Expand All @@ -9,6 +8,7 @@
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -30,7 +30,7 @@ public Page<ProductEntity> findByCriteria(ProductSearchCriteriaDto searchCriteri

QProductEntity product = QProductEntity.productEntity;
List<Predicate> predicates = new ArrayList<>();
if (!isEmpty(searchCriteria.getTitle())) {
if (!StringUtils.isEmpty(searchCriteria.getTitle())) {
predicates.add(product.title.eq(searchCriteria.getTitle()));
}
if (!isNull(searchCriteria.getPrice())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.devonfw.quarkus.productmanagement.domain.repo;

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);
Expand Down
Loading