From b4ea8a029a414227aa3f6288c792df37fc989575 Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Wed, 4 Sep 2024 12:40:01 +0200 Subject: [PATCH] 17 --- .../de/szut/store/errors/ErrorDetails.java | 14 ++++++++++ .../store/errors/GlobalExceptionHandler.java | 26 +++++++++++++++++++ .../errors/ResourceNotFoundException.java | 9 +++++++ .../szut/store/supplier/SupplierService.java | 8 +++++- .../szut/webshop/WebshopApplicationTests.java | 13 ---------- 5 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 src/main/java/de/szut/store/errors/ErrorDetails.java create mode 100644 src/main/java/de/szut/store/errors/GlobalExceptionHandler.java create mode 100644 src/main/java/de/szut/store/errors/ResourceNotFoundException.java delete mode 100644 src/test/java/de/szut/webshop/WebshopApplicationTests.java diff --git a/src/main/java/de/szut/store/errors/ErrorDetails.java b/src/main/java/de/szut/store/errors/ErrorDetails.java new file mode 100644 index 0000000..9338b96 --- /dev/null +++ b/src/main/java/de/szut/store/errors/ErrorDetails.java @@ -0,0 +1,14 @@ +package de.szut.store.errors; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.Date; + +@Data +@AllArgsConstructor +public class ErrorDetails { + private Date timestamp; + private String message; + private String details; +} diff --git a/src/main/java/de/szut/store/errors/GlobalExceptionHandler.java b/src/main/java/de/szut/store/errors/GlobalExceptionHandler.java new file mode 100644 index 0000000..3f3281b --- /dev/null +++ b/src/main/java/de/szut/store/errors/GlobalExceptionHandler.java @@ -0,0 +1,26 @@ +package de.szut.store.errors; + +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 org.springframework.web.context.request.WebRequest; + +import java.util.Date; + +@ControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(ResourceNotFoundException.class) + public ResponseEntity handleResourceNotFoundException(ResourceNotFoundException exception, WebRequest request) { + ErrorDetails errorDetails = new ErrorDetails(new Date(), exception.getMessage(), request.getDescription(false)); + return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception exception, WebRequest request) { + System.out.println(exception.getMessage()); + ErrorDetails errorDetails = new ErrorDetails(new Date(), exception.getMessage(), request.getDescription(false)); + return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR); + } +} diff --git a/src/main/java/de/szut/store/errors/ResourceNotFoundException.java b/src/main/java/de/szut/store/errors/ResourceNotFoundException.java new file mode 100644 index 0000000..bc122de --- /dev/null +++ b/src/main/java/de/szut/store/errors/ResourceNotFoundException.java @@ -0,0 +1,9 @@ +package de.szut.store.errors; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class ResourceNotFoundException extends RuntimeException{ + public ResourceNotFoundException(String message) {super(message);} +} diff --git a/src/main/java/de/szut/store/supplier/SupplierService.java b/src/main/java/de/szut/store/supplier/SupplierService.java index 7e66bb0..d048dda 100644 --- a/src/main/java/de/szut/store/supplier/SupplierService.java +++ b/src/main/java/de/szut/store/supplier/SupplierService.java @@ -1,9 +1,11 @@ package de.szut.store.supplier; +import de.szut.store.errors.ResourceNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; @Service public class SupplierService { @@ -36,7 +38,11 @@ public class SupplierService { } public Supplier readById(Long id) { - return repository.findById(id).orElse(null); + Optional supplier= repository.findById(id); + if (supplier.isEmpty()){ + throw new ResourceNotFoundException("Supplier with id " + id + " not found"); + } + return supplier.get(); } public void delete(Long id) { diff --git a/src/test/java/de/szut/webshop/WebshopApplicationTests.java b/src/test/java/de/szut/webshop/WebshopApplicationTests.java deleted file mode 100644 index 10aa34a..0000000 --- a/src/test/java/de/szut/webshop/WebshopApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.szut.webshop; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class WebshopApplicationTests { - - @Test - void contextLoads() { - } - -}