pull/12527/head
Phillip Webb 7 years ago
parent f80db03e75
commit 620208a802

@ -103,7 +103,8 @@ public class ErrorProperties {
public static class Whitelabel {
/**
* Whether to enable the default error page displayed in browsers in case of a server error.
* Whether to enable the default error page displayed in browsers in case of a
* server error.
*/
private boolean enabled = true;
@ -114,6 +115,7 @@ public class ErrorProperties {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
}

@ -119,20 +119,20 @@ public class DefaultErrorWebExceptionHandler extends AbstractErrorWebExceptionHa
boolean includeStackTrace = isIncludeStackTrace(request, MediaType.TEXT_HTML);
Map<String, Object> error = getErrorAttributes(request, includeStackTrace);
HttpStatus errorStatus = getHttpStatus(error);
ServerResponse.BodyBuilder response = ServerResponse.status(errorStatus)
ServerResponse.BodyBuilder responseBody = ServerResponse.status(errorStatus)
.contentType(MediaType.TEXT_HTML);
Flux<ServerResponse> result = Flux
.just("error/" + errorStatus.toString(),
"error/" + SERIES_VIEWS.get(errorStatus.series()), "error/error")
.flatMap((viewName) -> renderErrorView(viewName, response, error));
.flatMap((viewName) -> renderErrorView(viewName, responseBody, error));
if (this.errorProperties.getWhitelabel().isEnabled()) {
result = result.switchIfEmpty(renderDefaultErrorView(response, error));
result = result.switchIfEmpty(renderDefaultErrorView(responseBody, error));
}
else {
Throwable ex = getError(request);
result = result.switchIfEmpty(Mono.error(ex));
}
return result.next().doOnNext((resp) -> logError(request, errorStatus));
return result.next().doOnNext((response) -> logError(request, errorStatus));
}
/**

@ -53,7 +53,7 @@ public class CouchbaseAutoConfigurationTests {
@Test
public void bootstrapHostsIsRequired() {
this.contextRunner.run((context) -> assertNoCouchbaseBeans(context));
this.contextRunner.run(this::assertNoCouchbaseBeans);
}
@Test

@ -95,10 +95,8 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
this.contextRunner.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
client.get().uri("/notFound").exchange()
.expectStatus().isNotFound()
.expectBody().jsonPath("status")
.isEqualTo("404").jsonPath("error")
client.get().uri("/notFound").exchange().expectStatus().isNotFound()
.expectBody().jsonPath("status").isEqualTo("404").jsonPath("error")
.isEqualTo(HttpStatus.NOT_FOUND.getReasonPhrase()).jsonPath("path")
.isEqualTo(("/notFound")).jsonPath("exception").doesNotExist();
});
@ -125,10 +123,8 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
client.post().uri("/bind").contentType(MediaType.APPLICATION_JSON)
.syncBody("{}").exchange()
.expectStatus().isBadRequest()
.expectBody().jsonPath("status")
.isEqualTo("400").jsonPath("error")
.syncBody("{}").exchange().expectStatus().isBadRequest().expectBody()
.jsonPath("status").isEqualTo("400").jsonPath("error")
.isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase()).jsonPath("path")
.isEqualTo(("/bind")).jsonPath("exception").doesNotExist()
.jsonPath("errors").isArray().jsonPath("message").isNotEmpty();
@ -195,10 +191,9 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
client.get().uri("/badRequest").exchange()
.expectStatus().isBadRequest()
.expectBody().jsonPath("status").isEqualTo("400")
.jsonPath("error")
client.get().uri("/badRequest").exchange().expectStatus()
.isBadRequest().expectBody().jsonPath("status")
.isEqualTo("400").jsonPath("error")
.isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase())
.jsonPath("exception")
.isEqualTo(ResponseStatusException.class.getName());
@ -254,9 +249,8 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
String body = client.get().uri("/notfound")
.accept(MediaType.TEXT_HTML).exchange()
.expectStatus().isNotFound()
.expectHeader().contentType(MediaType.TEXT_HTML)
.accept(MediaType.TEXT_HTML).exchange().expectStatus()
.isNotFound().expectHeader().contentType(MediaType.TEXT_HTML)
.expectBody(String.class).returnResult().getResponseBody();
assertThat(body).contains("Whitelabel Error Page")
.contains("type=Not Found, status=404");
@ -276,17 +270,13 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
@Test
public void whilelabelDisabled() {
this.contextRunner
.withPropertyValues("server.error.whitelabel.enabled=false",
"spring.mustache.prefix=classpath:/unknown/")
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
client.get().uri("/notfound")
.accept(MediaType.TEXT_HTML).exchange()
.expectStatus().isNotFound()
.expectBody().isEmpty();
});
this.contextRunner.withPropertyValues("server.error.whitelabel.enabled=false",
"spring.mustache.prefix=classpath:/unknown/").run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
client.get().uri("/notfound").accept(MediaType.TEXT_HTML).exchange()
.expectStatus().isNotFound().expectBody().isEmpty();
});
}
@Configuration

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

Loading…
Cancel
Save