Rename Mono#otherwise[Empty] to switch[onError/IfEmpty]

Issue: SPR-15318
This commit is contained in:
Sebastien Deleuze
2017-04-13 15:30:21 +02:00
parent cec36fe784
commit e3fae2716e
19 changed files with 23 additions and 23 deletions

View File

@@ -57,7 +57,7 @@ public class ReactorHttpHandlerAdapter
ReactorServerHttpResponse resp = new ReactorServerHttpResponse(response, bufferFactory);
return this.httpHandler.handle(req, resp)
.otherwise(ex -> {
.switchOnError(ex -> {
logger.error("Could not complete request", ex);
response.status(HttpResponseStatus.INTERNAL_SERVER_ERROR);
return Mono.empty();

View File

@@ -66,7 +66,7 @@ public class RxNettyHttpHandlerAdapter implements RequestHandler<ByteBuf, ByteBu
RxNettyServerHttpResponse response = new RxNettyServerHttpResponse(nativeResponse, bufferFactory);
Publisher<Void> result = this.httpHandler.handle(request, response)
.otherwise(ex -> {
.switchOnError(ex -> {
logger.error("Could not complete request", ex);
nativeResponse.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
return Mono.empty();

View File

@@ -106,7 +106,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) {
return FORM_READER
.readMono(FORM_DATA_VALUE_TYPE, request, Collections.emptyMap())
.otherwiseIfEmpty(EMPTY_FORM_DATA)
.switchIfEmpty(EMPTY_FORM_DATA)
.cache();
}
}

View File

@@ -76,7 +76,7 @@ public class HttpWebHandlerAdapter extends WebHandlerDecorator implements HttpHa
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
ServerWebExchange exchange = createExchange(request, response);
return getDelegate().handle(exchange)
.otherwise(ex -> {
.switchOnError(ex -> {
if (logger.isDebugEnabled()) {
logger.debug("Could not complete request", ex);
}

View File

@@ -99,7 +99,7 @@ public class ExceptionHandlingWebHandler extends WebHandlerDecorator {
}
for (WebExceptionHandler handler : this.exceptionHandlers) {
completion = completion.otherwise(ex -> handler.handle(exchange, ex));
completion = completion.switchOnError(ex -> handler.handle(exchange, ex));
}
return completion;

View File

@@ -105,7 +105,7 @@ public class DefaultWebSessionManager implements WebSessionManager {
.concatMap(this.sessionStore::retrieveSession)
.next()
.flatMap(session -> validateSession(exchange, session))
.otherwiseIfEmpty(createSession(exchange))
.switchIfEmpty(createSession(exchange))
.map(session -> extendSession(exchange, session)));
}

View File

@@ -96,7 +96,7 @@ public class ServerHttpResponseTests {
public void writeWithError() throws Exception {
TestServerHttpResponse response = new TestServerHttpResponse();
IllegalStateException error = new IllegalStateException("boo");
response.writeWith(Flux.error(error)).otherwise(ex -> Mono.empty()).block();
response.writeWith(Flux.error(error)).switchOnError(ex -> Mono.empty()).block();
assertFalse(response.statusCodeWritten);
assertFalse(response.headersWritten);

View File

@@ -125,7 +125,7 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
return Flux.fromIterable(this.handlerMappings)
.concatMap(mapping -> mapping.getHandler(exchange))
.next()
.otherwiseIfEmpty(Mono.error(HANDLER_NOT_FOUND_EXCEPTION))
.switchIfEmpty(Mono.error(HANDLER_NOT_FOUND_EXCEPTION))
.flatMap(handler -> invokeHandler(exchange, handler))
.flatMap(result -> handleResult(exchange, result));
}
@@ -141,7 +141,7 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
private Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
return getResultHandler(result).handleResult(exchange, result)
.otherwise(ex -> result.applyExceptionHandler(ex).flatMap(exceptionResult ->
.switchOnError(ex -> result.applyExceptionHandler(ex).flatMap(exceptionResult ->
getResultHandler(exceptionResult).handleResult(exchange, exceptionResult)));
}

View File

@@ -168,7 +168,7 @@ class DefaultRenderingResponseBuilder implements RenderingResponse.Builder {
return Flux.fromStream(viewResolverStream)
.concatMap(viewResolver -> viewResolver.resolveViewName(name(), locale))
.next()
.otherwiseIfEmpty(Mono.error(new IllegalArgumentException("Could not resolve view with name '" +
.switchIfEmpty(Mono.error(new IllegalArgumentException("Could not resolve view with name '" +
name() +"'")))
.flatMap(view -> view.render(model(), contentType, exchange));
}

View File

@@ -48,7 +48,7 @@ public interface RouterFunction<T extends ServerResponse> {
*/
default RouterFunction<T> and(RouterFunction<T> other) {
return request -> this.route(request)
.otherwiseIfEmpty(Mono.defer(() -> other.route(request)));
.switchIfEmpty(Mono.defer(() -> other.route(request)));
}
/**
@@ -63,7 +63,7 @@ public interface RouterFunction<T extends ServerResponse> {
default RouterFunction<?> andOther(RouterFunction<?> other) {
return request -> this.route(request)
.map(RouterFunctions::cast)
.otherwiseIfEmpty(
.switchIfEmpty(
Mono.defer(() -> other.route(request).map(RouterFunctions::cast)));
}

View File

@@ -234,7 +234,7 @@ public abstract class RouterFunctions {
.defaultIfEmpty(notFound())
.flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request)))
.flatMap(response -> wrapException(() -> response.writeTo(exchange, strategies)))
.otherwise(ResponseStatusException.class,
.switchOnError(ResponseStatusException.class,
ex -> {
exchange.getResponse().setStatusCode(ex.getStatus());
if (ex.getMessage() != null) {

View File

@@ -276,7 +276,7 @@ public class ResourceWebHandler
public Mono<Void> handle(ServerWebExchange exchange) {
return getResource(exchange)
.otherwiseIfEmpty(Mono.defer(() -> {
.switchIfEmpty(Mono.defer(() -> {
logger.trace("No matching resource found - returning 404");
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
return Mono.empty();

View File

@@ -159,7 +159,7 @@ public class VersionResourceResolver extends AbstractResourceResolver {
List<? extends Resource> locations, ResourceResolverChain chain) {
return chain.resolveResource(exchange, requestPath, locations)
.otherwiseIfEmpty(Mono.defer(() ->
.switchIfEmpty(Mono.defer(() ->
resolveVersionedResource(exchange, requestPath, locations, chain)));
}

View File

@@ -75,7 +75,7 @@ public class WebJarsResourceResolver extends AbstractResourceResolver {
List<? extends Resource> locations, ResourceResolverChain chain) {
return chain.resolveResource(exchange, requestPath, locations)
.otherwiseIfEmpty(Mono.defer(() -> {
.switchIfEmpty(Mono.defer(() -> {
String webJarsResourcePath = findWebJarResourcePath(requestPath);
if (webJarsResourcePath != null) {
return chain.resolveResource(exchange, webJarsResourcePath, locations);
@@ -91,7 +91,7 @@ public class WebJarsResourceResolver extends AbstractResourceResolver {
List<? extends Resource> locations, ResourceResolverChain chain) {
return chain.resolveUrlPath(resourceUrlPath, locations)
.otherwiseIfEmpty(Mono.defer(() -> {
.switchIfEmpty(Mono.defer(() -> {
String webJarResourcePath = findWebJarResourcePath(resourceUrlPath);
if (webJarResourcePath != null) {
return chain.resolveUrlPath(webJarResourcePath, locations);

View File

@@ -130,9 +130,9 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
}
else {
Mono<?> mono = reader.readMono(bodyType, elementType, request, response, readHints);
mono = mono.otherwise(ex -> Mono.error(getReadError(bodyParameter, ex)));
mono = mono.switchOnError(ex -> Mono.error(getReadError(bodyParameter, ex)));
if (isBodyRequired || (adapter != null && !adapter.supportsEmpty())) {
mono = mono.otherwiseIfEmpty(Mono.error(getRequiredBodyError(bodyParameter)));
mono = mono.switchIfEmpty(Mono.error(getRequiredBodyError(bodyParameter)));
}
Object[] hints = extractValidationHints(bodyParameter);
if (hints != null) {

View File

@@ -103,7 +103,7 @@ public abstract class AbstractNamedValueArgumentResolver extends HandlerMethodAr
handleResolvedValue(arg, namedValueInfo.name, parameter, model, exchange);
return arg;
})
.otherwiseIfEmpty(getDefaultValue(
.switchIfEmpty(getDefaultValue(
namedValueInfo, parameter, bindingContext, model, exchange));
}

View File

@@ -189,7 +189,7 @@ public class RequestMappingHandlerAdapter implements HandlerAdapter, Application
.then(() -> this.methodResolver.getRequestMappingMethod(handlerMethod)
.invoke(exchange, bindingContext)
.doOnNext(result -> result.setExceptionHandler(exceptionHandler))
.otherwise(exceptionHandler));
.switchOnError(exceptionHandler));
}
private Mono<HandlerResult> handleException(Throwable ex, HandlerMethod handlerMethod,

View File

@@ -193,7 +193,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport
}
return valueMono
.otherwiseIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO)
.switchIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO)
.flatMap(returnValue -> {
Mono<List<View>> viewsMono;

View File

@@ -95,7 +95,7 @@ public class PersonHandler {
Mono<Person> personMono = this.repository.getPerson(personId);
return personMono
.then(person -> ServerResponse.ok().contentType(APPLICATION_JSON).body(fromObject(person)))
.otherwiseIfEmpty(notFound);
.switchIfEmpty(notFound);
}
}
----