forked from ehrbase/ehrbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NUM-1962 - Enhance error-handling in EHRbase
- Loading branch information
Marin
committed
Jun 29, 2023
1 parent
39e4a2e
commit 46f0161
Showing
82 changed files
with
1,563 additions
and
672 deletions.
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
273 changes: 273 additions & 0 deletions
273
api/src/main/java/org/ehrbase/api/exception/CustomizedExceptionHandler.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,273 @@ | ||
package org.ehrbase.api.exception; | ||
|
||
import static java.util.Objects.nonNull; | ||
import static org.ehrbase.api.exception.ExceptionsTemplate.errorMap; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import org.ehrbase.api.exception.dto.ErrorDetails; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class CustomizedExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(BadGatewayException.class) | ||
public ResponseEntity<ErrorDetails> handleBadGatewayException( | ||
BadGatewayException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(GeneralRequestProcessingException.class) | ||
public ResponseEntity<ErrorDetails> handleGeneralRequestProcessingException( | ||
GeneralRequestProcessingException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(InternalServerException.class) | ||
public ResponseEntity<ErrorDetails> handleInternalServerException( | ||
InternalServerException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(InvalidApiParameterException.class) | ||
public ResponseEntity<ErrorDetails> handleInvalidApiParameterException( | ||
InvalidApiParameterException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(NotAcceptableException.class) | ||
public ResponseEntity<ErrorDetails> handleNotAcceptableException( | ||
NotAcceptableException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(ObjectNotFoundException.class) | ||
public ResponseEntity<ErrorDetails> handleObjectNotFoundException( | ||
ObjectNotFoundException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message("Type: [" + exception.getType() + "] " + (Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage())) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(PreconditionFailedException.class) | ||
public ResponseEntity<ErrorDetails> handlePreconditionFailedException( | ||
PreconditionFailedException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.PRECONDITION_FAILED).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(StateConflictException.class) | ||
public ResponseEntity<ErrorDetails> handleStateConflictException( | ||
StateConflictException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(UnexpectedSwitchCaseException.class) | ||
public ResponseEntity<ErrorDetails> handleUnexpectedSwitchCaseException( | ||
UnexpectedSwitchCaseException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(UnprocessableEntityException.class) | ||
public ResponseEntity<ErrorDetails> handleUnprocessableEntityException( | ||
UnprocessableEntityException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(UnsupportedMediaTypeException.class) | ||
public ResponseEntity<ErrorDetails> handleUnsupportedMediaTypeException( | ||
UnsupportedMediaTypeException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE).body( errorDetails ); | ||
} | ||
|
||
@ExceptionHandler(ValidationException.class) | ||
public ResponseEntity<ErrorDetails> handleValidationException( | ||
ValidationException exception) { | ||
|
||
var errors = Map.of( exception.getEntity().getSimpleName(), | ||
exception.getEntityId() ); | ||
var className = nonNull(exception.getEntity()) ? exception.getEntity().getSimpleName() : null; | ||
var description = exception.getMessage(); | ||
|
||
|
||
ErrorDetails errorDetails = ErrorDetails | ||
.builder() | ||
.messageId( nonNull(errorMap.get( exception.getEntityId())) ? errorMap.get( exception.getEntityId() ).getId() : -1) | ||
.argumentsList( nonNull(exception.getEntity()) ? Arrays.asList(className, description) : new ArrayList<>() ) | ||
.message(Objects.isNull(exception.getCause()) ? exception.getMessage() : exception.getCause().getMessage() ) | ||
.details( errors ) | ||
.build(); | ||
log.debug(exception.getMessage(), exception); | ||
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body( errorDetails ); | ||
} | ||
|
||
|
||
|
||
} |
41 changes: 0 additions & 41 deletions
41
api/src/main/java/org/ehrbase/api/exception/DuplicateObjectException.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.