This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report exceptions without message with their exception class name #126
- Loading branch information
Showing
7 changed files
with
110 additions
and
28 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/main/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package biz.paluch.logging.gelf.intern; | ||
|
||
/** | ||
* {@link ErrorReporter} that post-processes the error message if it is {@code null} by using the exception class name as | ||
* fallback. | ||
* | ||
* @author Mark Paluch | ||
* @since 1.11.2 | ||
*/ | ||
public class MessagePostprocessingErrorReporter implements ErrorReporter { | ||
|
||
private final ErrorReporter delegate; | ||
|
||
public MessagePostprocessingErrorReporter(ErrorReporter delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void reportError(String message, Exception e) { | ||
|
||
String messageToUse = postProcessMessage(message, e); | ||
|
||
delegate.reportError(messageToUse, e); | ||
} | ||
|
||
private static String postProcessMessage(String message, Exception e) { | ||
|
||
if ((message == null || "null".equalsIgnoreCase(message)) && e != null) { | ||
|
||
if (e.getMessage() != null) { | ||
return e.getMessage(); | ||
} | ||
|
||
return e.getClass().getSimpleName(); | ||
} | ||
|
||
return message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/java/biz/paluch/logging/gelf/intern/MessagePostprocessingErrorReporterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package biz.paluch.logging.gelf.intern; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
|
||
import external.MockitoExtension; | ||
|
||
/** | ||
* @author Mark Paluch | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class MessagePostprocessingErrorReporterTests { | ||
|
||
@Mock | ||
private ErrorReporter target; | ||
|
||
@Test | ||
void shouldRetainOriginalMessage() { | ||
|
||
IOException e = new IOException(); | ||
new MessagePostprocessingErrorReporter(target).reportError("foo", e); | ||
|
||
verify(target).reportError("foo", e); | ||
} | ||
|
||
@Test | ||
void shouldReplaceNullMessageWithExceptionClassName() { | ||
|
||
IOException e = new IOException(); | ||
new MessagePostprocessingErrorReporter(target).reportError(null, e); | ||
|
||
verify(target).reportError("IOException", e); | ||
} | ||
|
||
@Test | ||
void shouldReplaceNullMessageWithExceptionMessage() { | ||
|
||
IOException e = new IOException("foo"); | ||
new MessagePostprocessingErrorReporter(target).reportError(null, e); | ||
|
||
verify(target).reportError("foo", e); | ||
} | ||
} |