use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.
the class ExceptionControllerAdvice method handle.
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ResultModels> handle(AccessDeniedException ex) {
ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.FORBIDDEN, ex.getMessage());
LOG.warn("[" + errorModel.getId() + "] ", ex);
return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.
the class ExceptionControllerAdvice method handle.
@ExceptionHandler(IdmAuthenticationException.class)
public ResponseEntity<ResultModels> handle(IdmAuthenticationException ex) {
ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.AUTH_FAILED);
// source exception message is shown only in log
LOG.warn("[" + errorModel.getId() + "] ", ex);
return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.
the class ExceptionControllerAdvice method handle.
@ExceptionHandler(PersistenceException.class)
public ResponseEntity<ResultModels> handle(PersistenceException ex) {
ErrorModel errorModel = null;
//
if (ex.getCause() != null && ex.getCause() instanceof ConstraintViolationException) {
ConstraintViolationException constraintEx = (ConstraintViolationException) ex.getCause();
// TODO: registrable contstrain error codes
if (constraintEx.getConstraintName().contains("name")) {
errorModel = new DefaultErrorModel(CoreResultCode.NAME_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
} else if (constraintEx.getConstraintName().contains("code")) {
errorModel = new DefaultErrorModel(CoreResultCode.CODE_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
} else {
errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
}
} else {
errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ex.getMessage());
}
LOG.error("[" + errorModel.getId() + "] ", ex);
return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.
the class ExceptionControllerAdvice method handleError.
/**
* Handle exceptions from http (~ authentication) filters.
*
* @see RestErrorAttributes
* @param request
* @param response
* @return error models
* @since 10.7.0
*/
@RequestMapping(path = { "/error" })
public ResponseEntity<?> handleError(HttpServletRequest request, HttpServletResponse response) {
Object exception = request.getAttribute(javax.servlet.RequestDispatcher.ERROR_EXCEPTION);
//
if (exception == null || !(exception instanceof Throwable)) {
// if source exception is not set (e.g. from controller security ~ forbidden), return base error attributes.
RestErrorAttributes attributes = new RestErrorAttributes();
attributes.resolveException(request, response, null, null);
Map<String, Object> errorAttributes = attributes.getErrorAttributes(new ServletWebRequest(request, response), false);
//
// fill http status
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
if (errorAttributes.containsKey(RestErrorAttributes.ATTRIBUTE_STATUS)) {
int statusCode = (int) errorAttributes.get(RestErrorAttributes.ATTRIBUTE_STATUS);
httpStatus = HttpStatus.valueOf(statusCode);
}
// move error as IdM errors
if (errorAttributes.containsKey(RestErrorAttributes.ATTRIBUTE_ERROR)) {
errorAttributes.put(ResultModels.ATTRIBUTE_ERRORS, Lists.newArrayList(errorAttributes.get(RestErrorAttributes.ATTRIBUTE_ERROR)));
errorAttributes.remove(RestErrorAttributes.ATTRIBUTE_ERROR);
}
// Return original error attributes => to cover all cases, when even "unknown" exception can be thrown (without our error).
return new ResponseEntity<>(errorAttributes, createHttpHeaders(request, response), httpStatus);
}
//
Throwable cause = Throwables.getRootCause((Throwable) exception);
HttpHeaders httpHeaders = createHttpHeaders(request, response);
//
if (cause instanceof ResultCodeException) {
ResultCodeException resultCodeException = (ResultCodeException) cause;
ExceptionUtils.log(LOG, resultCodeException);
//
return new ResponseEntity<>(resultCodeException.getError(), httpHeaders, resultCodeException.getStatus());
}
//
ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.INTERNAL_SERVER_ERROR, cause.getMessage());
ExceptionUtils.log(LOG, errorModel, cause);
//
return new ResponseEntity<>(new ResultModels(errorModel), httpHeaders, errorModel.getStatus());
}
use of eu.bcvsolutions.idm.core.api.exception.ErrorModel in project CzechIdMng by bcvsolutions.
the class ExceptionControllerAdvice method handle.
/**
* Upload file size exceeded.
*
* @param ex handled exception
* @return error model
* @since 10.2.0
*/
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<ResultModels> handle(MaxUploadSizeExceededException ex) {
long maxUploadSize = ex.getMaxUploadSize();
//
String effectiveMaxUploadSize;
if (maxUploadSize <= 0) {
effectiveMaxUploadSize = configurationService.getValue("spring.servlet.multipart.max-file-size", // -1 as default
String.valueOf(maxUploadSize));
} else {
effectiveMaxUploadSize = FileUtils.byteCountToDisplaySize(maxUploadSize);
}
//
ErrorModel errorModel = new DefaultErrorModel(CoreResultCode.ATTACHMENT_SIZE_LIMIT_EXCEEDED, ex.getMessage(), ImmutableMap.of("actualSize", effectiveMaxUploadSize));
LOG.warn("[" + errorModel.getId() + "] ", ex);
return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
Aggregations