Java Code Examples for reactor.core.publisher.Mono#onErrorResume()

The following examples show how to use reactor.core.publisher.Mono#onErrorResume() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-analysis-note   File: DefaultWebClient.java    License: MIT License 6 votes vote down vote up
private <T extends Publisher<?>> T handleBody(ClientResponse response,
		T bodyPublisher, Function<Mono<? extends Throwable>, T> errorFunction) {

	if (HttpStatus.resolve(response.rawStatusCode()) != null) {
		for (StatusHandler handler : this.statusHandlers) {
			if (handler.test(response.statusCode())) {
				HttpRequest request = this.requestSupplier.get();
				Mono<? extends Throwable> exMono = handler.apply(response, request);
				exMono = exMono.flatMap(ex -> drainBody(response, ex));
				exMono = exMono.onErrorResume(ex -> drainBody(response, ex));
				T result = errorFunction.apply(exMono);
				return insertCheckpoint(result, response.statusCode(), request);
			}
		}
		return bodyPublisher;
	}
	else {
		return errorFunction.apply(createResponseException(response, this.requestSupplier.get()));
	}
}
 
Example 2
@Override
public Mono<Void> handle(ServerWebExchange exchange) {

	Mono<Void> completion;
	try {
		completion = super.handle(exchange);
	}
	catch (Throwable ex) {
		completion = Mono.error(ex);
	}

	for (WebExceptionHandler handler : this.exceptionHandlers) {
		completion = completion.onErrorResume(ex -> handler.handle(exchange, ex));
	}

	return completion;
}
 
Example 3
Source Project: java-technology-stack   File: DefaultWebClient.java    License: MIT License 6 votes vote down vote up
private <T extends Publisher<?>> T handleBody(ClientResponse response,
		T bodyPublisher, Function<Mono<? extends Throwable>, T> errorFunction) {

	if (HttpStatus.resolve(response.rawStatusCode()) != null) {
		for (StatusHandler handler : this.statusHandlers) {
			if (handler.test(response.statusCode())) {
				HttpRequest request = this.requestSupplier.get();
				Mono<? extends Throwable> exMono = handler.apply(response, request);
				exMono = exMono.flatMap(ex -> drainBody(response, ex));
				exMono = exMono.onErrorResume(ex -> drainBody(response, ex));
				return errorFunction.apply(exMono);
			}
		}
		return bodyPublisher;
	}
	else {
		return errorFunction.apply(createResponseException(response, this.requestSupplier.get()));
	}
}
 
Example 4
@Override
public Mono<Void> handle(ServerWebExchange exchange) {

	Mono<Void> completion;
	try {
		completion = super.handle(exchange);
	}
	catch (Throwable ex) {
		completion = Mono.error(ex);
	}

	for (WebExceptionHandler handler : this.exceptionHandlers) {
		completion = completion.onErrorResume(ex -> handler.handle(exchange, ex));
	}

	return completion;
}
 
Example 5
@Override
public <T> Mono<T> run(Mono<T> toRun, Function<Throwable, Mono<T>> fallback) {
	io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry
			.circuitBreaker(id, config.getCircuitBreakerConfig());
	circuitBreakerCustomizer
			.ifPresent(customizer -> customizer.customize(defaultCircuitBreaker));
	Mono<T> toReturn = toRun
			.transform(CircuitBreakerOperator.of(defaultCircuitBreaker))
			.timeout(config.getTimeLimiterConfig().getTimeoutDuration())
			// Since we are using the Mono timeout we need to tell the circuit breaker
			// about the error
			.doOnError(TimeoutException.class,
					t -> defaultCircuitBreaker.onError(config.getTimeLimiterConfig()
							.getTimeoutDuration().toMillis(), TimeUnit.MILLISECONDS,
							t));
	if (fallback != null) {
		toReturn = toReturn.onErrorResume(fallback);
	}
	return toReturn;
}
 
Example 6
/**
 * TODO Where's the 'Failed' exception? Add some logging
 */
@Test
public void onErrorResume() throws Exception {
	//given
	AtomicBoolean cheapFlag = new AtomicBoolean();
	AtomicBoolean expensiveFlag = new AtomicBoolean();

	Mono<String> cheapButDangerous = Mono.fromCallable(() -> {
		cheapFlag.set(true);
		throw new RuntimeException("Failed");
	});

	Mono<String> expensive = Mono.fromCallable(() -> {
		expensiveFlag.set(true);
		return "Expensive";
	});

	//when
	final Mono<String> withError = cheapButDangerous
			.onErrorResume(e -> expensive);

	//then
	withError
			.as(StepVerifier::create)
			.expectNext("Expensive")
			.verifyComplete();
	assertThat(cheapFlag).isTrue();
	assertThat(expensiveFlag).isTrue();
}
 
Example 7
@Override
public <T> Mono<T> run(Mono<T> toRun, Function<Throwable, Mono<T>> fallback) {
	Mono<T> toReturn = toRun.transform(new SentinelReactorTransformer<>(
			new EntryConfig(resourceName, entryType)));
	if (fallback != null) {
		toReturn = toReturn.onErrorResume(fallback);
	}
	return toReturn;
}
 
Example 8
Source Project: reactor-netty   File: HttpClientConnect.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Mono<? extends Connection> connect() {
	HttpClientConfig config = configuration();

	Mono<? extends Connection> mono;
	if (config.deferredConf != null) {
		return config.deferredConf.apply(Mono.just(config))
		           .flatMap(MonoHttpConnect::new);
	}
	else {
		mono = new MonoHttpConnect(config);
	}

	if (config.doOnConnect() != null) {
		mono = mono.doOnSubscribe(s -> config.doOnConnect().accept(config));
	}

	if (config.doOnRequestError != null) {
		mono = mono.onErrorResume(error ->
				Mono.subscriberContext()
				    .doOnNext(ctx -> config.doOnRequestError.accept(new FailedHttpClientRequest(ctx, config), error))
				    .then(Mono.error(error)));
	}

	if (config.connector != null) {
		mono = config.connector.apply(mono);
	}
	return mono;
}
 
Example 9
Source Project: resilience4j   File: RecoveryFunction.java    License: Apache License 2.0 5 votes vote down vote up
default Mono<? super O> onErrorResume(Mono<? super O> mono) {
    return mono.onErrorResume(t -> {
        O fallbackValue;
        try {
            Throwable actual = Optional.ofNullable(t.getCause()).orElse(t);
            fallbackValue = apply(actual);
        } catch (Exception e) {
            return Mono.error(e);
        }
        return Mono.just(fallbackValue);
    });
}
 
Example 10
/**
 * Read the body from a method argument with {@link HttpMessageReader}.
 * @param bodyParam represents the element type for the body
 * @param actualParam the actual method argument type; possibly different
 * from {@code bodyParam}, e.g. for an {@code HttpEntity} argument
 * @param isBodyRequired true if the body is required
 * @param bindingContext the binding context to use
 * @param exchange the current exchange
 * @return a Mono with the value to use for the method argument
 * @since 5.0.2
 */
protected Mono<Object> readBody(MethodParameter bodyParam, @Nullable MethodParameter actualParam,
		boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) {

	ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParam);
	ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType);
	Class<?> resolvedType = bodyType.resolve();
	ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
	ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType);
	isBodyRequired = isBodyRequired || (adapter != null && !adapter.supportsEmpty());

	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();

	MediaType contentType = request.getHeaders().getContentType();
	MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
	Object[] hints = extractValidationHints(bodyParam);

	if (mediaType.isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED)) {
		return Mono.error(new IllegalStateException(
				"In a WebFlux application, form data is accessed via ServerWebExchange.getFormData()."));
	}

	if (logger.isDebugEnabled()) {
		logger.debug(exchange.getLogPrefix() + (contentType != null ?
				"Content-Type:" + contentType :
				"No Content-Type, using " + MediaType.APPLICATION_OCTET_STREAM));
	}

	for (HttpMessageReader<?> reader : getMessageReaders()) {
		if (reader.canRead(elementType, mediaType)) {
			Map<String, Object> readHints = Hints.from(Hints.LOG_PREFIX_HINT, exchange.getLogPrefix());
			if (adapter != null && adapter.isMultiValue()) {
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..N [" + elementType + "]");
				}
				Flux<?> flux = reader.read(actualType, elementType, request, response, readHints);
				flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					flux = flux.switchIfEmpty(Flux.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					flux = flux.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return Mono.just(adapter.fromPublisher(flux));
			}
			else {
				// Single-value (with or without reactive type wrapper)
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..1 [" + elementType + "]");
				}
				Mono<?> mono = reader.readMono(actualType, elementType, request, response, readHints);
				mono = mono.onErrorResume(ex -> Mono.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					mono = mono.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					mono = mono.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return (adapter != null ? Mono.just(adapter.fromPublisher(mono)) : Mono.from(mono));
			}
		}
	}

	// No compatible reader but body may be empty..

	HttpMethod method = request.getMethod();
	if (contentType == null && method != null && SUPPORTED_METHODS.contains(method)) {
		Flux<DataBuffer> body = request.getBody().doOnNext(buffer -> {
			DataBufferUtils.release(buffer);
			// Body not empty, back to 415..
			throw new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType);
		});
		if (isBodyRequired) {
			body = body.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
		}
		return (adapter != null ? Mono.just(adapter.fromPublisher(body)) : Mono.from(body));
	}

	return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType));
}
 
Example 11
/**
 * Read the body from a method argument with {@link HttpMessageReader}.
 * @param bodyParam represents the element type for the body
 * @param actualParam the actual method argument type; possibly different
 * from {@code bodyParam}, e.g. for an {@code HttpEntity} argument
 * @param isBodyRequired true if the body is required
 * @param bindingContext the binding context to use
 * @param exchange the current exchange
 * @return a Mono with the value to use for the method argument
 * @since 5.0.2
 */
protected Mono<Object> readBody(MethodParameter bodyParam, @Nullable MethodParameter actualParam,
		boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) {

	ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParam);
	ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType);
	Class<?> resolvedType = bodyType.resolve();
	ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
	ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType);
	isBodyRequired = isBodyRequired || (adapter != null && !adapter.supportsEmpty());

	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();

	MediaType contentType = request.getHeaders().getContentType();
	MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
	Object[] hints = extractValidationHints(bodyParam);

	if (logger.isDebugEnabled()) {
		logger.debug(exchange.getLogPrefix() + (contentType != null ?
				"Content-Type:" + contentType :
				"No Content-Type, using " + MediaType.APPLICATION_OCTET_STREAM));
	}

	for (HttpMessageReader<?> reader : getMessageReaders()) {
		if (reader.canRead(elementType, mediaType)) {
			Map<String, Object> readHints = Hints.from(Hints.LOG_PREFIX_HINT, exchange.getLogPrefix());
			if (adapter != null && adapter.isMultiValue()) {
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..N [" + elementType + "]");
				}
				Flux<?> flux = reader.read(actualType, elementType, request, response, readHints);
				flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					flux = flux.switchIfEmpty(Flux.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					flux = flux.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return Mono.just(adapter.fromPublisher(flux));
			}
			else {
				// Single-value (with or without reactive type wrapper)
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..1 [" + elementType + "]");
				}
				Mono<?> mono = reader.readMono(actualType, elementType, request, response, readHints);
				mono = mono.onErrorResume(ex -> Mono.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					mono = mono.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					mono = mono.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return (adapter != null ? Mono.just(adapter.fromPublisher(mono)) : Mono.from(mono));
			}
		}
	}

	// No compatible reader but body may be empty..

	HttpMethod method = request.getMethod();
	if (contentType == null && method != null && SUPPORTED_METHODS.contains(method)) {
		Flux<DataBuffer> body = request.getBody().doOnNext(o -> {
			// Body not empty, back to 415..
			throw new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType);
		});
		if (isBodyRequired) {
			body = body.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
		}
		return (adapter != null ? Mono.just(adapter.fromPublisher(body)) : Mono.from(body));
	}

	return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType));
}
 
Example 12
private Publisher<?> body(Object handler, ServerWebExchange exchange) {
	ResolvableType elementType = ResolvableType
			.forClass(this.inspector.getInputType(handler));

	// we effectively delegate type conversion to FunctionCatalog
	elementType = ResolvableType.forClass(String.class);

	ResolvableType actualType = elementType;

	Class<?> resolvedType = elementType.resolve();
	ReactiveAdapter adapter = (resolvedType != null
			? getAdapterRegistry().getAdapter(resolvedType) : null);

	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();

	MediaType contentType = request.getHeaders().getContentType();
	MediaType mediaType = (contentType != null ? contentType
			: MediaType.APPLICATION_OCTET_STREAM);

	if (logger.isDebugEnabled()) {
		logger.debug(exchange.getLogPrefix() + (contentType != null
				? "Content-Type:" + contentType
				: "No Content-Type, using " + MediaType.APPLICATION_OCTET_STREAM));
	}
	boolean isBodyRequired = (adapter != null && !adapter.supportsEmpty());

	MethodParameter bodyParam = new MethodParameter(handlerMethod(handler), 0);
	for (HttpMessageReader<?> reader : getMessageReaders()) {
		if (reader.canRead(elementType, mediaType)) {
			Map<String, Object> readHints = Hints.from(Hints.LOG_PREFIX_HINT,
					exchange.getLogPrefix());
			if (adapter != null && adapter.isMultiValue()) {
				if (logger.isDebugEnabled()) {
					logger.debug(
							exchange.getLogPrefix() + "0..N [" + elementType + "]");
				}
				Flux<?> flux = reader.read(actualType, elementType, request, response,
						readHints);
				flux = flux.onErrorResume(
						ex -> Flux.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					flux = flux.switchIfEmpty(
							Flux.error(() -> handleMissingBody(bodyParam)));
				}
				return Mono.just(adapter.fromPublisher(flux));
			}
			else {
				// Single-value (with or without reactive type wrapper)
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..1 [" + elementType + "]");
				}
				Mono<?> mono = reader.readMono(actualType, elementType, request,
						response, readHints).doOnNext(v -> {
							if (logger.isDebugEnabled()) {
								logger.debug("received: " + v);
							}
						});
				mono = mono.onErrorResume(
						ex -> Mono.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					mono = mono.switchIfEmpty(
							Mono.error(() -> handleMissingBody(bodyParam)));
				}
				return (adapter != null ? Mono.just(adapter.fromPublisher(mono))
						: Mono.from(mono));
			}
		}
	}

	return Mono.error(new UnsupportedMediaTypeStatusException(mediaType,
			Arrays.asList(MediaType.APPLICATION_JSON), elementType));
}