Commit ae8d9aba authored by Ben Anderson's avatar Ben Anderson

Added error message to error response

parent 1e87cd4f
......@@ -32,7 +32,7 @@ public class PromotionsController {
public Mono<PromotionDto> getPromotionById(@PathVariable String id){
return promotionService
.findPromoById(id)
.onErrorMap(throwable -> new PromotionNotFoundException(id));
.switchIfEmpty(Mono.error(new PromotionNotFoundException(id)));
}
@PostMapping()
......
package com.nisum.ascend.promotions.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class PromotionNotFoundException extends RuntimeException {
public PromotionNotFoundException(String id) {
super("The promotion with the ID of " + id + " was not found.");
......
package com.nisum.ascend.promotions.exception;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import springfox.documentation.spring.web.json.Json;
import java.nio.charset.StandardCharsets;
@Component
@Order(-2)
......@@ -15,9 +20,9 @@ class RestWebExceptionHandler implements WebExceptionHandler {
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof PromotionNotFoundException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(ex.getMessage().getBytes(StandardCharsets.UTF_8));
// marks the response as complete and forbids writing to it
return exchange.getResponse().setComplete();
return exchange.getResponse().writeWith(Flux.just(buffer));
}
return Mono.error(ex);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment