Skip to content

Commit 04692f7

Browse files
authored
fix: Mention exception in log message on failures (#497)
A small suggestion for some improved ergonomics when reading log output from db-scheduler in the case of failures. I always find it effective to include a small "summary" of any caught exception in the actual log message. Often this will allow me to pinpoint the error at hand without needing to look into the stacktrace of the logger event. (But this is still possible of course, as before) Logs when exceptions propagates all the way back to db-scheduler can typically look like this: <img width="973" alt="db-scheduler-failure-log" src="https://github.com/kagkarlsson/db-scheduler/assets/174823/cec1f574-1b76-4443-84bc-92af519bcc80"> This PR will change the log message to mention the actual exception which was caught in the log message: > Unhandled exception IllegalArgumentException: 'Illegal character in path at index 9: [replace with URL]' during execution of task with name 'my-task-name'. Treating as failure. ## Fixes No existing issue, I suspect. Please consider this PR the issue, with a suggested fix, all bundled nicely together :) ## Reminders - [x] Added/ran automated tests (existing tests have been run) - [x] Update README and/or examples (no update necessary, I think) - [x] Ran `mvn spotless:apply` --- cc @kagkarlsson
1 parent f243d5e commit 04692f7

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) Gustav Karlsson
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.github.kagkarlsson.scheduler;
15+
16+
final class ExceptionUtils {
17+
18+
static String describe(Throwable t) {
19+
if (t == null) {
20+
return null;
21+
}
22+
23+
String typeDescription = t.getClass().getSimpleName();
24+
String message = t.getMessage();
25+
return typeDescription + (message != null ? ": '" + message + "'" : "");
26+
}
27+
28+
private ExceptionUtils() {}
29+
}

db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/ExecutePicked.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package com.github.kagkarlsson.scheduler;
1515

16+
import static com.github.kagkarlsson.scheduler.ExceptionUtils.describe;
17+
1618
import com.github.kagkarlsson.scheduler.logging.ConfigurableLogger;
1719
import com.github.kagkarlsson.scheduler.stats.StatsRegistry;
1820
import com.github.kagkarlsson.scheduler.task.CompletionHandler;
@@ -129,9 +131,10 @@ private void complete(
129131
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.COMPLETIONHANDLER_ERROR);
130132
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.UNEXPECTED_ERROR);
131133
LOG.error(
132-
"Failed while completing execution {}. Execution will likely remain scheduled and locked/picked. "
134+
"Failed while completing execution {}, because {}. Execution will likely remain scheduled and locked/picked. "
133135
+ "The execution should be detected as dead after a while, and handled according to the tasks DeadExecutionHandler.",
134136
execution,
137+
describe(e),
135138
e);
136139
}
137140
}
@@ -142,9 +145,8 @@ private void failure(
142145
Throwable cause,
143146
Instant executionStarted,
144147
String errorMessagePrefix) {
145-
String logMessage =
146-
errorMessagePrefix + " during execution of task with name '{}'. Treating as failure.";
147-
failureLogger.log(logMessage, cause, task.getName());
148+
String logMessage = "{} {} during execution of task with name '{}'. Treating as failure.";
149+
failureLogger.log(logMessage, cause, errorMessagePrefix, describe(cause), task.getName());
148150

149151
ExecutionComplete completeEvent =
150152
ExecutionComplete.failure(execution, executionStarted, clock.now(), cause);
@@ -158,9 +160,10 @@ private void failure(
158160
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.FAILUREHANDLER_ERROR);
159161
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.UNEXPECTED_ERROR);
160162
LOG.error(
161-
"Failed while completing execution {}. Execution will likely remain scheduled and locked/picked. "
163+
"Failed while completing execution {}, because {}. Execution will likely remain scheduled and locked/picked. "
162164
+ "The execution should be detected as dead after a while, and handled according to the tasks DeadExecutionHandler.",
163165
execution,
166+
describe(cause),
164167
e);
165168
}
166169
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.kagkarlsson.scheduler;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
import static org.hamcrest.Matchers.nullValue;
6+
7+
import java.io.IOException;
8+
import org.junit.jupiter.api.Test;
9+
10+
class ExceptionUtilsTest {
11+
12+
@Test
13+
void exceptionWithNoMessageDescribedAsItsClassSimpleName() {
14+
assertThat(
15+
ExceptionUtils.describe(new IllegalArgumentException()), is("IllegalArgumentException"));
16+
}
17+
18+
@Test
19+
void exceptionDescribedAsClassSimpleNameAndMessage() {
20+
assertThat(
21+
ExceptionUtils.describe(new IOException("timed out")), is("IOException: 'timed out'"));
22+
}
23+
24+
@Test
25+
void describingNullIsNull() {
26+
assertThat(ExceptionUtils.describe(null), is(nullValue()));
27+
}
28+
}

0 commit comments

Comments
 (0)