Add unauthorized exception.

This commit is contained in:
Konstantinos Triantafyllou 2024-05-22 10:57:54 +03:00
parent ecbf34057b
commit b1c115ac80
2 changed files with 30 additions and 8 deletions

View File

@ -24,7 +24,7 @@ public class AdminToolsLibraryExceptionsHandler {
response.setStatus(HttpStatus.BAD_REQUEST);
log.error("invalidInput exception");
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(MismatchingContentException.class)
@ -35,7 +35,7 @@ public class AdminToolsLibraryExceptionsHandler {
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.BAD_REQUEST);
log.error("mismatchingContent exception: " + response.getErrorCode()+ " "+response.getErrorMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(ContentNotFoundException.class)
@ -46,7 +46,7 @@ public class AdminToolsLibraryExceptionsHandler {
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.NOT_FOUND);
log.error("contentNotFound exception: " + response.getErrorCode()+ " - "+response.getErrorMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(NullPointerException.class)
@ -57,7 +57,7 @@ public class AdminToolsLibraryExceptionsHandler {
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.BAD_REQUEST);
log.error("nullPointerException exception" + response.getErrorCode()+ " - "+response.getErrorMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(InvalidReCaptchaException.class)
@ -68,7 +68,7 @@ public class AdminToolsLibraryExceptionsHandler {
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.BAD_REQUEST);
log.error("invalidReCaptchaException exception");
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
@ -80,7 +80,18 @@ public class AdminToolsLibraryExceptionsHandler {
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.NOT_FOUND);
log.error("notFoundException exception : "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.NOT_FOUND);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<ExceptionResponse> unauthorizedException(Exception ex) {
ExceptionResponse response = new ExceptionResponse();
response.setErrorCode("Unauthorized Exception");
response.setErrorMessage("Unauthorized Exception");
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.UNAUTHORIZED);
log.error("Unauthorized exception: "+ ex.getMessage());
return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(ForbiddenException.class)
@ -91,7 +102,7 @@ public class AdminToolsLibraryExceptionsHandler {
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.FORBIDDEN);
log.error("forbiddenException exception: "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.FORBIDDEN);
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
}
@ExceptionHandler(DuplicateKeyException.class)
@ -102,6 +113,6 @@ public class AdminToolsLibraryExceptionsHandler {
response.setErrors(ex.getMessage());
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
log.error("duplicateKeyException exception: "+ ex.getMessage());
return new ResponseEntity<ExceptionResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@ -0,0 +1,11 @@
package eu.dnetlib.uoaadmintoolslibrary.handlers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public class UnauthorizedException extends RuntimeException {
public UnauthorizedException(String message){
super(message);
}
}