Add Global Exception Handler Classes and OrderGlobalException.java classes
This commit is contained in:
26
common/common-application/pom.xml
Normal file
26
common/common-application/pom.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>common</artifactId>
|
||||
<groupId>com.food.order</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-application</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.food.order.system.application.handler;
|
||||
|
||||
import lombok.Builder;
|
||||
|
||||
@Builder
|
||||
public record ErrorDTO(String code, String message) {
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.food.order.system.application.handler;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.ValidationException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
@ExceptionHandler(value = {Exception.class})
|
||||
@ResponseBody
|
||||
public ErrorDTO handleOrderDomainException(Exception e) {
|
||||
log.error("Error occurred: {}", e.getMessage());
|
||||
return ErrorDTO.builder()
|
||||
.code(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase())
|
||||
.message("Unknown error occurred")
|
||||
.build();
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(value = {ValidationException.class})
|
||||
@ResponseBody
|
||||
public ErrorDTO handleOrderDomainException(ValidationException e) {
|
||||
ErrorDTO errorDTO;
|
||||
if (e instanceof ConstraintViolationException) {
|
||||
String violations = extractViolationsFromException((ConstraintViolationException) e);
|
||||
log.error("Error occurred: {}", violations);
|
||||
errorDTO = ErrorDTO.builder()
|
||||
.code(HttpStatus.BAD_REQUEST.getReasonPhrase())
|
||||
.message(violations)
|
||||
.build();
|
||||
}
|
||||
else {
|
||||
log.error("Error occurred: {}", e.getMessage());
|
||||
errorDTO = ErrorDTO.builder()
|
||||
.code(HttpStatus.BAD_REQUEST.getReasonPhrase())
|
||||
.message(e.getMessage())
|
||||
.build();
|
||||
}
|
||||
return errorDTO;
|
||||
}
|
||||
|
||||
private String extractViolationsFromException(ConstraintViolationException e) {
|
||||
return e.getConstraintViolations()
|
||||
.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.joining("->"));
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>common-domain</module>
|
||||
<module>common-application</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
Reference in New Issue
Block a user