-
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.
refactor: switch to rest controller advice
- Loading branch information
1 parent
b00bdab
commit 8277175
Showing
10 changed files
with
204 additions
and
207 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...ion-server/src/main/java/dev/earlspilner/auth/rest/advice/GlobalRestExceptionHandler.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,49 @@ | ||
package dev.earlspilner.auth.rest.advice; | ||
|
||
import dev.earlspilner.auth.rest.advice.custom.BadUserCredentialsException; | ||
import dev.earlspilner.auth.rest.advice.custom.CustomJwtException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@RestControllerAdvice | ||
public class GlobalRestExceptionHandler { | ||
|
||
private static final Map<Class<? extends Exception>, HttpStatus> exceptionStatusMap = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
exceptionStatusMap.put(CustomJwtException.class, UNAUTHORIZED); | ||
exceptionStatusMap.put(BadUserCredentialsException.class, BAD_REQUEST); | ||
exceptionStatusMap.put(UsernameNotFoundException.class, NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler({ | ||
CustomJwtException.class, | ||
BadUserCredentialsException.class, | ||
UsernameNotFoundException.class, | ||
}) | ||
public ResponseEntity<ProblemDetail> handleException(Exception e) { | ||
HttpStatus status = exceptionStatusMap.getOrDefault(e.getClass(), INTERNAL_SERVER_ERROR); | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, e.getMessage()); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ProblemDetail> handleGenericException(Exception e) { | ||
HttpStatus status = INTERNAL_SERVER_ERROR; | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, "An unexpected error occurred. Please try again later."); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
} |
48 changes: 0 additions & 48 deletions
48
...ntication-server/src/main/java/dev/earlspilner/auth/rest/advice/RestControllerAdvice.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
...s-service/src/main/java/dev/earlspilner/books/rest/advice/GlobalRestExceptionHandler.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,54 @@ | ||
package dev.earlspilner.books.rest.advice; | ||
|
||
import dev.earlspilner.books.rest.advice.custom.BookExistsException; | ||
import dev.earlspilner.books.rest.advice.custom.BookNotFoundException; | ||
import dev.earlspilner.books.rest.advice.custom.UnauthorizedOperationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authorization.AuthorizationDeniedException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@RestControllerAdvice | ||
public class GlobalRestExceptionHandler { | ||
|
||
private static final Map<Class<? extends Exception>, HttpStatus> exceptionStatusMap = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
exceptionStatusMap.put(UnauthorizedOperationException.class, UNAUTHORIZED); | ||
exceptionStatusMap.put(AuthorizationDeniedException.class, FORBIDDEN); | ||
exceptionStatusMap.put(BookExistsException.class, BAD_REQUEST); | ||
exceptionStatusMap.put(BookNotFoundException.class, NOT_FOUND); | ||
exceptionStatusMap.put(IllegalArgumentException.class, BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler({ | ||
UnauthorizedOperationException.class, | ||
AuthorizationDeniedException.class, | ||
BookExistsException.class, | ||
BookNotFoundException.class, | ||
IllegalArgumentException.class, | ||
}) | ||
public ResponseEntity<ProblemDetail> handleException(Exception e) { | ||
HttpStatus status = exceptionStatusMap.getOrDefault(e.getClass(), INTERNAL_SERVER_ERROR); | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, e.getMessage()); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ProblemDetail> handleGenericException(Exception e) { | ||
HttpStatus status = INTERNAL_SERVER_ERROR; | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, "An unexpected error occurred. Please try again later."); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
} |
61 changes: 0 additions & 61 deletions
61
...i-books-service/src/main/java/dev/earlspilner/books/rest/advice/RestControllerAdvice.java
This file was deleted.
Oops, something went wrong.
5 changes: 2 additions & 3 deletions
5
library-api-gateway/src/main/java/dev/earlspilner/gateway/advice/GlobalExceptionHandler.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
48 changes: 48 additions & 0 deletions
48
...service/src/main/java/dev/earlspilner/library/rest/advice/GlobalRestExceptionHandler.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,48 @@ | ||
package dev.earlspilner.library.rest.advice; | ||
|
||
import dev.earlspilner.library.rest.advice.custom.BookRecordNotFoundException; | ||
import dev.earlspilner.library.rest.advice.custom.UnauthorizedOperationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@RestControllerAdvice | ||
public class GlobalRestExceptionHandler { | ||
|
||
private static final Map<Class<? extends Exception>, HttpStatus> exceptionStatusMap = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
exceptionStatusMap.put(UnauthorizedOperationException.class, UNAUTHORIZED); | ||
exceptionStatusMap.put(BookRecordNotFoundException.class, NOT_FOUND); | ||
exceptionStatusMap.put(IllegalArgumentException.class, BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler({ | ||
UnauthorizedOperationException.class, | ||
BookRecordNotFoundException.class, | ||
IllegalArgumentException.class | ||
}) | ||
public ResponseEntity<ProblemDetail> handleException(Exception e) { | ||
HttpStatus status = exceptionStatusMap.getOrDefault(e.getClass(), INTERNAL_SERVER_ERROR); | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, e.getMessage()); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ProblemDetail> handleGenericException(Exception e) { | ||
HttpStatus status = INTERNAL_SERVER_ERROR; | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, "An unexpected error occurred. Please try again later."); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
} |
47 changes: 0 additions & 47 deletions
47
...brary-service/src/main/java/dev/earlspilner/library/rest/advice/RestControllerAdvice.java
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
...n-service/src/main/java/dev/earlspilner/loans/rest/advice/GlobalRestExceptionHandler.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,51 @@ | ||
package dev.earlspilner.loans.rest.advice; | ||
|
||
import dev.earlspilner.loans.rest.advice.custom.LoanNotFoundException; | ||
import dev.earlspilner.loans.rest.advice.custom.UnauthorizedOperationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authorization.AuthorizationDeniedException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.springframework.http.HttpStatus.*; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@RestControllerAdvice | ||
public class GlobalRestExceptionHandler { | ||
|
||
private static final Map<Class<? extends Exception>, HttpStatus> exceptionStatusMap = new ConcurrentHashMap<>(); | ||
|
||
static { | ||
exceptionStatusMap.put(UnauthorizedOperationException.class, UNAUTHORIZED); | ||
exceptionStatusMap.put(AuthorizationDeniedException.class, FORBIDDEN); | ||
exceptionStatusMap.put(UnsupportedOperationException.class, UNPROCESSABLE_ENTITY); | ||
exceptionStatusMap.put(LoanNotFoundException.class, NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler({ | ||
UnauthorizedOperationException.class, | ||
AuthorizationDeniedException.class, | ||
UnsupportedOperationException.class, | ||
LoanNotFoundException.class | ||
}) | ||
public ResponseEntity<ProblemDetail> handleException(Exception e) { | ||
HttpStatus status = exceptionStatusMap.getOrDefault(e.getClass(), INTERNAL_SERVER_ERROR); | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, e.getMessage()); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ProblemDetail> handleGenericException(Exception e) { | ||
HttpStatus status = INTERNAL_SERVER_ERROR; | ||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, "An unexpected error occurred. Please try again later."); | ||
return ResponseEntity.status(status).body(problemDetail); | ||
} | ||
|
||
} |
Oops, something went wrong.