diff options
| author | Kevin J Hoerr <khoerr@ksmpartners.com> | 2022-01-27 14:12:29 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <khoerr@ksmpartners.com> | 2022-01-27 14:12:29 -0500 |
| commit | 615c8a0fc8d2b35da7df90115456d33444104c41 (patch) | |
| tree | aeab4261b4edaf63905bd008ce6992b0d2490916 /src/main/java | |
| parent | 8d8d8cdd639591c0c4c49b355db1152e310c22d6 (diff) | |
| download | pantry-615c8a0fc8d2b35da7df90115456d33444104c41.tar.gz pantry-615c8a0fc8d2b35da7df90115456d33444104c41.tar.bz2 pantry-615c8a0fc8d2b35da7df90115456d33444104c41.zip | |
Add error handling
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/dev/submelon/pantry/CustomControllerAdvice.java | 46 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/pantry/ErrorResponse.java | 58 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/pantry/Item.java | 75 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/pantry/ItemRepository.java | 7 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/pantry/PantryItem.java | 40 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/pantry/PantryItemController.java (renamed from src/main/java/dev/submelon/pantry/ItemController.java) | 14 | ||||
| -rw-r--r-- | src/main/java/dev/submelon/pantry/PantryItemRepository.java | 9 |
7 files changed, 160 insertions, 89 deletions
diff --git a/src/main/java/dev/submelon/pantry/CustomControllerAdvice.java b/src/main/java/dev/submelon/pantry/CustomControllerAdvice.java new file mode 100644 index 0000000..dad961b --- /dev/null +++ b/src/main/java/dev/submelon/pantry/CustomControllerAdvice.java @@ -0,0 +1,46 @@ +package dev.submelon.pantry; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.NoSuchElementException; + +@ControllerAdvice +class CustomControllerAdvice { + @ExceptionHandler(NoSuchElementException.class) + public ResponseEntity<ErrorResponse> handleNullPointerExceptions( + NoSuchElementException e + ) { + HttpStatus status = HttpStatus.NOT_FOUND; + + return new ResponseEntity<>( + new ErrorResponse(status, e.getMessage()), + status + ); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity<ErrorResponse> handleExceptions( + Exception e + ) { + HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR; + + // converting the stack trace to String + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + e.printStackTrace(printWriter); + String stackTrace = stringWriter.toString(); + + return new ResponseEntity<>( + new ErrorResponse( + status, + e.getMessage(), + stackTrace // specifying the stack trace in case of 500s + ), + status + ); + } +} diff --git a/src/main/java/dev/submelon/pantry/ErrorResponse.java b/src/main/java/dev/submelon/pantry/ErrorResponse.java new file mode 100644 index 0000000..c5b1dbe --- /dev/null +++ b/src/main/java/dev/submelon/pantry/ErrorResponse.java @@ -0,0 +1,58 @@ +package dev.submelon.pantry; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Getter; +import lombok.Setter; +import org.springframework.http.HttpStatus; +import java.util.Date; + +@Getter +@Setter +public class ErrorResponse { + // customizing timestamp serialization format + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") + private Date timestamp; + + private int code; + + private String status; + + private String message; + + private String stackTrace; + + private Object data; + + public ErrorResponse() { + timestamp = new Date(); + } + + public ErrorResponse(HttpStatus httpStatus, String message) { + this(); + + this.code = httpStatus.value(); + this.status = httpStatus.name(); + this.message = message; + } + + public ErrorResponse( + HttpStatus httpStatus, + String message, + String stackTrace + ) { + this(httpStatus, message); + + this.stackTrace = stackTrace; + } + + public ErrorResponse( + HttpStatus httpStatus, + String message, + String stackTrace, + Object data + ) { + this(httpStatus, message, stackTrace); + + this.data = data; + } +} diff --git a/src/main/java/dev/submelon/pantry/Item.java b/src/main/java/dev/submelon/pantry/Item.java deleted file mode 100644 index a17f5a1..0000000 --- a/src/main/java/dev/submelon/pantry/Item.java +++ /dev/null @@ -1,75 +0,0 @@ -package dev.submelon.pantry; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -public class Item { - @Id - @GeneratedValue(strategy=GenerationType.AUTO) - private Integer id; - - @Column(nullable=false) - private String name; - - private String description; - - @Column(unique=true, nullable=false) - private String shortid; - - @Column(nullable=false) - private double quantity; - - public Item() { - } - - public Item(String name, String description, String shortid, double quantity) { - this.name = name; - this.description = description; - this.shortid = shortid; - this.quantity = quantity; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getShortid() { - return shortid; - } - - public void setShortid(String shortid) { - this.shortid = shortid; - } - - public double getQuantity() { - return quantity; - } - - public void setQuantity(double quantity) { - this.quantity = quantity; - } -} diff --git a/src/main/java/dev/submelon/pantry/ItemRepository.java b/src/main/java/dev/submelon/pantry/ItemRepository.java deleted file mode 100644 index 01451e3..0000000 --- a/src/main/java/dev/submelon/pantry/ItemRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package dev.submelon.pantry; - -import org.springframework.data.repository.CrudRepository; - -public interface ItemRepository extends CrudRepository<Item, Integer> { - Item findByShortid(String shortid); -} diff --git a/src/main/java/dev/submelon/pantry/PantryItem.java b/src/main/java/dev/submelon/pantry/PantryItem.java new file mode 100644 index 0000000..36850bf --- /dev/null +++ b/src/main/java/dev/submelon/pantry/PantryItem.java @@ -0,0 +1,40 @@ +package dev.submelon.pantry; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@Getter +@Setter +@NoArgsConstructor +public class PantryItem { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Integer id; + + @Column(nullable=false) + private String name; + + private String description; + + @Column(unique=true, nullable=false) + private String shortid; + + @Column(nullable=false) + private double quantity; + + public PantryItem(String name, String description, String shortid, double quantity) { + this.name = name; + this.description = description; + this.shortid = shortid; + this.quantity = quantity; + } + +} diff --git a/src/main/java/dev/submelon/pantry/ItemController.java b/src/main/java/dev/submelon/pantry/PantryItemController.java index 7021f0a..37a71e9 100644 --- a/src/main/java/dev/submelon/pantry/ItemController.java +++ b/src/main/java/dev/submelon/pantry/PantryItemController.java @@ -11,33 +11,33 @@ import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(path="/items") -public class ItemController { +public class PantryItemController { @Autowired - private ItemRepository itemRepository; + private PantryItemRepository itemRepository; @PostMapping(path="/add") @ResponseBody Integer addNewItem(@RequestParam String name, @RequestParam String description, @RequestParam String shortid, @RequestParam double quantity) { - Item item = new Item(); + PantryItem item = new PantryItem(); item.setName(name); item.setDescription(description); item.setShortid(shortid); item.setQuantity(quantity); - Item updatedItem = itemRepository.save(item); + PantryItem updatedItem = itemRepository.save(item); return updatedItem.getId(); } @GetMapping(path="") @ResponseBody - Iterable<Item> getAllItems() { + Iterable<PantryItem> getAllItems() { return itemRepository.findAll(); } @GetMapping(path="/{shortid}") @ResponseBody - Item getByShortid(@PathVariable String shortid) { - return itemRepository.findByShortid(shortid); + PantryItem getByShortid(@PathVariable String shortid) { + return itemRepository.findByShortid(shortid).get(); } } diff --git a/src/main/java/dev/submelon/pantry/PantryItemRepository.java b/src/main/java/dev/submelon/pantry/PantryItemRepository.java new file mode 100644 index 0000000..7808186 --- /dev/null +++ b/src/main/java/dev/submelon/pantry/PantryItemRepository.java @@ -0,0 +1,9 @@ +package dev.submelon.pantry; + +import java.util.Optional; + +import org.springframework.data.repository.CrudRepository; + +public interface PantryItemRepository extends CrudRepository<PantryItem, Integer> { + Optional<PantryItem> findByShortid(String shortid); +} |
