From f67abf2e99c6ffe3f4f11256a3a170796ef4bae0 Mon Sep 17 00:00:00 2001 From: Suneet Saldanha Date: Tue, 14 Feb 2023 15:55:58 -0800 Subject: [PATCH] Better logs for query errors (#13776) * Better logs for query errors * checkstyle --- .../org/apache/druid/query/QueryException.java | 14 ++++++++++++++ .../apache/druid/query/QueryExceptionTest.java | 12 ++++++++++++ .../druid/query/QueryInterruptedException.java | 13 ------------- .../query/QueryInterruptedExceptionTest.java | 18 ++++++++++++++++++ 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/org/apache/druid/query/QueryException.java b/core/src/main/java/org/apache/druid/query/QueryException.java index 3bb896cef85..5dbdef6ddb7 100644 --- a/core/src/main/java/org/apache/druid/query/QueryException.java +++ b/core/src/main/java/org/apache/druid/query/QueryException.java @@ -22,6 +22,7 @@ package org.apache.druid.query; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.druid.common.exception.SanitizableException; +import org.apache.druid.java.util.common.StringUtils; import javax.annotation.Nullable; import javax.validation.constraints.NotNull; @@ -224,4 +225,17 @@ public class QueryException extends RuntimeException implements SanitizableExcep { return fromErrorCode(errorCode); } + + @Override + public String toString() + { + return StringUtils.format( + "%s{msg=%s, code=%s, class=%s, host=%s}", + getClass().getSimpleName(), + getMessage(), + getErrorCode(), + getErrorClass(), + getHost() + ); + } } diff --git a/core/src/test/java/org/apache/druid/query/QueryExceptionTest.java b/core/src/test/java/org/apache/druid/query/QueryExceptionTest.java index 8212ebc344f..10a0b78521d 100644 --- a/core/src/test/java/org/apache/druid/query/QueryExceptionTest.java +++ b/core/src/test/java/org/apache/druid/query/QueryExceptionTest.java @@ -113,6 +113,18 @@ public class QueryExceptionTest Assert.assertNull(exception.getMessage()); } + @Test + public void testToStringReturnsUsefulInformation() + { + QueryException queryException = new QueryException(ERROR_CODE, ERROR_MESSAGE_ORIGINAL, ERROR_CLASS, HOST); + String exceptionToString = queryException.toString(); + Assert.assertTrue(exceptionToString.startsWith(QueryException.class.getSimpleName())); + Assert.assertTrue(exceptionToString.contains("msg=" + ERROR_MESSAGE_ORIGINAL)); + Assert.assertTrue(exceptionToString.contains("code=" + ERROR_CODE)); + Assert.assertTrue(exceptionToString.contains("class=" + ERROR_CLASS)); + Assert.assertTrue(exceptionToString.contains("host=" + HOST)); + } + private void expectFailTypeForCode(FailType expected, String code) { QueryException exception = new QueryException(new Exception(), code, "java.lang.Exception", "test"); diff --git a/processing/src/main/java/org/apache/druid/query/QueryInterruptedException.java b/processing/src/main/java/org/apache/druid/query/QueryInterruptedException.java index 91760aa7c10..f1893cf3eaa 100644 --- a/processing/src/main/java/org/apache/druid/query/QueryInterruptedException.java +++ b/processing/src/main/java/org/apache/druid/query/QueryInterruptedException.java @@ -21,7 +21,6 @@ package org.apache.druid.query; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import org.apache.druid.java.util.common.StringUtils; import javax.annotation.Nullable; import java.util.concurrent.CancellationException; @@ -70,18 +69,6 @@ public class QueryInterruptedException extends QueryException super(cause, getErrorCodeFromThrowable(cause), getErrorClassFromThrowable(cause), host); } - @Override - public String toString() - { - return StringUtils.format( - "QueryInterruptedException{msg=%s, code=%s, class=%s, host=%s}", - getMessage(), - getErrorCode(), - getErrorClass(), - getHost() - ); - } - private static String getErrorCodeFromThrowable(Throwable e) { if (e instanceof QueryInterruptedException) { diff --git a/processing/src/test/java/org/apache/druid/query/QueryInterruptedExceptionTest.java b/processing/src/test/java/org/apache/druid/query/QueryInterruptedExceptionTest.java index b450b6ba718..70e688e4621 100644 --- a/processing/src/test/java/org/apache/druid/query/QueryInterruptedExceptionTest.java +++ b/processing/src/test/java/org/apache/druid/query/QueryInterruptedExceptionTest.java @@ -170,6 +170,24 @@ public class QueryInterruptedExceptionTest ); } + @Test + public void testToStringShouldReturnUsefulInformation() + { + QueryInterruptedException exception = new QueryInterruptedException( + "error", + "error messagez", + "error class", + "host" + ); + String exceptionString = exception.toString(); + Assert.assertTrue(exceptionString.startsWith(QueryInterruptedException.class.getSimpleName())); + Assert.assertTrue(exceptionString.contains("code=" + "error")); + Assert.assertTrue(exceptionString.contains("msg=" + "error messagez")); + Assert.assertTrue(exceptionString.contains("class=" + "error class")); + Assert.assertTrue(exceptionString.contains("host=" + "host")); + + } + private static QueryInterruptedException roundTrip(final QueryInterruptedException e) { try {