@Override public Mono<Void> setComplete() { return Mono.error(new UnsupportedOperationException()); } }
private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) { try { return supplier.get(); } catch (Throwable ex) { return Mono.error(ex); } }
/** * Apply the exception handler and return the alternative result. * @param failure the exception * @return the new result or the same error if there is no exception handler */ public Mono<HandlerResult> applyExceptionHandler(Throwable failure) { return (this.exceptionHandler != null ? this.exceptionHandler.apply(failure) : Mono.error(failure)); }
@Override public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) { return Mono.error(ex); } }
@Override public Mono<Void> handle(ServerWebExchange exchange) { if (this.raise) { throw this.exception; } return Mono.error(this.exception); } }
private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) { if (String.class == dataType.resolve()) { return Mono.just(data.substring(0, data.length() - 1)); } if (this.decoder == null) { return Mono.error(new CodecException("No SSE decoder configured and the data is not String.")); } byte[] bytes = data.getBytes(StandardCharsets.UTF_8); Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes)); return this.decoder.decodeToMono(input, dataType, MediaType.TEXT_EVENT_STREAM, hints); }
private <R> Mono<R> createNotFoundError() { return Mono.defer(() -> { Exception ex = new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler"); return Mono.error(ex); }); }
@Bean public WebHandler webHandler() { return exchange -> Mono.error(new Exception()); } }
@Override public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { if (this.body != null) { return Mono.error(new IllegalStateException("Multiple calls to writeWith() not supported")); } this.body = Flux.just(generateHeaders()).concatWith(body); // We don't actually want to write (just save the body Flux) return Mono.empty(); }
private static <P extends Publisher<?>, M extends ReactiveHttpOutputMessage> Mono<Void> writeWithMessageWriters( M outputMessage, BodyInserter.Context context, P body, ResolvableType bodyType) { MediaType mediaType = outputMessage.getHeaders().getContentType(); return context.messageWriters().stream() .filter(messageWriter -> messageWriter.canWrite(bodyType, mediaType)) .findFirst() .map(BodyInserters::cast) .map(writer -> write(body, bodyType, mediaType, outputMessage, context, writer)) .orElseGet(() -> Mono.error(unsupportedError(bodyType, context, mediaType))); }
@Override public Mono<Object> readMono( ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) { // We're ahead of String + "*/*" // Let's see if we can aggregate the output (lest we time out)... if (elementType.resolve() == String.class) { Flux<DataBuffer> body = message.getBody(); return stringDecoder.decodeToMono(body, elementType, null, null).cast(Object.class); } return Mono.error(new UnsupportedOperationException( "ServerSentEventHttpMessageReader only supports reading stream of events as a Flux")); }
@Test // SPR-17473 public void onStatusWithMonoErrorAndBodyNotConsumed() { RuntimeException ex = new RuntimeException("response error"); testOnStatus(ex, response -> Mono.error(ex)); }
@Override public Mono<Void> close(CloseStatus status) { try { CloseReason.CloseCode code = CloseCodes.getCloseCode(status.getCode()); getDelegate().close(new CloseReason(code, status.getReason())); } catch (IOException ex) { return Mono.error(ex); } return Mono.empty(); }
@Override public Mono<Void> handle(ServerWebExchange exchange) { if (exchange.getRequest().getURI().getPath().equals("/form-parts")) { return assertGetFormParts(exchange); } return Mono.error(new AssertionError()); }
@Override public Mono<Object> getHandlerInternal(ServerWebExchange exchange) { PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication(); Object handler; try { handler = lookupHandler(lookupPath, exchange); } catch (Exception ex) { return Mono.error(ex); } return Mono.justOrEmpty(handler); }
@Test public void failure() { Throwable expected = new IllegalStateException("oops"); AtomicReference<Object> actual = new AtomicReference<>(); ListenableFuture<String> future = new MonoToListenableFutureAdapter<>(Mono.error(expected)); future.addCallback(actual::set, actual::set); assertEquals(expected, actual.get()); }
@Test public void onStatusWithMonoErrorAndBodyConsumed() { RuntimeException ex = new RuntimeException("response error"); testOnStatus(ex, response -> response.bodyToMono(Void.class).then(Mono.error(ex))); }
@Test public void errorBeforeFirstItem() throws Exception { IllegalStateException error = new IllegalStateException("boo"); Mono<Void> completion = Mono.<String>error(error).as(this::sendOperator); Signal<Void> signal = completion.materialize().block(); assertNotNull(signal); assertSame("Unexpected signal: " + signal, error, signal.getThrowable()); }
@Test public void skipUntilByteCountErrorInFlux() { DataBuffer foo = stringBuffer("foo"); Flux<DataBuffer> flux = Flux.just(foo).concatWith(Mono.error(new RuntimeException())); Flux<DataBuffer> result = DataBufferUtils.skipUntilByteCount(flux, 3L); StepVerifier.create(result) .expectError(RuntimeException.class) .verify(Duration.ofSeconds(5)); }
@Test public void encodeError() throws Exception { Mono<Pojo> input = Mono.error(new InputException()); testEncode(input, Pojo.class, step -> step .expectError(InputException.class) .verify()); }