handle cases when t is null when producing a detailed message for it

This commit is contained in:
kimchy 2010-02-22 00:05:32 +02:00
parent 8c16840fd7
commit 16a7dd137d
2 changed files with 4 additions and 20 deletions

View File

@ -37,6 +37,9 @@ public final class ExceptionsHelper {
} }
public static String detailedMessage(Throwable t, boolean newLines, int initialCounter) { public static String detailedMessage(Throwable t, boolean newLines, int initialCounter) {
if (t == null) {
return "Unknown";
}
int counter = initialCounter + 1; int counter = initialCounter + 1;
if (t.getCause() != null) { if (t.getCause() != null) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

View File

@ -19,11 +19,9 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.elasticsearch.util.io.FastCharArrayWriter;
import org.elasticsearch.util.json.JsonBuilder; import org.elasticsearch.util.json.JsonBuilder;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import static org.elasticsearch.ExceptionsHelper.*; import static org.elasticsearch.ExceptionsHelper.*;
import static org.elasticsearch.util.json.JsonBuilder.*; import static org.elasticsearch.util.json.JsonBuilder.*;
@ -33,20 +31,6 @@ import static org.elasticsearch.util.json.JsonBuilder.*;
*/ */
public class JsonThrowableRestResponse extends JsonRestResponse { public class JsonThrowableRestResponse extends JsonRestResponse {
private static class Holder {
FastCharArrayWriter writer;
PrintWriter printWriter;
}
private static ThreadLocal<Holder> cache = new ThreadLocal<Holder>() {
@Override protected Holder initialValue() {
Holder holder = new Holder();
holder.writer = new FastCharArrayWriter();
holder.printWriter = new PrintWriter(holder.writer);
return holder;
}
};
public JsonThrowableRestResponse(RestRequest request, Throwable t) throws IOException { public JsonThrowableRestResponse(RestRequest request, Throwable t) throws IOException {
this(request, Status.INTERNAL_SERVER_ERROR, t); this(request, Status.INTERNAL_SERVER_ERROR, t);
} }
@ -56,12 +40,9 @@ public class JsonThrowableRestResponse extends JsonRestResponse {
} }
private static JsonBuilder convert(RestRequest request, Throwable t) throws IOException { private static JsonBuilder convert(RestRequest request, Throwable t) throws IOException {
Holder holder = cache.get();
holder.writer.reset();
t.printStackTrace(holder.printWriter);
JsonBuilder builder = jsonBuilder().prettyPrint() JsonBuilder builder = jsonBuilder().prettyPrint()
.startObject().field("error", detailedMessage(t)); .startObject().field("error", detailedMessage(t));
if (request.paramAsBoolean("errorTrace", false)) { if (t != null && request.paramAsBoolean("errorTrace", false)) {
builder.startObject("errorTrace"); builder.startObject("errorTrace");
boolean first = true; boolean first = true;
while (t != null) { while (t != null) {