Class Exceptions
- Author:
- Stephane Maldini, Injae Kim
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAThrowablethat wraps the actualcausedelivered viaSubscriber.onError(Throwable)in case ofPublishers that themselves emit items of typePublisher. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Consumer<? super AutoCloseable>A general-purposeConsumerthat closesAutoCloseableresource.static final StringA common error message used when a reactive streams source doesn't seem to respect backpressure signals, resulting in an operator's internal queue to be full.static final ThrowableA singleton instance of a Throwable indicating a terminal state for exceptions, don't leak this! -
Method Summary
Modifier and TypeMethodDescriptionstatic final RuntimeExceptionaddSuppressed(RuntimeException original, Throwable suppressed) Safely suppress aThrowableon aRuntimeException.static final ThrowableaddSuppressed(Throwable original, Throwable suppressed) static <T> booleanaddThrowable(AtomicReferenceFieldUpdater<T, @Nullable Throwable> field, T instance, Throwable exception) Update an empty atomic reference with the given exception, or combine further added exceptions together as suppressed exceptions under a root Throwable with the"Multiple exceptions"message, if the atomic reference already holds one.static RuntimeExceptionPrepare an uncheckedRuntimeExceptionthat will bubble upstream if thrown by an operator.static IllegalStateExceptionReturn anUnsupportedOperationExceptionindicating that the error callback on a subscriber was not implemented, yet an error was propagated.static RuntimeExceptionAn exception that is propagated upward and considered as "fatal" as per Reactive Stream limited list of exceptions allowed to bubble.static IllegalStateExceptionReturn anIllegalStateExceptionindicating the receiver is overrun by more signals than expected in case of a bounded queue, or more generally that data couldn't be emitted due to a lack of requeststatic IllegalStateExceptionfailWithOverflow(String message) Return anIllegalStateExceptionindicating the receiver is overrun by more signals than expected in case of a bounded queue or more generally that data couldn't be emitted due to a lack of requeststatic RejectedExecutionExceptionReturn a singletonRejectedExecutionExceptionstatic RejectedExecutionExceptionfailWithRejected(String message) Return a newRejectedExecutionExceptionwith given message.static RejectedExecutionExceptionfailWithRejected(Throwable cause) Return a newRejectedExecutionExceptionwith standard message and cause, unless thecauseis already aRejectedExecutionExceptioncreated viafailWithRejected(Throwable)(not the singleton-producing variants).static RejectedExecutionExceptionReturn a singletonRejectedExecutionExceptionwith a message indicating the reason is due to the scheduler not being time-capablestatic booleanCheck if the given exception is abubbledwrapped exception.static booleanCheck if the given error is acancel signal.static booleanCheck if the given error is acallback not implementedexception, in which case itscausewill be the propagated error that couldn't be processed.static booleanCheck if aThrowableis considered by Reactor as Fatal and would be thrown bythrowIfFatal(Throwable).static booleanCheck if aThrowableis considered by Reactor as Jvm Fatal and would be thrown by boththrowIfFatal(Throwable)andthrowIfJvmFatal(Throwable).static booleanCheck aThrowableto see if it is a composite, as created bymultiple(Throwable...).static booleanCheck if the given exception represents anoverflow.static booleanCheck aThrowableto see if it indicates too many retry attempts have failed.static booleanCheck aThrowableto see if it is a traceback, as created by the checkpoint operator or debug utilities.static RuntimeExceptionCreate a composite exception that wraps the givenThrowable(s), as suppressed exceptions.static RuntimeExceptionCreate a composite exception that wraps the givenThrowable(s), as suppressed exceptions.static IllegalArgumentExceptionnullOrNegativeRequestException(long elements) static RuntimeExceptionPrepare an uncheckedRuntimeExceptionthat should be propagated downstream throughSubscriber.onError(Throwable).static RuntimeExceptionretryExhausted(String message, @Nullable Throwable cause) Return a newRuntimeExceptionthat represents too many failures on retry.terminate(AtomicReferenceFieldUpdater<T, @Nullable Throwable> field, T instance) Atomic utility to safely mark a volatile throwable reference with a terminal marker.static voidThrows a particularThrowableonly if it belongs to a set of "fatal" error varieties.static voidThrows a particularThrowableonly if it belongs to a set of "fatal" error varieties native to the JVM.static ThrowableunwrapMultiple(@Nullable Throwable potentialMultiple) unwrapMultipleExcludingTracebacks(@Nullable Throwable potentialMultiple) static ThrowablewrapSource(Throwable throwable) Wrap aThrowabledelivered viaSubscriber.onError(Throwable)from an upstreamPublisherthat itself emitsPublishers to distinguish the error signal from the inner sequence's processing errors.
-
Field Details
-
BACKPRESSURE_ERROR_QUEUE_FULL
A common error message used when a reactive streams source doesn't seem to respect backpressure signals, resulting in an operator's internal queue to be full.- See Also:
-
TERMINATED
A singleton instance of a Throwable indicating a terminal state for exceptions, don't leak this! -
AUTO_CLOSE
A general-purposeConsumerthat closesAutoCloseableresource. If exception is thrown during closing the resource, it will be propagated bypropagate(Throwable).
-
-
Method Details
-
addThrowable
public static <T> boolean addThrowable(AtomicReferenceFieldUpdater<T, @Nullable Throwable> field, T instance, Throwable exception) Update an empty atomic reference with the given exception, or combine further added exceptions together as suppressed exceptions under a root Throwable with the"Multiple exceptions"message, if the atomic reference already holds one. This is short-circuited if the reference containsTERMINATED.Since composite exceptions and traceback exceptions share the same underlying mechanism of suppressed exceptions, a traceback could be made part of a composite exception. Use
unwrapMultipleExcludingTracebacks(Throwable)to filter out such elements in a composite if needed.- Type Parameters:
T- the parent instance type- Parameters:
field- the target field updaterinstance- the parent instance for the fieldexception- the Throwable to add.- Returns:
- true if added, false if the field contained the
TERMINATEDinstance. - See Also:
-
wrapSource
Wrap aThrowabledelivered viaSubscriber.onError(Throwable)from an upstreamPublisherthat itself emitsPublishers to distinguish the error signal from the inner sequence's processing errors.- Parameters:
throwable- the source sequenceerrorsignal- Returns:
Exceptions.SourceException
-
multiple
Create a composite exception that wraps the givenThrowable(s), as suppressed exceptions. Instances create by this method can be detected using theisMultiple(Throwable)check. TheunwrapMultiple(Throwable)method will correctly unwrap these to aListof the suppressed exceptions. Note that is will also be consistent in producing a List for other types of exceptions by putting the input inside a single-element List.Since composite exceptions and traceback exceptions share the same underlying mechanism of suppressed exceptions, a traceback could be made part of a composite exception. Use
unwrapMultipleExcludingTracebacks(Throwable)to filter out such elements in a composite if needed.- Parameters:
throwables- the exceptions to wrap into a composite- Returns:
- a composite exception with a standard message, and the given throwables as suppressed exceptions
- See Also:
-
multiple
Create a composite exception that wraps the givenThrowable(s), as suppressed exceptions. Instances created by this method can be detected using theisMultiple(Throwable)check. TheunwrapMultiple(Throwable)method will correctly unwrap these to aListof the suppressed exceptions. Note that is will also be consistent in producing a List for other types of exceptions by putting the input inside a single-element List.Since composite exceptions and traceback exceptions share the same underlying mechanism of suppressed exceptions, a traceback could be made part of a composite exception. Use
unwrapMultipleExcludingTracebacks(Throwable)to filter out such elements in a composite if needed.- Parameters:
throwables- the exceptions to wrap into a composite- Returns:
- a composite exception with a standard message, and the given throwables as suppressed exceptions
- See Also:
-
bubble
Prepare an uncheckedRuntimeExceptionthat will bubble upstream if thrown by an operator.This method invokes
throwIfFatal(Throwable).- Parameters:
t- the root cause- Returns:
- an unchecked exception that should choose bubbling up over error callback path
-
duplicateOnSubscribeException
- Returns:
- a new
IllegalStateExceptionwith a cause message abiding to reactive stream specification rule 2.12.
-
errorCallbackNotImplemented
Return anUnsupportedOperationExceptionindicating that the error callback on a subscriber was not implemented, yet an error was propagated.- Parameters:
cause- original error not processed by a receiver.- Returns:
- an
UnsupportedOperationExceptionindicating the error callback was not implemented and holding the original propagated error. - See Also:
-
failWithCancel
An exception that is propagated upward and considered as "fatal" as per Reactive Stream limited list of exceptions allowed to bubble. It is not meant to be common error resolution but might assist implementors in dealing with boundaries (queues, combinations and async).- Returns:
- a
RuntimeExceptionthat can be identified viaisCancel(java.lang.Throwable) - See Also:
-
failWithOverflow
Return anIllegalStateExceptionindicating the receiver is overrun by more signals than expected in case of a bounded queue, or more generally that data couldn't be emitted due to a lack of request- Returns:
- an
IllegalStateException - See Also:
-
failWithOverflow
Return anIllegalStateExceptionindicating the receiver is overrun by more signals than expected in case of a bounded queue or more generally that data couldn't be emitted due to a lack of request- Parameters:
message- the exception's message- Returns:
- an
IllegalStateException - See Also:
-
failWithRejected
Return a singletonRejectedExecutionException- Returns:
- a singleton
RejectedExecutionException
-
failWithRejectedNotTimeCapable
Return a singletonRejectedExecutionExceptionwith a message indicating the reason is due to the scheduler not being time-capable- Returns:
- a singleton
RejectedExecutionException
-
failWithRejected
Return a newRejectedExecutionExceptionwith standard message and cause, unless thecauseis already aRejectedExecutionExceptioncreated viafailWithRejected(Throwable)(not the singleton-producing variants).- Parameters:
cause- the original exception that caused the rejection- Returns:
- a new
RejectedExecutionExceptionwith standard message and cause
-
failWithRejected
Return a newRejectedExecutionExceptionwith given message.- Parameters:
message- the rejection message- Returns:
- a new
RejectedExecutionExceptionwith custom message
-
retryExhausted
Return a newRuntimeExceptionthat represents too many failures on retry. This nature can be detected viaisRetryExhausted(Throwable). The cause of the last retry attempt is passed and stored as this exception'scause.- Parameters:
message- the messagecause- the cause of the last retry attempt that failed (or null if irrelevant)- Returns:
- a new
RuntimeExceptionrepresenting retry exhaustion due to too many attempts
-
isOverflow
Check if the given exception represents anoverflow. -
isBubbling
Check if the given exception is abubbledwrapped exception. -
isCancel
Check if the given error is acancel signal. -
isErrorCallbackNotImplemented
Check if the given error is acallback not implementedexception, in which case itscausewill be the propagated error that couldn't be processed. -
isMultiple
Check aThrowableto see if it is a composite, as created bymultiple(Throwable...).- Parameters:
t- theThrowableto check, null always yields false- Returns:
- true if the Throwable is an instance created by
multiple(Throwable...), false otherwise
-
isRetryExhausted
Check aThrowableto see if it indicates too many retry attempts have failed. Such an exception can be created viaretryExhausted(String, Throwable).- Parameters:
t- theThrowableto check, null always yields false- Returns:
- true if the Throwable is an instance representing retry exhaustion, false otherwise
-
isTraceback
Check aThrowableto see if it is a traceback, as created by the checkpoint operator or debug utilities.- Parameters:
t- theThrowableto check, null always yields false- Returns:
- true if the Throwable is a traceback, false otherwise
-
nullOrNegativeRequestException
- Parameters:
elements- the invalid requested demand- Returns:
- a new
IllegalArgumentExceptionwith a cause message abiding to reactive stream specification rule 3.9.
-
propagate
Prepare an uncheckedRuntimeExceptionthat should be propagated downstream throughSubscriber.onError(Throwable).This method invokes
throwIfFatal(Throwable).- Parameters:
t- the root cause- Returns:
- an unchecked exception to propagate through onError signals.
-
terminate
public static <T> @Nullable Throwable terminate(AtomicReferenceFieldUpdater<T, @Nullable Throwable> field, T instance) Atomic utility to safely mark a volatile throwable reference with a terminal marker.- Type Parameters:
T- the instance type- Parameters:
field- the atomic containerinstance- the reference instance- Returns:
- the previously masked throwable
-
isJvmFatal
Check if aThrowableis considered by Reactor as Jvm Fatal and would be thrown by boththrowIfFatal(Throwable)andthrowIfJvmFatal(Throwable). This is a subset ofisFatal(Throwable), namely:Unless wrapped explicitly, such exceptions would always be thrown by operators instead of propagation through onError, potentially interrupting progress of Flux/Mono sequences. When they occur, the JVM itself is assumed to be in an unrecoverable state, and so is Reactor.
- Parameters:
t- theThrowableto check- Returns:
- true if the throwable is considered Jvm Fatal
- See Also:
-
isFatal
Check if aThrowableis considered by Reactor as Fatal and would be thrown bythrowIfFatal(Throwable).BubblingException(as detectable byisBubbling(Throwable))ErrorCallbackNotImplemented(as detectable byisErrorCallbackNotImplemented(Throwable))-
Jvm Fatal exceptions
Unless wrapped explicitly, such exceptions would always be thrown by operators instead of propagation through onError, potentially interrupting progress of Flux/Mono sequences. When they occur, the assumption is that Reactor is in an unrecoverable state (notably because the JVM itself might be in an unrecoverable state).
- Parameters:
t- theThrowableto check- Returns:
- true if the throwable is considered fatal
- See Also:
-
throwIfFatal
Throws a particularThrowableonly if it belongs to a set of "fatal" error varieties. These varieties are as follows:BubblingException(as detectable byisBubbling(Throwable))ErrorCallbackNotImplemented(as detectable byisErrorCallbackNotImplemented(Throwable))VirtualMachineErrorThreadDeathLinkageError
- Parameters:
t- the exception to evaluate
-
throwIfJvmFatal
Throws a particularThrowableonly if it belongs to a set of "fatal" error varieties native to the JVM. These varieties are as follows:- Parameters:
t- the exception to evaluate
-
unwrap
- Parameters:
t- the exception to unwrap- Returns:
- the unwrapped exception or current one if null
-
unwrapMultiple
Attempt to unwrap aThrowableinto aListof Throwables. This is only done on the condition that said Throwable is a composite exception built bymultiple(Throwable...), in which case the list contains the exceptions wrapped as suppressed exceptions in the composite. In any other case, the list only contains the input Throwable (or is empty in case of null input).Since composite exceptions and traceback exceptions share the same underlying mechanism of suppressed exceptions, a traceback could be made part of a composite exception. Use
unwrapMultipleExcludingTracebacks(Throwable)to filter out such elements in a composite if needed. -
unwrapMultipleExcludingTracebacks
public static List<Throwable> unwrapMultipleExcludingTracebacks(@Nullable Throwable potentialMultiple) Attempt to unwrap aThrowableinto aListof Throwables, excluding instances that aretracebacks. This is only done on the condition that said Throwable is a composite exception built bymultiple(Throwable...), in which case the returned list contains its suppressed exceptions minus the tracebacks. In any other case, the list only contains the input Throwable (or is empty in case of null input).This is useful because tracebacks are added as suppressed exceptions and thus can appear as components of a composite.
-
addSuppressed
Safely suppress aThrowableon aRuntimeException. The returnedRuntimeException, bearing the suppressed exception, is most often the same as the original exception unless:- original and tentatively suppressed exceptions are one and the same: do nothing but return the original.
- original is one of the singleton
RejectedExecutionExceptioncreated by Reactor: make a copy theRejectedExecutionException, add the suppressed exception to it and return that copy.
- Parameters:
original- the originalRuntimeExceptionto bear a suppressed exceptionsuppressed- theThrowableto suppress- Returns:
- the (most of the time original)
RuntimeExceptionbearing the suppressedThrowable
-
addSuppressed
Safely suppress aThrowableon an originalThrowable. The returnedThrowable, bearing the suppressed exception, is most often the same as the original one unless:- original and tentatively suppressed exceptions are one and the same: do nothing but return the original.
- original is one of the singleton
RejectedExecutionExceptioncreated by Reactor: make a copy theRejectedExecutionException, add the suppressed exception to it and return that copy. - original is a special internal TERMINATED singleton instance: return it without suppressing the exception.
-