Skip to content

Commit

Permalink
Mention exception in log message on failures
Browse files Browse the repository at this point in the history
  • Loading branch information
runeflobakk committed May 30, 2024
1 parent 3bd4745 commit 3639bcf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) Gustav Karlsson
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.kagkarlsson.scheduler;

final class ExceptionUtils {

static String describe(Throwable t) {
if (t == null) {
return null;
}

String typeDescription = t.getClass().getSimpleName();
String message = t.getMessage();
return typeDescription + (message != null ? ": '" + message + "'" : "");
}

private ExceptionUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.github.kagkarlsson.scheduler;

import static com.github.kagkarlsson.scheduler.ExceptionUtils.describe;

import com.github.kagkarlsson.scheduler.logging.ConfigurableLogger;
import com.github.kagkarlsson.scheduler.stats.StatsRegistry;
import com.github.kagkarlsson.scheduler.task.CompletionHandler;
Expand Down Expand Up @@ -129,9 +131,10 @@ private void complete(
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.COMPLETIONHANDLER_ERROR);
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.UNEXPECTED_ERROR);
LOG.error(
"Failed while completing execution {}. Execution will likely remain scheduled and locked/picked. "
"Failed while completing execution {}, because {}. Execution will likely remain scheduled and locked/picked. "
+ "The execution should be detected as dead after a while, and handled according to the tasks DeadExecutionHandler.",
execution,
describe(e),
e);
}
}
Expand All @@ -142,9 +145,8 @@ private void failure(
Throwable cause,
Instant executionStarted,
String errorMessagePrefix) {
String logMessage =
errorMessagePrefix + " during execution of task with name '{}'. Treating as failure.";
failureLogger.log(logMessage, cause, task.getName());
String logMessage = "{} {} during execution of task with name '{}'. Treating as failure.";
failureLogger.log(logMessage, cause, errorMessagePrefix, describe(cause), task.getName());

ExecutionComplete completeEvent =
ExecutionComplete.failure(execution, executionStarted, clock.now(), cause);
Expand All @@ -158,9 +160,10 @@ private void failure(
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.FAILUREHANDLER_ERROR);
statsRegistry.register(StatsRegistry.SchedulerStatsEvent.UNEXPECTED_ERROR);
LOG.error(
"Failed while completing execution {}. Execution will likely remain scheduled and locked/picked. "
"Failed while completing execution {}, because {}. Execution will likely remain scheduled and locked/picked. "
+ "The execution should be detected as dead after a while, and handled according to the tasks DeadExecutionHandler.",
execution,
describe(cause),
e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.kagkarlsson.scheduler;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

import java.io.IOException;
import org.junit.jupiter.api.Test;

class ExceptionUtilsTest {

@Test
void exceptionWithNoMessageDescribedAsItsClassSimpleName() {
assertThat(
ExceptionUtils.describe(new IllegalArgumentException()), is("IllegalArgumentException"));
}

@Test
void exceptionDescribedAsClassSimpleNameAndMessage() {
assertThat(
ExceptionUtils.describe(new IOException("timed out")), is("IOException: 'timed out'"));
}

@Test
void describingNullIsNull() {
assertThat(ExceptionUtils.describe(null), is(nullValue()));
}
}

0 comments on commit 3639bcf

Please sign in to comment.