Skip to content

Commit

Permalink
feat: Added Global Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-merlin committed Apr 8, 2024
1 parent dcd75fd commit b249dd6
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit b249dd6

Please sign in to comment.