Skip to content

Commit

Permalink
Polish "Throw 404 ResponseStatusException when no routes found"
Browse files Browse the repository at this point in the history
  • Loading branch information
poutsma committed Dec 3, 2021
1 parent 69df27a commit b84fe99
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ public Mono<Void> handle(ServerWebExchange exchange) {
ServerRequest request = new DefaultServerRequest(exchange, this.strategies.messageReaders());
addAttributes(exchange, request);
return this.routerFunction.route(request)
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
.switchIfEmpty(createNotFoundError())
.flatMap(handlerFunction -> wrapException(() -> handlerFunction.handle(request)))
.flatMap(response -> wrapException(() -> response.writeTo(exchange,
new HandlerStrategiesResponseContext(this.strategies))));
Expand All @@ -1260,6 +1260,11 @@ private void addAttributes(ServerWebExchange exchange, ServerRequest request) {
attributes.put(REQUEST_ATTRIBUTE, request);
}

private <R> Mono<R> createNotFoundError() {
return Mono.defer(() -> Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND,
"No matching router function")));
}

private static <T> Mono<T> wrapException(Supplier<Mono<T>> supplier) {
try {
return supplier.get();
Expand Down

0 comments on commit b84fe99

Please sign in to comment.