Search in sources :

Example 1 with ErrorMessageResponse

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));
}
Also used : ErrorMessage(org.molgenis.web.ErrorMessageResponse.ErrorMessage) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse)

Example 2 with ErrorMessageResponse

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);
}
Also used : Locale(java.util.Locale) ObjectError(org.springframework.validation.ObjectError) MessageSource(org.springframework.context.MessageSource) FieldError(org.springframework.validation.FieldError) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse)

Example 3 with ErrorMessageResponse

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);
}
Also used : ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) ErrorMessage(org.molgenis.web.ErrorMessageResponse.ErrorMessage) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse)

Example 4 with ErrorMessageResponse

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);
}
Also used : ObjectError(org.springframework.validation.ObjectError) ErrorMessage(org.molgenis.web.ErrorMessageResponse.ErrorMessage) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse)

Example 5 with ErrorMessageResponse

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);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ErrorMessageResponse(org.molgenis.web.ErrorMessageResponse)

Aggregations

ErrorMessageResponse (org.molgenis.web.ErrorMessageResponse)5 ErrorMessage (org.molgenis.web.ErrorMessageResponse.ErrorMessage)3 ObjectError (org.springframework.validation.ObjectError)2 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)1 MessageSource (org.springframework.context.MessageSource)1 ResponseEntity (org.springframework.http.ResponseEntity)1 FieldError (org.springframework.validation.FieldError)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1