-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛡️implement exception handling across microservices
- Loading branch information
Adnane Miliari
committed
Nov 29, 2024
1 parent
8506e98
commit dc3e201
Showing
47 changed files
with
782 additions
and
323 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/src/main/java/exceptionhandler/business/CustomerException.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 exceptionhandler.business; | ||
|
||
import exceptionhandler.core.BaseException; | ||
|
||
public class CustomerException extends BaseException { | ||
public CustomerException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
common/src/main/java/exceptionhandler/business/NotificationException.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,9 @@ | ||
package exceptionhandler.business; | ||
|
||
import exceptionhandler.core.BaseException; | ||
|
||
public class NotificationException extends BaseException { | ||
public NotificationException(String message) { | ||
super(message); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
common/src/main/java/exceptionhandler/business/OrderException.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 exceptionhandler.business; | ||
|
||
import exceptionhandler.core.BaseException; | ||
|
||
public class OrderException extends BaseException { | ||
public OrderException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
common/src/main/java/exceptionhandler/business/PaymentException.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 exceptionhandler.business; | ||
|
||
import exceptionhandler.core.BaseException; | ||
|
||
public class PaymentException extends BaseException { | ||
public PaymentException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
9 changes: 9 additions & 0 deletions
9
common/src/main/java/exceptionhandler/business/ProductException.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,9 @@ | ||
package exceptionhandler.business; | ||
|
||
import exceptionhandler.core.BaseException; | ||
|
||
public class ProductException extends BaseException { | ||
public ProductException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
common/src/main/java/exceptionhandler/core/BadRequestException.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,7 @@ | ||
package exceptionhandler.core; | ||
|
||
public class BadRequestException extends BaseException { | ||
public BadRequestException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
common/src/main/java/exceptionhandler/core/BaseException.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,7 @@ | ||
package exceptionhandler.core; | ||
|
||
public abstract class BaseException extends RuntimeException { | ||
public BaseException(String message) { | ||
super(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
common/src/main/java/exceptionhandler/core/DuplicateResourceException.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,8 @@ | ||
package exceptionhandler.core; | ||
|
||
public class DuplicateResourceException extends BaseException { | ||
public DuplicateResourceException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
7 changes: 7 additions & 0 deletions
7
common/src/main/java/exceptionhandler/core/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,7 @@ | ||
package exceptionhandler.core; | ||
|
||
public class ResourceNotFoundException extends BaseException { | ||
public ResourceNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
common/src/main/java/exceptionhandler/core/ValidationException.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,17 @@ | ||
package exceptionhandler.core; | ||
|
||
import exceptionhandler.payload.ValidationError; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class ValidationException extends BaseException { | ||
private final List<ValidationError> errors; | ||
|
||
public ValidationException(String message, List<ValidationError> errors) { | ||
super(message); | ||
this.errors = errors; | ||
} | ||
} | ||
|
115 changes: 115 additions & 0 deletions
115
common/src/main/java/exceptionhandler/handler/RestResponseEntityExceptionHandler.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,115 @@ | ||
package exceptionhandler.handler; | ||
|
||
import exceptionhandler.business.CustomerException; | ||
import exceptionhandler.business.NotificationException; | ||
import exceptionhandler.business.OrderException; | ||
import exceptionhandler.business.PaymentException; | ||
import exceptionhandler.business.ProductException; | ||
import exceptionhandler.core.BaseException; | ||
import exceptionhandler.core.DuplicateResourceException; | ||
import exceptionhandler.core.ResourceNotFoundException; | ||
import exceptionhandler.core.ValidationException; | ||
import exceptionhandler.payload.ErrorCode; | ||
import exceptionhandler.payload.ErrorDetails; | ||
import exceptionhandler.payload.ValidationError; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(ResourceNotFoundException.class) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
public ResponseEntity<ErrorDetails> handleResourceNotFoundException( | ||
ResourceNotFoundException ex, WebRequest request) { | ||
return buildErrorResponse(ex.getMessage(), | ||
ErrorCode.RESOURCE_NOT_FOUND, | ||
HttpStatus.NOT_FOUND, | ||
request); | ||
} | ||
|
||
@ExceptionHandler(ValidationException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ResponseEntity<ErrorDetails> handleValidationException( | ||
ValidationException ex, WebRequest request) { | ||
return buildErrorResponse(ex.getMessage(), | ||
ErrorCode.VALIDATION_FAILED, | ||
HttpStatus.BAD_REQUEST, | ||
request, | ||
ex.getErrors()); | ||
} | ||
|
||
@ExceptionHandler(DuplicateResourceException.class) | ||
@ResponseStatus(HttpStatus.CONFLICT) | ||
public ResponseEntity<ErrorDetails> handleDuplicateResourceException( | ||
DuplicateResourceException ex, WebRequest request) { | ||
return buildErrorResponse(ex.getMessage(), | ||
ErrorCode.DUPLICATE_RESOURCE, | ||
HttpStatus.CONFLICT, | ||
request); | ||
} | ||
|
||
@ExceptionHandler({ | ||
CustomerException.class, | ||
OrderException.class, | ||
PaymentException.class, | ||
ProductException.class, | ||
NotificationException.class | ||
}) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public ResponseEntity<ErrorDetails> handleBusinessExceptions( | ||
BaseException ex, WebRequest request) { | ||
return buildErrorResponse(ex.getMessage(), | ||
ErrorCode.BAD_REQUEST, | ||
HttpStatus.BAD_REQUEST, | ||
request); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public ResponseEntity<ErrorDetails> handleAllUncaughtException( | ||
Exception ex, WebRequest request) { | ||
return buildErrorResponse(ex.getMessage(), | ||
ErrorCode.INTERNAL_ERROR, | ||
HttpStatus.INTERNAL_SERVER_ERROR, | ||
request); | ||
} | ||
|
||
private ResponseEntity<ErrorDetails> buildErrorResponse( | ||
String message, | ||
ErrorCode errorCode, | ||
HttpStatus status, | ||
WebRequest request) { | ||
return buildErrorResponse(message, errorCode, status, request, null); | ||
} | ||
|
||
private ResponseEntity<ErrorDetails> buildErrorResponse( | ||
String message, | ||
ErrorCode errorCode, | ||
HttpStatus status, | ||
WebRequest request, | ||
List<ValidationError> errors) { | ||
|
||
ErrorDetails errorDetails = ErrorDetails.builder() | ||
.timestamp(LocalDateTime.now()) | ||
.code(errorCode.getCode()) | ||
.message(message) | ||
.path(((ServletWebRequest) request).getRequest().getRequestURI()) | ||
.errors(errors) | ||
.build(); | ||
|
||
return new ResponseEntity<>(errorDetails, status); | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
common/src/main/java/exceptionhandler/payload/ErrorCode.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,20 @@ | ||
package exceptionhandler.payload; | ||
|
||
public enum ErrorCode { | ||
RESOURCE_NOT_FOUND("ERR_001"), | ||
VALIDATION_FAILED("ERR_002"), | ||
DUPLICATE_RESOURCE("ERR_003"), | ||
BAD_REQUEST("ERR_004"), | ||
INTERNAL_ERROR("ERR_005"); | ||
|
||
private final String code; | ||
|
||
ErrorCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
common/src/main/java/exceptionhandler/payload/ErrorDetails.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,18 @@ | ||
package exceptionhandler.payload; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Builder | ||
public class ErrorDetails { | ||
private LocalDateTime timestamp; | ||
private String code; | ||
private String message; | ||
private String path; | ||
private List<ValidationError> errors; | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
common/src/main/java/exceptionhandler/payload/ValidationError.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,12 @@ | ||
package exceptionhandler.payload; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class ValidationError { | ||
private String field; | ||
private String message; | ||
} | ||
|
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
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
Oops, something went wrong.