Skip to content

Commit 0da539a

Browse files
Removed all Error Handling in Controller Methods
1 parent 6d89c76 commit 0da539a

File tree

3 files changed

+10
-21
lines changed

3 files changed

+10
-21
lines changed

src/main/java/com/answerdigital/answerking/controller/CategoryController.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.springframework.beans.factory.annotation.Autowired;
1515
import org.springframework.http.HttpStatus;
1616
import org.springframework.http.ResponseEntity;
17-
import org.springframework.validation.Errors;
1817
import org.springframework.validation.annotation.Validated;
1918
import org.springframework.web.bind.annotation.PathVariable;
2019
import org.springframework.web.bind.annotation.RequestBody;
@@ -50,10 +49,8 @@ public CategoryController(final CategoryService categoryService) {
5049
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) })
5150
})
5251
@PostMapping
53-
public ResponseEntity<Category> addCategory(@Valid @RequestBody final AddCategoryRequest categoryRequest,
54-
final Errors errors) {
55-
return new ResponseEntity<>(categoryService.addCategory(categoryRequest),
56-
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.CREATED);
52+
public ResponseEntity<Category> addCategory(@Valid @RequestBody final AddCategoryRequest categoryRequest) {
53+
return new ResponseEntity<>(categoryService.addCategory(categoryRequest), HttpStatus.CREATED);
5754
}
5855

5956
@Operation(summary = "Get all categories.")
@@ -112,10 +109,8 @@ public ResponseEntity<Category> removeProductFromCategory(@PathVariable @NotNull
112109
})
113110
@PutMapping("/{categoryId}")
114111
public ResponseEntity<Category> updateCategory(@Valid @RequestBody final UpdateCategoryRequest updateCategoryRequest,
115-
@PathVariable @NotNull final Long categoryId,
116-
final Errors errors) {
117-
return new ResponseEntity<>(categoryService.updateCategory(updateCategoryRequest, categoryId),
118-
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
112+
@PathVariable @NotNull final Long categoryId) {
113+
return new ResponseEntity<>(categoryService.updateCategory(updateCategoryRequest, categoryId), HttpStatus.OK);
119114
}
120115

121116
@Operation(summary = "Retire an existing category.")

src/main/java/com/answerdigital/answerking/controller/OrderController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.http.HttpStatus;
99
import org.springframework.http.ResponseEntity;
10-
import org.springframework.validation.Errors;
1110
import org.springframework.validation.annotation.Validated;
1211
import org.springframework.web.bind.annotation.GetMapping;
1312
import org.springframework.web.bind.annotation.PostMapping;
@@ -36,9 +35,8 @@ public OrderController(final OrderService orderService) {
3635
}
3736

3837
@PostMapping
39-
public ResponseEntity<Order> addOrder(@Valid @RequestBody final OrderRequest orderRequest, final Errors errors) {
40-
return new ResponseEntity<>(orderService.addOrder(orderRequest),
41-
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.CREATED);
38+
public ResponseEntity<Order> addOrder(@Valid @RequestBody final OrderRequest orderRequest) {
39+
return new ResponseEntity<>(orderService.addOrder(orderRequest), HttpStatus.CREATED);
4240
}
4341

4442
@GetMapping(path = "/{orderId}")

src/main/java/com/answerdigital/answerking/controller/ProductController.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.springframework.beans.factory.annotation.Autowired;
1414
import org.springframework.http.HttpStatus;
1515
import org.springframework.http.ResponseEntity;
16-
import org.springframework.validation.Errors;
1716
import org.springframework.validation.annotation.Validated;
1817
import org.springframework.web.bind.annotation.DeleteMapping;
1918
import org.springframework.web.bind.annotation.GetMapping;
@@ -70,9 +69,8 @@ public ResponseEntity<Product> getProductById(@Valid @PathVariable @NotNull fina
7069
content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)) })
7170
})
7271
@PostMapping
73-
public ResponseEntity<Product> addProduct(@Valid @RequestBody final ProductRequest productRequest, final Errors errors) {
74-
return new ResponseEntity<>(productService.addNewProduct(productRequest),
75-
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.CREATED);
72+
public ResponseEntity<Product> addProduct(@Valid @RequestBody final ProductRequest productRequest) {
73+
return new ResponseEntity<>(productService.addNewProduct(productRequest), HttpStatus.CREATED);
7674
}
7775

7876
@Operation(summary = "Update an existing product.")
@@ -86,10 +84,8 @@ public ResponseEntity<Product> addProduct(@Valid @RequestBody final ProductReque
8684
})
8785
@PutMapping("/{id}")
8886
public ResponseEntity<Product> updateProduct(@PathVariable @NotNull final Long id,
89-
@Valid @RequestBody final ProductRequest productRequest,
90-
final Errors errors) {
91-
return new ResponseEntity<>(productService.updateProduct(id, productRequest),
92-
errors.hasErrors() ? HttpStatus.BAD_REQUEST : HttpStatus.OK);
87+
@Valid @RequestBody final ProductRequest productRequest) {
88+
return new ResponseEntity<>(productService.updateProduct(id, productRequest), HttpStatus.OK);
9389
}
9490

9591
@Operation(summary = "Retire an existing product.")

0 commit comments

Comments
 (0)