Commit be80c860 authored by Ben Anderson's avatar Ben Anderson

Implemented error handling for unique key violations

parent 06946133
package com.nisum.ascend.inventory.controller;
import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.exception.ResourceAlreadyExistsException;
import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import com.nisum.ascend.inventory.service.ProductService;
......@@ -19,8 +21,7 @@ public class ProductController {
ProductService productService;
@PostMapping("")
public ResponseEntity<Mono<ProductDto>> postProduct(@RequestBody Product product) {
Mono<ProductDto> newProduct = productService.createProduct(product);
return new ResponseEntity<>(newProduct, HttpStatus.ACCEPTED);
public Mono<ProductDto> postProduct(@RequestBody Product product) {
return productService.createProduct(product).onErrorMap(throwable -> new ResourceAlreadyExistsException());
}
}
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ public class ProductDto {
private int stock;
private PromotionDto promotion;
public ProductDto() {}
public static ProductDto generateDtoFromProduct(Product product) {
// TODO: Fetch the product's promotion from the Promotions com.nisum.ascend.inventory.service
......
package com.nisum.ascend.inventory.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
public class ResourceAlreadyExistsException extends RuntimeException {
public ResourceAlreadyExistsException() {
super("A resource with the provided SKU already exists");
}
}
package com.nisum.ascend.inventory.exception;
import com.mongodb.DuplicateKeyException;
import org.springframework.core.annotation.Order;
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.Mono;
@Component
@Order(-2)
class RestWebExceptionHandler implements WebExceptionHandler {
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (ex instanceof ResourceAlreadyExistsException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
// marks the response as complete and forbids writing to it
return exchange.getResponse().setComplete();
}
return Mono.error(ex);
}
}
package com.nisum.ascend.inventory.service;
import com.nisum.ascend.inventory.dto.ProductDto;
import com.nisum.ascend.inventory.exception.ResourceAlreadyExistsException;
import com.nisum.ascend.inventory.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import com.nisum.ascend.inventory.repository.ProductRepository;
......@@ -13,9 +15,7 @@ public class ProductService {
ProductRepository productRepository;
public Mono<ProductDto> createProduct(Product product) {
return productRepository.save(product)
.map(ProductDto::generateDtoFromProduct)
.map(ProductDto::generateDtoFromProduct);
}
}
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