From feab5a374a33a7b7b8e96c6a88b872c4db33dcf1 Mon Sep 17 00:00:00 2001 From: Ismael Juma Date: Mon, 6 Jun 2016 20:28:31 -0700 Subject: [PATCH] KAFKA-3781; Errors.exceptionName() can throw NPE Author: Ismael Juma Reviewers: Grant Henke , Ewen Cheslack-Postava Closes #1476 from ijuma/kafka-3781-exception-name-npe --- .../java/org/apache/kafka/common/protocol/Errors.java | 4 ++-- .../org/apache/kafka/common/protocol/ErrorsTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/clients/src/main/java/org/apache/kafka/common/protocol/Errors.java b/clients/src/main/java/org/apache/kafka/common/protocol/Errors.java index 64a709e5eea..bd7310ba455 100644 --- a/clients/src/main/java/org/apache/kafka/common/protocol/Errors.java +++ b/clients/src/main/java/org/apache/kafka/common/protocol/Errors.java @@ -170,10 +170,10 @@ public enum Errors { } /** - * Returns the class name of the exception + * Returns the class name of the exception or null if this is {@code Errors.NONE}. */ public String exceptionName() { - return exception.getClass().getName(); + return exception == null ? null : exception.getClass().getName(); } /** diff --git a/clients/src/test/java/org/apache/kafka/common/protocol/ErrorsTest.java b/clients/src/test/java/org/apache/kafka/common/protocol/ErrorsTest.java index 2d96e587a60..e198e739ac9 100644 --- a/clients/src/test/java/org/apache/kafka/common/protocol/ErrorsTest.java +++ b/clients/src/test/java/org/apache/kafka/common/protocol/ErrorsTest.java @@ -77,4 +77,14 @@ public class ErrorsTest { assertEquals("forException should default to unknown", Errors.UNKNOWN, error); } + @Test + public void testExceptionName() { + String exceptionName = Errors.UNKNOWN.exceptionName(); + assertEquals("org.apache.kafka.common.errors.UnknownServerException", exceptionName); + exceptionName = Errors.NONE.exceptionName(); + assertNull(exceptionName); + exceptionName = Errors.INVALID_TOPIC_EXCEPTION.exceptionName(); + assertEquals("org.apache.kafka.common.errors.InvalidTopicException", exceptionName); + } + }