/** * Determine the HTTP status implied by the given exception. * @param ex the exception to introspect * @return the associated HTTP status, if any * @since 5.0.5 */ @Nullable protected HttpStatus determineStatus(Throwable ex) { if (ex instanceof ResponseStatusException) { return ((ResponseStatusException) ex).getStatus(); } return null; }
private <R> Mono<R> createNotFoundError() { return Mono.defer(() -> { Exception ex = new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler"); return Mono.error(ex); }); }
@Override public String getMessage() { String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : ""); return NestedExceptionUtils.buildMessage(msg, getCause()); }
/** * Template method that handles an {@link ResponseStatusException}. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the exception. * @param ex the exception * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @return an empty ModelAndView, i.e. exception resolved * @since 5.0 */ protected ModelAndView resolveResponseStatusException(ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception { int statusCode = ex.getStatus().value(); String reason = ex.getReason(); return applyStatusAndReason(statusCode, reason, response); }
private String determineMessage(Throwable error) { if (error instanceof WebExchangeBindException) { return error.getMessage(); } if (error instanceof ResponseStatusException) { return ((ResponseStatusException) error).getReason(); } ResponseStatus responseStatus = AnnotatedElementUtils .findMergedAnnotation(error.getClass(), ResponseStatus.class); if (responseStatus != null) { return responseStatus.reason(); } return error.getMessage(); }
/** * Template method that handles an {@link ResponseStatusException}. * <p>The default implementation delegates to {@link #applyStatusAndReason} * with the status code and reason from the exception. * @param ex the exception * @param request current HTTP request * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen at the * time of the exception, e.g. if multipart resolution failed * @return an empty ModelAndView, i.e. exception resolved * @since 5.0 */ protected ModelAndView resolveResponseStatusException(ResponseStatusException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws Exception { int statusCode = ex.getStatus().value(); String reason = ex.getReason(); return applyStatusAndReason(statusCode, reason, response); }
/** * Determine the HTTP status implied by the given exception. * @param ex the exception to introspect * @return the associated HTTP status, if any * @since 5.0.5 */ @Nullable protected HttpStatus determineStatus(Throwable ex) { if (ex instanceof ResponseStatusException) { return ((ResponseStatusException) ex).getStatus(); } return null; }
@Test public void handleNestedResponseStatusException() { Throwable ex = new Exception(new ResponseStatusException(HttpStatus.BAD_REQUEST, "")); this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode()); }
@Override public String getMessage() { String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : ""); return NestedExceptionUtils.buildMessage(msg, getCause()); }
private HttpStatus determineHttpStatus(Throwable error) { if (error instanceof ResponseStatusException) { return ((ResponseStatusException) error).getStatus(); } ResponseStatus responseStatus = AnnotatedElementUtils .findMergedAnnotation(error.getClass(), ResponseStatus.class); if (responseStatus != null) { return responseStatus.code(); } return HttpStatus.INTERNAL_SERVER_ERROR; }
private Mono<ClientRegistration> findByRegistrationId(ServerWebExchange exchange, String clientRegistration) { return this.clientRegistrationRepository.findByRegistrationId(clientRegistration) .switchIfEmpty(Mono.error(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid client registration id"))); }
@Override public String getMessage() { String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : ""); return NestedExceptionUtils.buildMessage(msg, getCause()); }
@Test public void resolveWhenClientRegistrationNotFoundMatchThenBadRequest() { when(this.clientRegistrationRepository.findByRegistrationId(any())).thenReturn( Mono.empty()); ResponseStatusException expected = catchThrowableOfType(() -> resolve("/oauth2/authorization/not-found-id"), ResponseStatusException.class); assertThat(expected.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST); }
@Test // SPR-15524 public void responseStatusExceptionWithReason() throws Exception { ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, "The reason"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertResolved(mav, 400, "The reason"); }
@Override public String getMessage() { String msg = "Response status " + this.status + (this.reason != null ? " with reason \"" + reason + "\"" : ""); return NestedExceptionUtils.buildMessage(msg, getCause()); }
private void testResolvePathWithTraversal(HttpMethod httpMethod, String requestPath, Resource location) throws Exception { ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.method(httpMethod, "")); setPathWithinHandlerMapping(exchange, requestPath); StepVerifier.create(this.handler.handle(exchange)) .expectErrorSatisfies(err -> { assertThat(err, instanceOf(ResponseStatusException.class)); assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) err).getStatus()); }) .verify(TIMEOUT); if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) { fail(requestPath + " doesn't actually exist as a relative path"); } }
@Test public void handleResponseStatusException() { Throwable ex = new ResponseStatusException(HttpStatus.BAD_REQUEST, ""); this.handler.handle(this.exchange, ex).block(Duration.ofSeconds(5)); assertEquals(HttpStatus.BAD_REQUEST, this.exchange.getResponse().getStatusCode()); }
private void testInvalidPath(String requestPath, ResourceWebHandler handler) { ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); setPathWithinHandlerMapping(exchange, requestPath); StepVerifier.create(handler.handle(exchange)) .expectErrorSatisfies(err -> { assertThat(err, instanceOf(ResponseStatusException.class)); assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) err).getStatus()); }).verify(TIMEOUT); }
@Test public void responseStatusException() throws Exception { ResponseStatusException ex = new ResponseStatusException(HttpStatus.BAD_REQUEST); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertResolved(mav, 400, null); }
@Test public void directory() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); setPathWithinHandlerMapping(exchange, "js/"); StepVerifier.create(this.handler.handle(exchange)) .expectErrorSatisfies(err -> { assertThat(err, instanceOf(ResponseStatusException.class)); assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) err).getStatus()); }).verify(TIMEOUT); }