use of org.molgenis.web.ErrorMessageResponse in project molgenis by molgenis.
the class RestController method handleAuthenticationException.
@ExceptionHandler(AuthenticationException.class)
@ResponseStatus(UNAUTHORIZED)
public ErrorMessageResponse handleAuthenticationException(AuthenticationException e) {
LOG.info("", e);
// workaround for https://github.com/molgenis/molgenis/issues/4441
String message = e.getMessage();
String messagePrefix = "org.springframework.security.core.userdetails.UsernameNotFoundException: ";
if (message.startsWith(messagePrefix)) {
message = message.substring(messagePrefix.length());
}
return new ErrorMessageResponse(new ErrorMessage(message));
}
use of org.molgenis.web.ErrorMessageResponse in project molgenis by molgenis.
the class ExceptionHandlerUtils method getErrorMessageResponse.
private static ErrorMessageResponse getErrorMessageResponse(Errors errors) {
List<ErrorMessageResponse.ErrorMessage> errorMessages = newArrayList();
Locale locale = LocaleContextHolder.getLocale();
MessageSource messageSource = MessageSourceHolder.getMessageSource();
for (ObjectError objectError : errors.getGlobalErrors()) {
String message = messageSource.getMessage("org.molgenis.web.exception.ObjectError", new Object[] { objectError.getObjectName(), objectError }, locale);
errorMessages.add(new ErrorMessageResponse.ErrorMessage(message, objectError.getCode()));
}
for (FieldError fieldError : errors.getFieldErrors()) {
String message = messageSource.getMessage("org.molgenis.web.exception.FieldError", new Object[] { fieldError.getField(), fieldError.getObjectName(), fieldError }, locale);
errorMessages.add(new ErrorMessageResponse.ErrorMessage(message, fieldError.getCode()));
}
return new ErrorMessageResponse(errorMessages);
}
use of org.molgenis.web.ErrorMessageResponse in project molgenis by molgenis.
the class RestController method handleMolgenisValidationException.
@ExceptionHandler(MolgenisValidationException.class)
@ResponseStatus(BAD_REQUEST)
public ErrorMessageResponse handleMolgenisValidationException(MolgenisValidationException e) {
LOG.info("", e);
List<ErrorMessage> messages = Lists.newArrayList();
for (ConstraintViolation violation : e.getViolations()) {
messages.add(new ErrorMessage(violation.getMessage()));
}
return new ErrorMessageResponse(messages);
}
use of org.molgenis.web.ErrorMessageResponse in project molgenis by molgenis.
the class RestController method handleMethodArgumentNotValidException.
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(BAD_REQUEST)
public ErrorMessageResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
LOG.debug("", e);
List<ErrorMessage> messages = Lists.newArrayList();
for (ObjectError error : e.getBindingResult().getAllErrors()) {
messages.add(new ErrorMessage(error.getDefaultMessage()));
}
return new ErrorMessageResponse(messages);
}
use of org.molgenis.web.ErrorMessageResponse in project molgenis by molgenis.
the class ExceptionHandlerUtils method handleException.
private static Object handleException(Exception e, boolean isHtmlRequest, HttpStatus httpStatus, String errorCode, String environment) {
LOG.info("", e);
ErrorMessageResponse errorMessageResponse = createErrorMessageResponse(e, errorCode);
if (isHtmlRequest) {
Map<String, Object> model = new HashMap<>();
model.put(KEY_ERROR_MESSAGE_RESPONSE, errorMessageResponse);
model.put(HTTP_STATUS_CODE, httpStatus.value());
if (environment.equals(DEVELOPMENT)) {
model.put(STACK_TRACE, e.getStackTrace());
}
return new ModelAndView(VIEW_EXCEPTION, model, httpStatus);
} else {
return new ResponseEntity<>(errorMessageResponse, httpStatus);
}
}
Aggregations