From 258244ef37dd0db6fb6b0dda92487d1b5de6d5de Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Mon, 12 Nov 2012 17:09:34 +0100 Subject: [PATCH] Deriving the REST status code from a failure can, very rarely, cause an infinite loop fixes #2402 --- .../elasticsearch/ElasticSearchException.java | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/elasticsearch/ElasticSearchException.java b/src/main/java/org/elasticsearch/ElasticSearchException.java index 608801815af..75ca45d30ac 100644 --- a/src/main/java/org/elasticsearch/ElasticSearchException.java +++ b/src/main/java/org/elasticsearch/ElasticSearchException.java @@ -23,8 +23,6 @@ import org.elasticsearch.rest.RestStatus; /** * A base class for all elasticsearch exceptions. - * - * */ public class ElasticSearchException extends RuntimeException { @@ -52,21 +50,15 @@ public class ElasticSearchException extends RuntimeException { * Returns the rest status code associated with this exception. */ public RestStatus status() { - ElasticSearchException current = this; - while (current instanceof ElasticSearchWrapperException) { - if (getCause() == null) { - break; - } - if (getCause() instanceof ElasticSearchException) { - current = (ElasticSearchException) getCause(); - } else { - break; - } - } - if (current == this) { + Throwable cause = unwrapCause(); + if (cause == this) { return RestStatus.INTERNAL_SERVER_ERROR; + } else if (cause instanceof ElasticSearchException) { + return ((ElasticSearchException) cause).status(); + } else if (cause instanceof IllegalArgumentException) { + return RestStatus.BAD_REQUEST; } else { - return current.status(); + return RestStatus.INTERNAL_SERVER_ERROR; } }