public static void renderException(XContentBuilder builder, Params params, Exception e) throws IOException { builder.startObject("error"); final ElasticsearchException[] rootCauses = ElasticsearchException.guessRootCauses(e); builder.field("root_cause"); builder.startArray(); for (ElasticsearchException rootCause : rootCauses) { builder.startObject(); rootCause.toXContent( builder, new ToXContent.DelegatingMapParams( Collections.singletonMap(ElasticsearchException.REST_EXCEPTION_SKIP_CAUSE, "true"), params)); builder.endObject(); } builder.endArray(); ElasticsearchException.toXContent(builder, params, e); builder.endObject(); }
/** Renders a cause exception as xcontent */ protected void causeToXContent(XContentBuilder builder, Params params) throws IOException { final Throwable cause = getCause(); if (cause != null && params.paramAsBoolean(REST_EXCEPTION_SKIP_CAUSE, REST_EXCEPTION_SKIP_CAUSE_DEFAULT) == false) { builder.field("caused_by"); builder.startObject(); toXContent(builder, params, cause); builder.endObject(); } }
/** * Statis toXContent helper method that also renders non {@link * org.elasticsearch.ElasticsearchException} instances as XContent. */ public static void toXContent(XContentBuilder builder, Params params, Throwable ex) throws IOException { ex = ExceptionsHelper.unwrapCause(ex); if (ex instanceof ElasticsearchException) { ((ElasticsearchException) ex).toXContent(builder, params); } else { builder.field("type", getExceptionName(ex)); builder.field("reason", ex.getMessage()); if (ex.getCause() != null) { builder.field("caused_by"); builder.startObject(); toXContent(builder, params, ex.getCause()); builder.endObject(); } if (params.paramAsBoolean( REST_EXCEPTION_SKIP_STACK_TRACE, REST_EXCEPTION_SKIP_STACK_TRACE_DEFAULT) == false) { builder.field("stack_trace", ExceptionsHelper.stackTrace(ex)); } } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { Throwable ex = ExceptionsHelper.unwrapCause(this); if (ex != this) { toXContent(builder, params, this); } else { builder.field("type", getExceptionName()); builder.field("reason", getMessage()); for (String key : headers.keySet()) { if (key.startsWith("es.")) { List<String> values = headers.get(key); xContentHeader(builder, key.substring("es.".length()), values); } } innerToXContent(builder, params); renderHeader(builder, params); if (params.paramAsBoolean( REST_EXCEPTION_SKIP_STACK_TRACE, REST_EXCEPTION_SKIP_STACK_TRACE_DEFAULT) == false) { builder.field("stack_trace", ExceptionsHelper.stackTrace(this)); } } return builder; }