@@ -294,7 +294,7 @@ public Throwable unwrapCause() {
294
294
public String getDetailedMessage () {
295
295
if (getCause () != null ) {
296
296
StringBuilder sb = new StringBuilder ();
297
- sb .append (toString () ).append ("; " );
297
+ sb .append (this ).append ("; " );
298
298
if (getCause () instanceof ElasticsearchException ) {
299
299
sb .append (((ElasticsearchException ) getCause ()).getDetailedMessage ());
300
300
} else {
@@ -384,7 +384,7 @@ protected XContentBuilder toXContent(XContentBuilder builder, Params params, int
384
384
if (ex != this ) {
385
385
generateThrowableXContent (builder , params , this , nestedLevel );
386
386
} else {
387
- innerToXContent (builder , params , this , getExceptionName (), getMessage (), headers , metadata , getCause (), nestedLevel );
387
+ innerToXContent (builder , params , this , headers , metadata , getCause (), nestedLevel );
388
388
}
389
389
return builder ;
390
390
}
@@ -393,8 +393,6 @@ protected static void innerToXContent(
393
393
XContentBuilder builder ,
394
394
Params params ,
395
395
Throwable throwable ,
396
- String type ,
397
- String message ,
398
396
Map <String , List <String >> headers ,
399
397
Map <String , List <String >> metadata ,
400
398
Throwable cause ,
@@ -408,16 +406,12 @@ protected static void innerToXContent(
408
406
return ;
409
407
}
410
408
411
- builder .field (TYPE , type );
412
- builder .field (REASON , message );
409
+ builder .field (TYPE , throwable instanceof ElasticsearchException e ? e . getExceptionName () : getExceptionName ( throwable ) );
410
+ builder .field (REASON , throwable . getMessage () );
413
411
414
- boolean timedOut = false ;
415
- if (throwable instanceof ElasticsearchException exception ) {
416
- // TODO: we could walk the exception chain to see if _any_ causes are timeouts?
417
- timedOut = exception .isTimeout ();
418
- }
419
- if (timedOut ) {
420
- builder .field (TIMED_OUT , timedOut );
412
+ // TODO: we could walk the exception chain to see if _any_ causes are timeouts?
413
+ if (throwable instanceof ElasticsearchException exception && exception .isTimeout ()) {
414
+ builder .field (TIMED_OUT , true );
421
415
}
422
416
423
417
for (Map .Entry <String , List <String >> entry : metadata .entrySet ()) {
@@ -428,13 +422,10 @@ protected static void innerToXContent(
428
422
exception .metadataToXContent (builder , params );
429
423
}
430
424
431
- if (params .paramAsBoolean (REST_EXCEPTION_SKIP_CAUSE , REST_EXCEPTION_SKIP_CAUSE_DEFAULT ) == false ) {
432
- if (cause != null ) {
433
- builder .field (CAUSED_BY );
434
- builder .startObject ();
435
- generateThrowableXContent (builder , params , cause , nestedLevel + 1 );
436
- builder .endObject ();
437
- }
425
+ if (cause != null && params .paramAsBoolean (REST_EXCEPTION_SKIP_CAUSE , REST_EXCEPTION_SKIP_CAUSE_DEFAULT ) == false ) {
426
+ builder .startObject (CAUSED_BY );
427
+ generateThrowableXContent (builder , params , cause , nestedLevel + 1 );
428
+ builder .endObject ();
438
429
}
439
430
440
431
if (headers .isEmpty () == false ) {
@@ -607,7 +598,7 @@ public static ElasticsearchException innerFromXContent(XContentParser parser, bo
607
598
/**
608
599
* Static toXContent helper method that renders {@link org.elasticsearch.ElasticsearchException} or {@link Throwable} instances
609
600
* as XContent, delegating the rendering to {@link #toXContent(XContentBuilder, Params)}
610
- * or {@link #innerToXContent(XContentBuilder, Params, Throwable, String, String, Map, Map, Throwable, int)}.
601
+ * or {@link #innerToXContent(XContentBuilder, Params, Throwable, Map, Map, Throwable, int)}.
611
602
*
612
603
* This method is usually used when the {@link Throwable} is rendered as a part of another XContent object, and its result can
613
604
* be parsed back using the {@link #fromXContent(XContentParser)} method.
@@ -627,7 +618,7 @@ protected static void generateThrowableXContent(XContentBuilder builder, Params
627
618
if (t instanceof ElasticsearchException ) {
628
619
((ElasticsearchException ) t ).toXContent (builder , params , nestedLevel );
629
620
} else {
630
- innerToXContent (builder , params , t , getExceptionName ( t ), t . getMessage (), emptyMap (), emptyMap (), t .getCause (), nestedLevel );
621
+ innerToXContent (builder , params , t , emptyMap (), emptyMap (), t .getCause (), nestedLevel );
631
622
}
632
623
}
633
624
@@ -723,8 +714,8 @@ public static ElasticsearchException failureFromXContent(XContentParser parser)
723
714
*/
724
715
public ElasticsearchException [] guessRootCauses () {
725
716
final Throwable cause = getCause ();
726
- if (cause != null && cause instanceof ElasticsearchException ) {
727
- return (( ElasticsearchException ) cause ) .guessRootCauses ();
717
+ if (cause instanceof ElasticsearchException ese ) {
718
+ return ese .guessRootCauses ();
728
719
}
729
720
return new ElasticsearchException [] { this };
730
721
}
@@ -773,35 +764,28 @@ protected String getExceptionName() {
773
764
*/
774
765
public static String getExceptionName (Throwable ex ) {
775
766
String simpleName = ex .getClass ().getSimpleName ();
776
- if (simpleName .startsWith ("Elasticsearch" )) {
777
- simpleName = simpleName .substring ("Elasticsearch" .length ());
778
- }
779
767
// TODO: do we really need to make the exception name in underscore casing?
780
- return toUnderscoreCase (simpleName );
768
+ return toUnderscoreCase (simpleName , simpleName . startsWith ( "Elasticsearch" ) ? "Elasticsearch" . length () : 0 );
781
769
}
782
770
783
771
static String buildMessage (String type , String reason , String stack ) {
784
- StringBuilder message = new StringBuilder ("Elasticsearch exception [" );
785
- message .append (TYPE ).append ('=' ).append (type );
786
- message .append (", " ).append (REASON ).append ('=' ).append (reason );
787
- if (stack != null ) {
788
- message .append (", " ).append (STACK_TRACE ).append ('=' ).append (stack );
789
- }
790
- message .append (']' );
791
- return message .toString ();
772
+ return "Elasticsearch exception ["
773
+ + TYPE
774
+ + "="
775
+ + type
776
+ + ", "
777
+ + REASON
778
+ + "="
779
+ + reason
780
+ + (stack == null ? "" : (", " + STACK_TRACE + "=" + stack ))
781
+ + "]" ;
792
782
}
793
783
794
784
@ Override
795
785
public String toString () {
796
- StringBuilder builder = new StringBuilder ();
797
- if (metadata .containsKey (INDEX_METADATA_KEY )) {
798
- builder .append (getIndex ());
799
- if (metadata .containsKey (SHARD_METADATA_KEY )) {
800
- builder .append ('[' ).append (getShardId ()).append (']' );
801
- }
802
- builder .append (' ' );
803
- }
804
- return builder .append (super .toString ().trim ()).toString ();
786
+ return (metadata .containsKey (INDEX_METADATA_KEY )
787
+ ? (getIndex () + (metadata .containsKey (SHARD_METADATA_KEY ) ? "[" + getShardId () + "] " : " " ))
788
+ : "" ) + super .toString ().trim ();
805
789
}
806
790
807
791
/**
@@ -2106,19 +2090,17 @@ public String getResourceType() {
2106
2090
}
2107
2091
2108
2092
// lower cases and adds underscores to transitions in a name
2109
- private static String toUnderscoreCase (String value ) {
2093
+ private static String toUnderscoreCase (String value , final int offset ) {
2110
2094
StringBuilder sb = new StringBuilder ();
2111
2095
boolean changed = false ;
2112
- for (int i = 0 ; i < value .length (); i ++) {
2096
+ for (int i = offset ; i < value .length (); i ++) {
2113
2097
char c = value .charAt (i );
2114
2098
if (Character .isUpperCase (c )) {
2115
2099
if (changed == false ) {
2116
2100
// copy it over here
2117
- for (int j = 0 ; j < i ; j ++) {
2118
- sb .append (value .charAt (j ));
2119
- }
2101
+ sb .append (value , offset , i );
2120
2102
changed = true ;
2121
- if (i == 0 ) {
2103
+ if (i == offset ) {
2122
2104
sb .append (Character .toLowerCase (c ));
2123
2105
} else {
2124
2106
sb .append ('_' );
@@ -2135,7 +2117,7 @@ private static String toUnderscoreCase(String value) {
2135
2117
}
2136
2118
}
2137
2119
if (changed == false ) {
2138
- return value ;
2120
+ return offset == 0 ? value : value . substring ( offset ) ;
2139
2121
}
2140
2122
return sb .toString ();
2141
2123
}
0 commit comments