-
Notifications
You must be signed in to change notification settings - Fork 214
/
MainMethodExceptionHandler.java
81 lines (68 loc) · 2.98 KB
/
MainMethodExceptionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.base.service;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.text.MessageFormat;
import java.util.concurrent.Callable;
import javax.annotation.concurrent.Immutable;
import org.slf4j.Logger;
import org.apache.pekko.actor.ActorSystem;
/**
* This class wraps the execution of a Runnable for catching {@code Throwable}s. The Runnable is assumed to be the
* body of a {@code main} method. If a Throwable gets caught it will be logged and re-thrown. Catching
* {@code Throwable} instead of {@code Exception} is done deliberately for being informed about really every
* exception which is thrown by {@code main} method.
*/
@Immutable
final class MainMethodExceptionHandler {
/**
* Pattern of the log message in case a Throwable was caught.
*/
static final String LOG_MESSAGE_PATTERN = "An exception occurred in main method of <{0}>!";
private final Logger logger;
private MainMethodExceptionHandler(final Logger theLogger) {
logger = theLogger;
}
/**
* Returns an instance of {@code MainMethodExceptionHandler}.
*
* @param logger the logger to be used for logging potential exceptions.
* @return the instance.
* @throws NullPointerException if {@code logger} is {@code null}.
*/
public static MainMethodExceptionHandler getInstance(final Logger logger) {
return new MainMethodExceptionHandler(checkNotNull(logger, "logger"));
}
/**
* Executes the specified Runnable. Occurring {@code Throwables} are logged and re-thrown.
*
* @param mainMethodBody the body of the {@code main} method to be executed.
* @throws NullPointerException if {@code mainMethodBody} is {@code null}.
*/
public ActorSystem call(final Callable<ActorSystem> mainMethodBody) {
return tryToRunMainMethodBody(checkNotNull(mainMethodBody, "Runnable to be executed"));
}
@SuppressWarnings("squid:S1181")
private ActorSystem tryToRunMainMethodBody(final Callable<ActorSystem> mainMethodBody) {
try {
return mainMethodBody.call();
} catch (final RuntimeException | Error t) {
logger.error(MessageFormat.format(LOG_MESSAGE_PATTERN, logger.getName()), t);
throw t;
} catch (final Throwable t) {
// Deliberately catching Throwable here for being informed about really every exception of main.
logger.error(MessageFormat.format(LOG_MESSAGE_PATTERN, logger.getName()), t);
throw new RuntimeException("Exception on main thread: " + t.getMessage(), t);
}
}
}