added new exception in global exception handler
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -132,10 +132,13 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.10.1</version> <!-- or the latest version -->
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.zalando</groupId>
|
||||||
|
<artifactId>problem-spring-web</artifactId>
|
||||||
|
<version>0.23.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package net.gepafin.tendermanagement.web.rest.api.errors;
|
||||||
|
|
||||||
|
import org.zalando.problem.AbstractThrowableProblem;
|
||||||
|
import org.zalando.problem.Status;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class BadRequestAlertException extends AbstractThrowableProblem {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public BadRequestAlertException(String defaultMessage, String entityName, String errorKey) {
|
||||||
|
this(ErrorConstants.DEFAULT_TYPE, defaultMessage, entityName, errorKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadRequestAlertException(URI type, String defaultMessage, String entityName, String errorKey) {
|
||||||
|
super(type, defaultMessage, Status.BAD_REQUEST, null, null, null, getAlertParameters(entityName, errorKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> getAlertParameters(String entityName, String errorKey) {
|
||||||
|
Map<String, Object> parameters = new HashMap<>();
|
||||||
|
parameters.put("message", "error." + errorKey);
|
||||||
|
parameters.put("params", entityName);
|
||||||
|
return parameters;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package net.gepafin.tendermanagement.web.rest.api.errors;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.FORBIDDEN)
|
||||||
|
public class ForbiddenAccessException extends RuntimeException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private final Status status;
|
||||||
|
|
||||||
|
public ForbiddenAccessException(Status status, String message) {
|
||||||
|
super(message);
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Status getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +1,37 @@
|
|||||||
package net.gepafin.tendermanagement.web.rest.api.errors;
|
package net.gepafin.tendermanagement.web.rest.api.errors;
|
||||||
|
|
||||||
|
import net.gepafin.tendermanagement.config.Translator;
|
||||||
import net.gepafin.tendermanagement.model.util.Response;
|
import net.gepafin.tendermanagement.model.util.Response;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
|
import org.springframework.security.core.AuthenticationException;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
|
import org.springframework.validation.ObjectError;
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
public final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
|
||||||
@ExceptionHandler(CustomValidationException.class)
|
@ExceptionHandler(CustomValidationException.class)
|
||||||
public ResponseEntity<Response<Void>> handleCustomValidationException(CustomValidationException ex) {
|
@ResponseBody
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
public Response<Object> handleCustomValidationException(final CustomValidationException ex) {
|
||||||
.body(new Response<>(null, ex.getStatus(), ex.getMessage()));
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
return new Response<>(null, ex.getStatus(), ex.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(ResourceNotFoundException.class)
|
@ExceptionHandler(ResourceNotFoundException.class)
|
||||||
@@ -20,4 +39,77 @@ public class GlobalExceptionHandler {
|
|||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||||
.body(new Response<>(null, ex.getStatus(), ex.getMessage()));
|
.body(new Response<>(null, ex.getStatus(), ex.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Object> handleThrowable(final Throwable ex) {
|
||||||
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
return new Response<>(null, Status.EXCEPTION_ERROR, Translator.toLocale("common_message"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
|
||||||
|
@ExceptionHandler(AuthenticationException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Object> handleInvalidTokenException(final Throwable ex) {
|
||||||
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
return new Response<>(ex.getMessage(), Status.UNAUTHORIZED, Translator.toLocale("invalid_signature"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
|
||||||
|
@ExceptionHandler(UnauthorizedAccessException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Object> unauthorizedAccessException(final Throwable ex) {
|
||||||
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
return new Response<>(null, Status.UNAUTHORIZED, ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.FORBIDDEN)
|
||||||
|
@ExceptionHandler(ForbiddenAccessException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Object> forbiddenAccessException(final Throwable ex) {
|
||||||
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
return new Response<>(null, Status.FORBIDDEN, ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Object> handleValiationException(final MethodArgumentNotValidException ex) {
|
||||||
|
|
||||||
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
final List<String> errors = new ArrayList<String>();
|
||||||
|
for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
|
||||||
|
errors.add(error.getField() + ": " + error.getDefaultMessage());
|
||||||
|
}
|
||||||
|
for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
|
||||||
|
errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
|
||||||
|
}
|
||||||
|
return new Response<>(errors, Status.VALIDATION_ERROR, Translator.toLocale("req_validation_er"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
|
||||||
|
@ExceptionHandler(BadCredentialsException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Object> handleUnAuthorizedException(final Throwable ex) {
|
||||||
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
return new Response<>(null, Status.VALIDATION_ERROR, Translator.toLocale("invalid_login"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
|
||||||
|
@ExceptionHandler(BadRequestAlertException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Response<Object> badRequestAlertException(final Throwable ex) {
|
||||||
|
log.error(ex.getMessage());
|
||||||
|
log.error(ex.getLocalizedMessage(), ex);
|
||||||
|
return new Response<>(null, Status.EXCEPTION_ERROR, ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package net.gepafin.tendermanagement.web.rest.api.errors;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
|
||||||
|
public class UnauthorizedAccessException extends RuntimeException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
private final Status status;
|
||||||
|
|
||||||
|
public UnauthorizedAccessException(Status status, String message) {
|
||||||
|
super(message);
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Status getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -48,6 +48,13 @@ login.successfully=Login successfully.
|
|||||||
pass.min.len.msg=Password must be at least 8 characters long.
|
pass.min.len.msg=Password must be at least 8 characters long.
|
||||||
email.already.exists=A user with this email already exists.
|
email.already.exists=A user with this email already exists.
|
||||||
|
|
||||||
|
#Global messages
|
||||||
|
common_message=Something went wrong..Please try again..
|
||||||
|
invalid_signature=Invalid token.
|
||||||
|
invalid_login=Invalid username or password.
|
||||||
|
req_validation_er=Request Validation Error
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -48,4 +48,8 @@ login.successfully=Accesso effettuato con successo.
|
|||||||
pass.min.len.msg=La password deve essere lunga almeno 8 caratteri.
|
pass.min.len.msg=La password deve essere lunga almeno 8 caratteri.
|
||||||
email.already.exists=Esiste gi<67> un utente con questa email.
|
email.already.exists=Esiste gi<67> un utente con questa email.
|
||||||
|
|
||||||
|
#Global messages
|
||||||
|
common_message=qualcosa é andato storto. Per favore riprova
|
||||||
|
invalid_signature=Gettone non valido.
|
||||||
|
invalid_login=Nome utente o password errati
|
||||||
|
req_validation_er=Errore di convalida
|
||||||
|
|||||||
Reference in New Issue
Block a user