Package com.linecorp.armeria.common.util
Class Exceptions
java.lang.Object
com.linecorp.armeria.common.util.Exceptions
Provides methods that are useful for handling exceptions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Throwable>
TclearTrace
(T exception) Empties the stack trace of the specifiedexception
.static boolean
isExpected
(Throwable cause) Returnstrue
if the specified exception is expected to occur in well-known circumstances.static boolean
isStreamCancelling
(Throwable cause) Returnstrue
if the specified exception will cancel the current request or response stream.static void
logIfUnexpected
(Logger logger, Channel ch, @Nullable SessionProtocol protocol, String debugData, Throwable cause) Logs the specified exception if it is unexpected.static void
logIfUnexpected
(Logger logger, Channel ch, @Nullable SessionProtocol protocol, Throwable cause) Logs the specified exception if it is unexpected.static void
logIfUnexpected
(Logger logger, Channel ch, String debugData, Throwable cause) Logs the specified exception if it is unexpected.static void
logIfUnexpected
(Logger logger, Channel ch, Throwable cause) Logs the specified exception if it is unexpected.static Throwable
Returns the cause of the specifiedthrowable
peeling it recursively, if it is one of theCompletionException
,ExecutionException
,InvocationTargetException
orExceptionInInitializerError
.static void
Throws a particularThrowable
only if it belongs to a set of "fatal" error varieties.static <T> T
throwUnsafely
(Throwable cause) Throws the specified exception violating thethrows
clause of the enclosing method.static String
Converts the stack trace of the specifiedexception
into aString
.
-
Method Details
-
logIfUnexpected
Logs the specified exception if it is unexpected. -
logIfUnexpected
Logs the specified exception if it is unexpected. -
logIfUnexpected
public static void logIfUnexpected(Logger logger, Channel ch, @Nullable @Nullable SessionProtocol protocol, Throwable cause) Logs the specified exception if it is unexpected. -
logIfUnexpected
public static void logIfUnexpected(Logger logger, Channel ch, @Nullable @Nullable SessionProtocol protocol, String debugData, Throwable cause) Logs the specified exception if it is unexpected. -
isExpected
Returnstrue
if the specified exception is expected to occur in well-known circumstances.ClosedChannelException
ClosedSessionException
IOException
- 'Connection reset/closed/aborted by peer'- 'Broken pipe'
Http2Exception
- 'Stream closed'SSLException
- 'SSLEngine closed already'
- See Also:
-
isStreamCancelling
Returnstrue
if the specified exception will cancel the current request or response stream. -
clearTrace
Empties the stack trace of the specifiedexception
. -
throwUnsafely
Throws the specified exception violating thethrows
clause of the enclosing method. This method is useful when you need to rethrow a checked exception inFunction
,Consumer
,Supplier
andRunnable
, only if you are sure that the rethrown exception will be handled as aThrowable
or anException
. For example:CompletableFuture.supplyAsync(() -> { try (FileInputStream fin = new FileInputStream(...)) { .... return someValue; } catch (IOException e) { // 'throw e;' won't work because Runnable.run() does not allow any checked exceptions. return Exceptions.throwUnsafely(e); } }).exceptionally(CompletionActions::log);
- Returns:
- This method never returns because it always throws an exception. However, combined with an
arbitrary return clause, you can terminate any non-void function with a single statement.
e.g.
return Exceptions.throwUnsafely(...);
vs.Exceptions.throwUnsafely(...); return null;
-
throwIfFatal
Throws a particularThrowable
only if it belongs to a set of "fatal" error varieties. These varieties are as follows:VirtualMachineError
ThreadDeath
LinkageError
onError
methods, but only if the errors are not so catastrophic that such a call would be futile, in which case you simply want to rethrow the error.- Parameters:
t
- theThrowable
to test and perhaps throw- See Also:
-
peel
Returns the cause of the specifiedthrowable
peeling it recursively, if it is one of theCompletionException
,ExecutionException
,InvocationTargetException
orExceptionInInitializerError
. Otherwise returns thethrowable
. -
traceText
Converts the stack trace of the specifiedexception
into aString
. This method always uses'\n'
as a line delimiter, unlikeThrowable.printStackTrace(PrintWriter)
orThrowables.getStackTraceAsString(Throwable)
.
-