Skip to content

Commit

Permalink
Fix default error page and error page without stacktrace
Browse files Browse the repository at this point in the history
  • Loading branch information
dgageot committed May 7, 2015
1 parent 6c549d4 commit f1c7461
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/codestory/http/errors/ErrorPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class ErrorPayload {
public final String error;

public ErrorPayload(Throwable e) {
error = e.toString();
error = (e == null) ? "" : e.toString();
}
}
10 changes: 8 additions & 2 deletions src/main/java/net/codestory/http/payload/PayloadWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,15 @@ protected Payload errorPage(Throwable e) {
}

protected Payload errorPage(int errorCode, Throwable e) {
if ("text/html".equals(request.header("Accept", "text/html"))) {
return errorPageHtml(errorCode, e);
String defaultAcceptedType = "text/html";
String acceptedTyped = request.header("Accept", defaultAcceptedType);

for (String acceptedType : acceptedTyped.split("[,]")) {
if ("text/html".equals(acceptedType)) {
return errorPageHtml(errorCode, e);
}
}

return errorAsJson(errorCode, e);
}

Expand Down
36 changes: 36 additions & 0 deletions src/test/java/net/codestory/http/browser/ErrorPageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2013-2015 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package net.codestory.http.browser;

import net.codestory.http.testhelpers.AbstractProdWebServerTest;
import net.codestory.simplelenium.FluentTest;
import net.codestory.simplelenium.rules.TakeSnapshot;
import org.junit.Rule;
import org.junit.Test;

public class ErrorPageTest extends AbstractProdWebServerTest {
@Rule
public final TakeSnapshot takeSnapshot = new TakeSnapshot();

@Test
public void page_not_found() {
String baseUrl = "http://localhost:" + port();

new FluentTest(baseUrl)
.goTo("/unknown")
.find("h1").should().contain("Page not found");
}
}
36 changes: 36 additions & 0 deletions src/test/java/net/codestory/http/errors/ErrorPayloadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2013-2015 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package net.codestory.http.errors;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class ErrorPayloadTest {
@Test
public void error_message() {
ErrorPayload payload = new ErrorPayload(new RuntimeException("BUG"));

assertThat(payload.error).isEqualTo("java.lang.RuntimeException: BUG");
}

@Test
public void no_error() {
ErrorPayload payload = new ErrorPayload(null);

assertThat(payload.error).isEmpty();
}
}

0 comments on commit f1c7461

Please sign in to comment.