Skip to content

Commit

Permalink
refactor: switch to rest controller advice
Browse files Browse the repository at this point in the history
  • Loading branch information
thisdudkin committed Sep 25, 2024
1 parent b00bdab commit 8277175
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 207 deletions.
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);
}

}

This file was deleted.

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);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dev.earlspilner.gateway.advice;

import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import io.jsonwebtoken.ExpiredJwtException;
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 org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

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

}

This file was deleted.

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);
}

}
Loading

0 comments on commit 8277175

Please sign in to comment.