From b249dd637713fee56a93347d8f3bd01817713dd6 Mon Sep 17 00:00:00 2001 From: miguel-merlin Date: Sun, 7 Apr 2024 22:44:51 -0400 Subject: [PATCH] feat: Added Global Exceptions --- .../exceptions/GlobalExceptionHandler.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/main/java/com/sitblueprint/admin/controller/exceptions/GlobalExceptionHandler.java diff --git a/src/main/java/com/sitblueprint/admin/controller/exceptions/GlobalExceptionHandler.java b/src/main/java/com/sitblueprint/admin/controller/exceptions/GlobalExceptionHandler.java new file mode 100644 index 0000000..cf9772f --- /dev/null +++ b/src/main/java/com/sitblueprint/admin/controller/exceptions/GlobalExceptionHandler.java @@ -0,0 +1,26 @@ +package com.sitblueprint.admin.controller.exceptions; + +import org.hibernate.service.spi.ServiceException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; + +import java.util.NoSuchElementException; + +public class GlobalExceptionHandler { + @ExceptionHandler + public ResponseEntity handleServiceException(ServiceException e, WebRequest request) { + return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + + @ExceptionHandler + public ResponseEntity handleNoSuchElementException(NoSuchElementException e, WebRequest request) { + return new ResponseEntity("Resource not found", HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleGenericException(Exception e, WebRequest request) { + return new ResponseEntity("An unexpected error has occurred", HttpStatus.INTERNAL_SERVER_ERROR); + } +}