Skip to content

Commit

Permalink
added api and resource not found exceptions with global exception han…
Browse files Browse the repository at this point in the history
…dler
  • Loading branch information
ved-asole committed Feb 28, 2024
1 parent 09562a5 commit c1c9b1d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.vedasole.ekartecommercebackend.exception;

public class APIException extends RuntimeException {

public APIException() { }

public APIException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.vedasole.ekartecommercebackend.exception;

import com.vedasole.ekartecommercebackend.payload.ApiResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ApiResponse> resourceNotFoundExceptionHandler(
ResourceNotFoundException ex
){
String message = ex.getMessage();
ApiResponse apiResponse = new ApiResponse(message, false);

return new ResponseEntity<>(
apiResponse,
HttpStatus.NOT_FOUND
);
}

@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> methodArgumentNotValidExceptionHandler(
MethodArgumentNotValidException ex
){
HashMap<String, String> errorList = new HashMap<>();

ex.getAllErrors().forEach(e -> {
String objectName = ((FieldError)e).getField();
String message = e.getDefaultMessage();
errorList.put(objectName, message);
});

return new ResponseEntity<>(errorList, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(APIException.class)
public ResponseEntity<ApiResponse> APIExceptionHandler(APIException ex) {

String message = ex.getMessage();
ApiResponse apiResponse = new ApiResponse(message, true);

return new ResponseEntity<>(
apiResponse,
HttpStatus.BAD_REQUEST
);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.vedasole.ekartecommercebackend.exception;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ResourceNotFoundException extends RuntimeException {

private String resourceName;
private String fieldName;
private String fieldValue;

public ResourceNotFoundException(String resourceName,
String fieldName,
String fieldValue) {

super(String.format("%s not found with %s : %s",
resourceName, fieldName, fieldValue));

this.setResourceName(resourceName);
this.setFieldName(fieldName);
this.setFieldValue(fieldValue);
}

public ResourceNotFoundException(String resourceName,
String fieldName,
Long fieldValue) {

super(String.format("%s not found with %s : %s",
resourceName, fieldName, fieldValue));

this.setResourceName(resourceName);
this.setFieldName(fieldName);
this.setFieldValue(fieldValue.toString());
}

}

0 comments on commit c1c9b1d

Please sign in to comment.