-
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.
Merge pull request #112 from szymonpoltorak/DEV-194-Exception-handler
DEV-194 Added abstract exception handler
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...backend/src/main/java/dev/corn/cornbackend/utils/exceptions/AbstractExceptionHandler.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,37 @@ | ||
package dev.corn.cornbackend.utils.exceptions; | ||
|
||
import dev.corn.cornbackend.utils.exceptions.data.AbstractExceptionResponse; | ||
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.RestControllerAdvice; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
public class AbstractExceptionHandler { | ||
|
||
private static final String EXCEPTION_OCCURRED_MESSAGE = "Exception occurred with status: {} and response: {}"; | ||
|
||
@ExceptionHandler(AbstractException.class) | ||
public final ResponseEntity<AbstractExceptionResponse> handleAbstractException(AbstractException exception) { | ||
AbstractExceptionResponse response = buildExceptionResponse(exception); | ||
|
||
HttpStatus status = HttpStatus.valueOf(exception.getStatusCode().value()); | ||
|
||
log.error(EXCEPTION_OCCURRED_MESSAGE, status.value(), response); | ||
log.error("", exception); | ||
|
||
return ResponseEntity.status(status).body(response); | ||
} | ||
|
||
private AbstractExceptionResponse buildExceptionResponse(Exception exception) { | ||
return AbstractExceptionResponse.builder() | ||
.exceptionMessage(exception.getMessage()) | ||
.exceptionName(exception.getClass().getSimpleName()) | ||
.timeStamp(LocalDateTime.now()) | ||
.build(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...d/src/main/java/dev/corn/cornbackend/utils/exceptions/data/AbstractExceptionResponse.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 dev.corn.cornbackend.utils.exceptions.data; | ||
|
||
import lombok.Builder; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Builder | ||
public record AbstractExceptionResponse(String exceptionName, String exceptionMessage, LocalDateTime timeStamp){ | ||
} |