update security module
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package sydney.cheng.microservice.commons.exceptions.feign;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class FeignBadRequestException extends RuntimeException {
|
||||
private Map<String, String> errors;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package sydney.cheng.microservice.commons.exceptions.feign;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
public class FeignErrorException extends RuntimeException {
|
||||
private final String message;
|
||||
private final HttpStatus httpStatus;
|
||||
|
||||
public FeignErrorException(String message, HttpStatus httpStatus) {
|
||||
super(message);
|
||||
this.message = message;
|
||||
this.httpStatus = httpStatus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package sydney.cheng.microservice.commons.exceptions.handlers;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
import sydney.cheng.microservice.commons.exceptions.auth.WrongCredentialsException;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class AuthExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(WrongCredentialsException.class)
|
||||
public ResponseEntity<?> usernameOrPasswordInvalidException(WrongCredentialsException exception) {
|
||||
Map<String, String> errors = new HashMap<>();
|
||||
errors.put("error", exception.getMessage());
|
||||
return new ResponseEntity<>(errors, HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package sydney.cheng.microservice.commons.exceptions.handlers;
|
||||
|
||||
import sydney.cheng.microservice.commons.exceptions.feign.*;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class FeignExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(FeignErrorException.class)
|
||||
public ResponseEntity<?> genericError(FeignErrorException exception) {
|
||||
Map<String, String> errors = new HashMap<>();
|
||||
errors.put("error", exception.getMessage());
|
||||
return new ResponseEntity<>(errors, exception.getHttpStatus());
|
||||
}
|
||||
|
||||
@ExceptionHandler(FeignBadRequestException.class)
|
||||
public ResponseEntity<?> validationException(FeignBadRequestException exception) {
|
||||
return ResponseEntity.badRequest().body(exception.getErrors());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package sydney.cheng.microservice.commons.exceptions.handlers;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GenericExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public final ResponseEntity<?> handleAllException(Exception ex) {
|
||||
Map<String, String> errors = new HashMap<>();
|
||||
errors.put("error", ex.getMessage());
|
||||
return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user