private static XContentBuilder convert(RestChannel channel, Throwable t) throws IOException {
        XContentBuilder builder = channel.newBuilder().startObject()
            .startObject("error");
        SQLActionException sqlActionException = null;
        builder.field("message", detailedMessage(t));
        if (t instanceof SQLActionException) {
            sqlActionException = (SQLActionException)t;
            builder.field("code", sqlActionException.errorCode());
        } else {
            builder.field("code", 5000);
        }
        builder.endObject();
        if (t != null && channel.request().paramAsBoolean("error_trace", false)
                && sqlActionException != null) {
            builder.field("error_trace", sqlActionException.stackTrace());
        }
        builder.endObject();
        return builder;
    }