Search in sources :

Example 6 with WebRequest

use of org.springframework.web.context.request.WebRequest in project Flare-event-calendar by PollubCafe.

the class GlobalExceptionHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    BindingResult bindingResult = ex.getBindingResult();
    List<ApiFieldError> apiFieldErrors = bindingResult.getFieldErrors().stream().map(fieldError -> new ApiFieldError(fieldError.getField(), fieldError.getDefaultMessage())).collect(toList());
    ApiErrorsView apiErrorsView = new ApiErrorsView(apiFieldErrors);
    return new ResponseEntity<>(apiErrorsView, HttpStatus.UNPROCESSABLE_ENTITY);
}
Also used : RegistrationException(pl.pollub.cs.pentagoncafe.flare.exception.registration.RegistrationException) SendingEmailException(pl.pollub.cs.pentagoncafe.flare.exception.sendingEmail.SendingEmailException) ObjectNotFoundException(pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) BindingResult(org.springframework.validation.BindingResult) ApiErrorsView(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiErrorsView) TooManyLoginAttempts(pl.pollub.cs.pentagoncafe.flare.exception.auth.TooManyLoginAttempts) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) EventCalendarApplication(pl.pollub.cs.pentagoncafe.flare.EventCalendarApplication) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) Messages(pl.pollub.cs.pentagoncafe.flare.component.message.Messages) HttpHeaders(org.springframework.http.HttpHeaders) ResetPasswordException(pl.pollub.cs.pentagoncafe.flare.exception.ResetPasswordException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ApiFieldError(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiFieldError) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.apache.logging.log4j.Logger) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) LogManager(org.apache.logging.log4j.LogManager) BindingResult(org.springframework.validation.BindingResult) ResponseEntity(org.springframework.http.ResponseEntity) ApiErrorsView(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiErrorsView) ApiFieldError(pl.pollub.cs.pentagoncafe.flare.exception.handler.error.ApiFieldError)

Example 7 with WebRequest

use of org.springframework.web.context.request.WebRequest in project spring-boot by spring-projects.

the class ManagementErrorEndpointTests method errorResponseWithDefaultErrorAttributesSubclassUsingDelegation.

@Test
void errorResponseWithDefaultErrorAttributesSubclassUsingDelegation() {
    ErrorAttributes attributes = new DefaultErrorAttributes() {

        @Override
        public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
            Map<String, Object> response = super.getErrorAttributes(webRequest, options);
            response.put("error", "custom error");
            response.put("custom", "value");
            response.remove("path");
            return response;
        }
    };
    ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
    Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
    assertThat(response).containsEntry("error", "custom error");
    assertThat(response).containsEntry("custom", "value");
    assertThat(response).doesNotContainKey("path");
    assertThat(response).containsKey("timestamp");
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) WebRequest(org.springframework.web.context.request.WebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 8 with WebRequest

use of org.springframework.web.context.request.WebRequest in project spring-boot by spring-projects.

the class ManagementErrorEndpointTests method errorResponseWithCustomErrorAttributesUsingDeprecatedApi.

@Test
void errorResponseWithCustomErrorAttributesUsingDeprecatedApi() {
    ErrorAttributes attributes = new ErrorAttributes() {

        @Override
        public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
            return Collections.singletonMap("message", "An error occurred");
        }

        @Override
        public Throwable getError(WebRequest webRequest) {
            return null;
        }
    };
    ManagementErrorEndpoint endpoint = new ManagementErrorEndpoint(attributes, this.errorProperties);
    Map<String, Object> response = endpoint.invoke(new ServletWebRequest(new MockHttpServletRequest()));
    assertThat(response).containsExactly(entry("message", "An error occurred"));
}
Also used : ErrorAttributes(org.springframework.boot.web.servlet.error.ErrorAttributes) DefaultErrorAttributes(org.springframework.boot.web.servlet.error.DefaultErrorAttributes) WebRequest(org.springframework.web.context.request.WebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ErrorAttributeOptions(org.springframework.boot.web.error.ErrorAttributeOptions) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 9 with WebRequest

use of org.springframework.web.context.request.WebRequest in project rpki-validator-3 by RIPE-NCC.

the class ApiErrorHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    Locale locale = request.getLocale();
    List<ApiError> errors = ex.getBindingResult().getFieldErrors().stream().map((fieldError) -> ApiError.builder().status(String.valueOf(HttpStatus.BAD_REQUEST.value())).code(fieldError.getCode()).title(messages.getMessage("title." + fieldError.getCode(), null, HttpStatus.BAD_REQUEST.getReasonPhrase(), locale)).detail(messages.getMessage(fieldError, locale)).source(ApiErrorSource.of(Optional.of("/" + fieldError.getField().replaceAll("[.\\[]", "/").replace("]", "")), Optional.empty())).build()).collect(Collectors.toList());
    return ResponseEntity.badRequest().contentType(MediaType.valueOf(Api.API_MIME_TYPE)).body(ApiResponse.error(errors));
}
Also used : Locale(java.util.Locale) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) MethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Locale(java.util.Locale) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) MessageSource(org.springframework.context.MessageSource)

Example 10 with WebRequest

use of org.springframework.web.context.request.WebRequest in project rpki-validator-3 by RIPE-NCC.

the class ApiErrorHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    Locale locale = request.getLocale();
    List<ApiError> errors = ex.getBindingResult().getFieldErrors().stream().map((fieldError) -> ApiError.builder().status(String.valueOf(HttpStatus.BAD_REQUEST.value())).code(fieldError.getCode()).title(messages.getMessage("title." + fieldError.getCode(), null, HttpStatus.BAD_REQUEST.getReasonPhrase(), locale)).detail(messages.getMessage(fieldError, locale)).source(ApiErrorSource.of(Optional.of("/" + fieldError.getField().replaceAll("[.\\[]", "/").replace("]", "")), Optional.empty())).build()).collect(Collectors.toList());
    return ResponseEntity.badRequest().contentType(MediaType.valueOf(Api.API_MIME_TYPE)).body(ApiResponse.error(errors));
}
Also used : Locale(java.util.Locale) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) MethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Locale(java.util.Locale) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) MessageSource(org.springframework.context.MessageSource)

Aggregations

WebRequest (org.springframework.web.context.request.WebRequest)20 Test (org.testng.annotations.Test)6 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)5 List (java.util.List)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Locale (java.util.Locale)3 HttpHeaders (org.springframework.http.HttpHeaders)3 HttpStatus (org.springframework.http.HttpStatus)3 ResponseEntity (org.springframework.http.ResponseEntity)3 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)3 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)3 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.jupiter.api.Test)2 ErrorAttributeOptions (org.springframework.boot.web.error.ErrorAttributeOptions)2 DefaultErrorAttributes (org.springframework.boot.web.servlet.error.DefaultErrorAttributes)2 ErrorAttributes (org.springframework.boot.web.servlet.error.ErrorAttributes)2 MessageSource (org.springframework.context.MessageSource)2