use of io.crate.action.sql.SQLActionException in project crate by crate.
the class SQLExceptions method createSQLActionException.
/**
* Create a {@link SQLActionException} out of a {@link Throwable}.
* If concrete {@link ElasticsearchException} is found, first transform it
* to a {@link CrateException}
*/
public static SQLActionException createSQLActionException(Throwable e) {
if (e instanceof SQLActionException) {
return (SQLActionException) e;
}
e = esToCrateException(e);
int errorCode = 5000;
RestStatus restStatus = RestStatus.INTERNAL_SERVER_ERROR;
if (e instanceof CrateException) {
CrateException crateException = (CrateException) e;
if (e instanceof ValidationException) {
errorCode = 4000 + crateException.errorCode();
restStatus = RestStatus.BAD_REQUEST;
} else if (e instanceof ReadOnlyException) {
errorCode = 4030 + crateException.errorCode();
restStatus = RestStatus.FORBIDDEN;
} else if (e instanceof ResourceUnknownException) {
errorCode = 4040 + crateException.errorCode();
restStatus = RestStatus.NOT_FOUND;
} else if (e instanceof ConflictException) {
errorCode = 4090 + crateException.errorCode();
restStatus = RestStatus.CONFLICT;
} else if (e instanceof UnhandledServerException) {
errorCode = 5000 + crateException.errorCode();
}
} else if (e instanceof ParsingException) {
errorCode = 4000;
restStatus = RestStatus.BAD_REQUEST;
} else if (e instanceof MapperParsingException) {
errorCode = 4000;
restStatus = RestStatus.BAD_REQUEST;
}
String message = e.getMessage();
if (message == null) {
if (e instanceof CrateException && e.getCause() != null) {
// use cause because it contains a more meaningful error in most cases
e = e.getCause();
}
StackTraceElement[] stackTraceElements = e.getStackTrace();
if (stackTraceElements.length > 0) {
message = String.format(Locale.ENGLISH, "%s in %s", e.getClass().getSimpleName(), stackTraceElements[0]);
} else {
message = "Error in " + e.getClass().getSimpleName();
}
} else {
message = e.getClass().getSimpleName() + ": " + message;
}
return new SQLActionException(message, errorCode, restStatus, e.getStackTrace());
}
use of io.crate.action.sql.SQLActionException in project crate by crate.
the class CrateThrowableRestResponse method convert.
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) {
StringWriter stackTrace = new StringWriter();
sqlActionException.printStackTrace(new PrintWriter(stackTrace));
builder.field("error_trace", stackTrace.toString());
}
builder.endObject();
return builder;
}
Aggregations