|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-present, RxJava Contributors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | + * compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 | + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 | + * the License for the specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package io.reactivex.rxjava3.internal.observers; |
| 15 | + |
| 16 | +import io.reactivex.rxjava3.annotations.NonNull; |
| 17 | +import io.reactivex.rxjava3.core.MaybeObserver; |
| 18 | +import io.reactivex.rxjava3.disposables.Disposable; |
| 19 | +import io.reactivex.rxjava3.exceptions.*; |
| 20 | +import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 21 | + |
| 22 | +/** |
| 23 | + * Wraps another {@link MaybeObserver} and catches exceptions thrown by its |
| 24 | + * {@code onSubscribe}, {@code onSuccess}, {@code onError} or |
| 25 | + * {@code onComplete} methods despite the protocol forbids it. |
| 26 | + * <p> |
| 27 | + * Such exceptions are routed to the {@link RxJavaPlugins#onError(Throwable)} handler. |
| 28 | + * |
| 29 | + * @param <T> the element type of the sequence |
| 30 | + * @since 3.0.0 |
| 31 | + */ |
| 32 | +public final class SafeMaybeObserver<T> implements MaybeObserver<T> { |
| 33 | + |
| 34 | + final MaybeObserver<? super T> downstream; |
| 35 | + |
| 36 | + boolean onSubscribeFailed; |
| 37 | + |
| 38 | + public SafeMaybeObserver(MaybeObserver<? super T> downstream) { |
| 39 | + this.downstream = downstream; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onSubscribe(@NonNull Disposable d) { |
| 44 | + try { |
| 45 | + downstream.onSubscribe(d); |
| 46 | + } catch (Throwable ex) { |
| 47 | + Exceptions.throwIfFatal(ex); |
| 48 | + onSubscribeFailed = true; |
| 49 | + d.dispose(); |
| 50 | + RxJavaPlugins.onError(ex); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onSuccess(@NonNull T t) { |
| 56 | + if (!onSubscribeFailed) { |
| 57 | + try { |
| 58 | + downstream.onSuccess(t); |
| 59 | + } catch (Throwable ex) { |
| 60 | + Exceptions.throwIfFatal(ex); |
| 61 | + RxJavaPlugins.onError(ex); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onError(@NonNull Throwable e) { |
| 68 | + if (onSubscribeFailed) { |
| 69 | + RxJavaPlugins.onError(e); |
| 70 | + } else { |
| 71 | + try { |
| 72 | + downstream.onError(e); |
| 73 | + } catch (Throwable ex) { |
| 74 | + Exceptions.throwIfFatal(ex); |
| 75 | + RxJavaPlugins.onError(new CompositeException(e, ex)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onComplete() { |
| 82 | + if (!onSubscribeFailed) { |
| 83 | + try { |
| 84 | + downstream.onComplete(); |
| 85 | + } catch (Throwable ex) { |
| 86 | + Exceptions.throwIfFatal(ex); |
| 87 | + RxJavaPlugins.onError(ex); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments