/** * 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; }
/** * 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; }
/** * 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 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; }
/** * 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); }
@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); }
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"); } }
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 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); }
@Test public void directoryInJarFile() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); setPathWithinHandlerMapping(exchange, "underscorejs/"); StepVerifier.create(this.handler.handle(exchange)) .expectErrorSatisfies(err -> { assertThat(err, instanceOf(ResponseStatusException.class)); assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) err).getStatus()); }).verify(TIMEOUT); }
@Test public void missingResourcePath() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); setPathWithinHandlerMapping(exchange, ""); StepVerifier.create(this.handler.handle(exchange)) .expectErrorSatisfies(err -> { assertThat(err, instanceOf(ResponseStatusException.class)); assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) err).getStatus()); }).verify(TIMEOUT); }
private CustomStatusResponseDecorator(Throwable ex, ServerHttpResponse delegate) { super(delegate); this.status = (ex instanceof ResponseStatusException) ? ((ResponseStatusException) ex).getStatus() : HttpStatus.INTERNAL_SERVER_ERROR; }
private void resourceNotFound(HttpMethod httpMethod) { MockServerHttpRequest request = MockServerHttpRequest.method(httpMethod, "").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); setPathWithinHandlerMapping(exchange, "not-there.css"); Mono<Void> mono = this.handler.handle(exchange); StepVerifier.create(mono) .expectErrorSatisfies(err -> { assertThat(err, instanceOf(ResponseStatusException.class)); assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) err).getStatus()); }).verify(TIMEOUT); // SPR-17475 AtomicReference<Throwable> exceptionRef = new AtomicReference<>(); StepVerifier.create(mono).consumeErrorWith(exceptionRef::set).verify(); StepVerifier.create(mono).consumeErrorWith(ex -> assertNotSame(exceptionRef.get(), ex)).verify(); }
SofaStatusResponseDecorator(Throwable throwable, ServerHttpResponse delegate) { super(delegate); this.status = throwable instanceof ResponseStatusException ? ((ResponseStatusException) throwable) .getStatus() : HttpStatus.INTERNAL_SERVER_ERROR; }
/** * 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; }
/** * 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; }
@API(status = INTERNAL) @ExceptionHandler default Mono<ResponseEntity<Problem>> handleResponseStatusException( final ResponseStatusException exception, final ServerWebExchange request) { return create(exception.getStatus(), exception, request); }