-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added api and resource not found exceptions with global exception han…
…dler
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/com/vedasole/ekartecommercebackend/exception/APIException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/vedasole/ekartecommercebackend/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
|
||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/vedasole/ekartecommercebackend/exception/ResourceNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |