-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactiveExceptions.java
74 lines (72 loc) · 2.33 KB
/
ReactiveExceptions.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
// Part of Hookless: https://hookless.machinezoo.com
package com.machinezoo.hookless.noexception;
import com.machinezoo.hookless.*;
import com.machinezoo.noexception.*;
import com.machinezoo.stagean.*;
/**
* Wrappers for {@link ExceptionHandler} and {@link ExceptionFilter}.
*/
@ApiIssue("if this is used often, we might consider neater API, e.g. .silence().blocking().run(...)")
public class ReactiveExceptions {
/*
* We aren't using the more concise anonymous inner classes, because we want nice class names in stack traces.
*/
private static class BlockingExceptionHandler extends ExceptionHandler {
private final ExceptionHandler inner;
BlockingExceptionHandler(ExceptionHandler inner) {
this.inner = inner;
}
@Override
public boolean handle(Throwable exception) {
if (CurrentReactiveScope.blocked())
return inner.handle(exception);
return false;
}
}
public static ExceptionHandler blocking(ExceptionHandler handler) {
return new BlockingExceptionHandler(handler);
}
private static class NonBlockingExceptionHandler extends ExceptionHandler {
private final ExceptionHandler inner;
NonBlockingExceptionHandler(ExceptionHandler inner) {
this.inner = inner;
}
@Override
public boolean handle(Throwable exception) {
if (!CurrentReactiveScope.blocked())
return inner.handle(exception);
return false;
}
}
public static ExceptionHandler nonblocking(ExceptionHandler handler) {
return new NonBlockingExceptionHandler(handler);
}
private static class BlockingExceptionFilter extends ExceptionFilter {
private final ExceptionFilter inner;
BlockingExceptionFilter(ExceptionFilter inner) {
this.inner = inner;
}
@Override
public void handle(Throwable exception) {
if (CurrentReactiveScope.blocked())
inner.handle(exception);
}
}
public static ExceptionFilter blocking(ExceptionFilter handler) {
return new BlockingExceptionFilter(handler);
}
private static class NonBlockingExceptionFilter extends ExceptionFilter {
private final ExceptionFilter inner;
NonBlockingExceptionFilter(ExceptionFilter inner) {
this.inner = inner;
}
@Override
public void handle(Throwable exception) {
if (!CurrentReactiveScope.blocked())
inner.handle(exception);
}
}
public static ExceptionFilter nonblocking(ExceptionFilter handler) {
return new NonBlockingExceptionFilter(handler);
}
}