Skip to content

Commit

Permalink
Merge pull request #35 from volodbol/controller-advice
Browse files Browse the repository at this point in the history
Handle Exception.class errors in ControllerAdvice
  • Loading branch information
yyluchkiv authored Sep 13, 2024
2 parents 62914af + 700d665 commit 9658469
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
### Changelog [v3.0.7]
— TBD
— Modification: add Exception.class errors handling
— Modification: add LOWEST_PRECEDENCE order to ControllerAdvice handler
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConversionException;
Expand All @@ -19,7 +20,9 @@

import static io.tech1.framework.foundation.utilities.exceptions.ExceptionsMessagesUtility.contactDevelopmentTeam;

// WARNING: @Order by default uses Ordered.LOWEST_PRECEDENCE
@Slf4j
@Order
@ControllerAdvice
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ResourceExceptionHandler {
Expand Down Expand Up @@ -98,4 +101,18 @@ public ResponseEntity<ExceptionEntity> badRequestExceptions(Exception ex) {
public ResponseEntity<ExceptionEntity> internalServerError(Exception ex) {
return new ResponseEntity<>(new ExceptionEntity(ex), HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler({
Exception.class
})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<ExceptionEntity> generalException(Exception ex) {
LOGGER.error("Unexpected error occurred", ex);
var response = new ExceptionEntity(
ExceptionEntityType.ERROR,
contactDevelopmentTeam("An unexpected error occurred"),
ex.getMessage()
);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

0 comments on commit 9658469

Please sign in to comment.