From 238f5d9b8b9e4819a9909a7403dcbc7bec0dc714 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 29 Apr 2019 22:31:55 -0700 Subject: Log exception root cause --- src/main/java/org/traccar/api/ResourceErrorHandler.java | 12 +++--------- src/main/java/org/traccar/helper/Log.java | 5 +++++ src/test/java/org/traccar/helper/LogTest.java | 13 +++++++++++-- 3 files changed, 19 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/api/ResourceErrorHandler.java b/src/main/java/org/traccar/api/ResourceErrorHandler.java index 1d618b08d..108a8e8cc 100644 --- a/src/main/java/org/traccar/api/ResourceErrorHandler.java +++ b/src/main/java/org/traccar/api/ResourceErrorHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2019 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,14 +26,8 @@ public class ResourceErrorHandler implements ExceptionMapper { @Override public Response toResponse(Exception e) { if (e instanceof WebApplicationException) { - WebApplicationException exception = (WebApplicationException) e; - String message; - if (exception.getCause() != null) { - message = Log.exceptionStack(exception.getCause()); - } else { - message = Log.exceptionStack(exception); - } - return Response.fromResponse(exception.getResponse()).entity(message).build(); + WebApplicationException webException = (WebApplicationException) e; + return Response.fromResponse(webException.getResponse()).entity(Log.exceptionStack(webException)).build(); } else { return Response.status(Response.Status.BAD_REQUEST).entity(Log.exceptionStack(e)).build(); } diff --git a/src/main/java/org/traccar/helper/Log.java b/src/main/java/org/traccar/helper/Log.java index f328e8ce9..607a0585f 100644 --- a/src/main/java/org/traccar/helper/Log.java +++ b/src/main/java/org/traccar/helper/Log.java @@ -211,6 +211,11 @@ public final class Log { } public static String exceptionStack(Throwable exception) { + Throwable cause; + while (null != (cause = exception.getCause()) && (exception != cause) ) { + exception = cause; + } + StringBuilder s = new StringBuilder(); String exceptionMsg = exception.getMessage(); if (exceptionMsg != null) { diff --git a/src/test/java/org/traccar/helper/LogTest.java b/src/test/java/org/traccar/helper/LogTest.java index 853eb05c9..ef33c32ba 100644 --- a/src/test/java/org/traccar/helper/LogTest.java +++ b/src/test/java/org/traccar/helper/LogTest.java @@ -7,8 +7,17 @@ import static org.junit.Assert.assertEquals; public class LogTest { @Test - public void testLog() { - assertEquals("test - Exception (LogTest:11 < ...)", Log.exceptionStack(new Exception("test"))); + public void testExceptionStack() { + assertEquals( + "test - Exception (LogTest:11 < ...)", + Log.exceptionStack(new Exception("test"))); + } + + @Test + public void testExceptionStackRootCause() { + assertEquals( + "root - Exception (LogTest:18 < ...)", + Log.exceptionStack(new Exception("test", new Exception("root")))); } } -- cgit v1.2.3