Skip to content

Commit

Permalink
Merge pull request #36 from palantir/feature/always-serialize-excepti…
Browse files Browse the repository at this point in the history
…on-type

JsonExceptionMapper includes exception type when includeStackTrace=false
  • Loading branch information
uschi2000 committed Feb 9, 2016
2 parents 9aac096 + 160b916 commit bb3efa1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public final Response toResponse(T exception) {
} else {
String errorId = UUID.randomUUID().toString();
log.error("Error {}: {}", errorId, message, exception);
error = SerializableError.of(errorId, RuntimeException.class);
error = SerializableError.of(errorId, exception.getClass());
}
builder.type(MediaType.APPLICATION_JSON);
String json = MAPPER.writeValueAsString(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* 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 com.palantir.remoting.http.server;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;

import javax.ws.rs.core.Response;
import org.junit.Test;

public final class JsonExceptionMapperTest {

private static class FooException extends RuntimeException {
FooException(String message) {
super(message);
}
}

private static final Response.Status STATUS = Response.Status.REQUESTED_RANGE_NOT_SATISFIABLE;

private static class TestJsonExceptionMapper extends JsonExceptionMapper<FooException> {
TestJsonExceptionMapper(boolean includeStackTrace) {
super(includeStackTrace, STATUS);
}
}

private FooException createException(String message) {
return new FooException(message);
}

private final TestJsonExceptionMapper mapper = new TestJsonExceptionMapper(true);
private final TestJsonExceptionMapper noStacktraceMapper = new TestJsonExceptionMapper(false);

@Test
public void test_withStacktrace() {
Response response = mapper.toResponse(createException("foo"));
assertThat(response.getStatus(), is(STATUS.getStatusCode()));
assertThat(response.getEntity().toString(), containsString("foo"));
assertThat(response.getEntity().toString(),
containsString("com.palantir.remoting.http.server.JsonExceptionMapperTest$FooException"));
assertThat(response.getEntity().toString(), containsString("\"methodName\" : \"createException\""));
}

@Test
public void test_withoutStacktrace() {
Response response = noStacktraceMapper.toResponse(createException("foo"));
assertThat(response.getStatus(), is(STATUS.getStatusCode()));
assertThat(response.getEntity().toString(), not(containsString("foo")));
assertThat(response.getEntity().toString(),
containsString("com.palantir.remoting.http.server.JsonExceptionMapperTest$FooException"));
assertThat(response.getEntity().toString(), not(containsString("\"methodName\" : \"createException\"")));
}
}

0 comments on commit bb3efa1

Please sign in to comment.