Repository navigation
Expand file tree
/
Copy pathExpErrorHandling.java
More file actions
235 lines (208 loc) · 9.2 KB
/
Copy pathExpErrorHandling.java
File metadata and controls
235 lines (208 loc) · 9.2 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
// Manejo de errores en pipelines reactivos:
// onErrorReturn, onErrorResume, retry con backoff, doOnError
public class ExpErrorHandling {
// ======================= RESULTADO REACTIVO =======================
// Representa un elemento que puede ser valor o error
sealed interface Result<T> {
record Ok<T>(T value) implements Result<T> {}
record Err<T>(Throwable error) implements Result<T> {}
}
// ======================= REACTIVE PIPELINE CON ERROR HANDLING =======================
static class ReactPipeline<T> {
private final List<Result<T>> items;
ReactPipeline(List<Result<T>> items) {
this.items = items;
}
@SafeVarargs
static <T> ReactPipeline<T> of(T... values) {
List<Result<T>> items = new ArrayList<>();
for (T v : values) items.add(new Result.Ok<>(v));
return new ReactPipeline<>(items);
}
// Permite inyectar un error en posición específica para demos
static <T> ReactPipeline<T> withError(List<Object> mixed) {
List<Result<T>> items = new ArrayList<>();
for (Object o : mixed) {
if (o instanceof Throwable t) {
items.add(new Result.Err<>(t));
} else {
@SuppressWarnings("unchecked")
T val = (T) o;
items.add(new Result.Ok<>(val));
}
}
return new ReactPipeline<>(items);
}
// map: si hay error en el item, propaga el error
<R> ReactPipeline<R> map(Function<T, R> fn) {
List<Result<R>> result = new ArrayList<>();
for (Result<T> item : items) {
if (item instanceof Result.Ok<T> ok) {
try {
result.add(new Result.Ok<>(fn.apply(ok.value())));
} catch (Exception e) {
result.add(new Result.Err<>(e));
}
} else if (item instanceof Result.Err<T> err) {
result.add(new Result.Err<>(err.error()));
}
}
return new ReactPipeline<>(result);
}
// doOnError: efecto secundario al encontrar error, sin alterar el flujo
ReactPipeline<T> doOnError(Consumer<Throwable> action) {
for (Result<T> item : items) {
if (item instanceof Result.Err<T> err) {
action.accept(err.error());
}
}
return this;
}
// onErrorReturn: reemplazar error por un valor por defecto
ReactPipeline<T> onErrorReturn(T fallback) {
List<Result<T>> result = new ArrayList<>();
for (Result<T> item : items) {
if (item instanceof Result.Err) {
result.add(new Result.Ok<>(fallback));
} else {
result.add(item);
}
}
return new ReactPipeline<>(result);
}
// onErrorResume: reemplazar error con resultado de una función de recuperación
ReactPipeline<T> onErrorResume(Function<Throwable, T> fn) {
List<Result<T>> result = new ArrayList<>();
for (Result<T> item : items) {
if (item instanceof Result.Err<T> err) {
try {
result.add(new Result.Ok<>(fn.apply(err.error())));
} catch (Exception e2) {
result.add(new Result.Err<>(e2));
}
} else {
result.add(item);
}
}
return new ReactPipeline<>(result);
}
void subscribe(Consumer<T> onNext, Consumer<Throwable> onError, Runnable onComplete) {
for (Result<T> item : items) {
if (item instanceof Result.Ok<T> ok) {
onNext.accept(ok.value());
} else if (item instanceof Result.Err<T> err) {
onError.accept(err.error());
return; // error termina el stream
}
}
onComplete.run();
}
}
// ======================= RETRY CON BACKOFF =======================
// Simula retry con backoff exponencial: espera 100ms, 200ms, 400ms...
static class ServicioInestable {
private int intentos = 0;
private final int fallosAntes; // número de fallos antes de éxito
ServicioInestable(int fallosAntes) {
this.fallosAntes = fallosAntes;
}
String llamar() throws Exception {
intentos++;
if (intentos <= fallosAntes) {
System.out.println(" [Servicio] Intento " + intentos + " → FALLO");
throw new RuntimeException("Servicio no disponible (intento " + intentos + ")");
}
System.out.println(" [Servicio] Intento " + intentos + " → EXITO");
return "respuesta-ok";
}
}
static String retryConBackoff(ServicioInestable servicio, int maxRetries) throws Exception {
long espera = 100; // ms inicial
for (int i = 0; i <= maxRetries; i++) {
try {
return servicio.llamar();
} catch (Exception e) {
if (i == maxRetries) throw e; // último intento fallido → propagar
System.out.println(" [Retry] Esperando " + espera + "ms antes del reintento " + (i + 2) + "...");
Thread.sleep(espera);
espera *= 2; // backoff exponencial
}
}
throw new RuntimeException("Nunca debería llegar aquí");
}
public static void main(String[] args) throws Exception {
System.out.println("=== doOnError (efecto secundario sin cambiar flujo) ===\n");
List<Object> datos = new ArrayList<>();
datos.add(10);
datos.add(0); // provocará división por cero en map
datos.add(5);
ReactPipeline.<Integer>withError(datos)
.map(n -> {
if (n == 0) throw new ArithmeticException("Division por cero");
return 100 / n;
})
.doOnError(e -> System.out.println(" [LOG] Error detectado: " + e.getMessage()))
.subscribe(
v -> System.out.println(" onNext: " + v),
e -> System.out.println(" onError: " + e.getMessage()),
() -> System.out.println(" onComplete")
);
System.out.println();
System.out.println("=== onErrorReturn (valor de fallback) ===\n");
ReactPipeline.of(10, 5, 0, 2)
.map(n -> {
if (n == 0) throw new ArithmeticException("Division por cero");
return 100 / n;
})
.onErrorReturn(-1) // si hay error, emitir -1 y continuar
.subscribe(
v -> System.out.println(" onNext: " + v),
e -> System.out.println(" onError: " + e.getMessage()),
() -> System.out.println(" onComplete")
);
System.out.println();
System.out.println("=== onErrorResume (fallback con lógica) ===\n");
ReactPipeline.of("42", "abc", "7")
.map(s -> {
int val = Integer.parseInt(s); // "abc" lanza NumberFormatException
return val * 2;
})
.onErrorResume(e -> {
System.out.println(" [Recover] Error: " + e.getMessage() + " → usando 0");
return 0;
})
.subscribe(
v -> System.out.println(" onNext: " + v),
e -> System.out.println(" onError: " + e.getMessage()),
() -> System.out.println(" onComplete")
);
System.out.println();
System.out.println("=== retry con backoff exponencial ===\n");
System.out.println("Servicio que falla 3 veces antes de responder:");
try {
String resultado = retryConBackoff(new ServicioInestable(3), 5);
System.out.println(" Resultado final: " + resultado);
} catch (Exception e) {
System.out.println(" Error irrecuperable: " + e.getMessage());
}
System.out.println();
System.out.println("Servicio que siempre falla (maxRetries=2):");
try {
retryConBackoff(new ServicioInestable(10), 2);
} catch (Exception e) {
System.out.println(" Error irrecuperable propagado: " + e.getMessage());
}
System.out.println();
System.out.println("=== Resumen de operadores de error ===");
System.out.println("doOnError(fn) — efecto secundario (log), no altera el stream");
System.out.println("onErrorReturn(val) — emite valor por defecto, stream continúa/completa");
System.out.println("onErrorResume(fn) — fallback con lógica compleja");
System.out.println("retry(n) — vuelve a suscribirse hasta n veces");
System.out.println("retryWhen(fn) — retry con backoff exponencial personalizado");
}
}