-
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.
- Loading branch information
1 parent
6f7ad71
commit 9935e86
Showing
3 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/com/zenjob/challenge/exceptionhandler/ErrorResponseDto.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,11 @@ | ||
package com.zenjob.challenge.exceptionhandler; | ||
|
||
import lombok.*; | ||
|
||
@Getter | ||
@Setter | ||
@RequiredArgsConstructor | ||
public class ErrorResponseDto { | ||
private final int statusCode; | ||
private final String message; | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/zenjob/challenge/exceptionhandler/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,39 @@ | ||
package com.zenjob.challenge.exceptionhandler; | ||
|
||
import com.zenjob.challenge.customexception.InvalidActionException; | ||
import com.zenjob.challenge.customexception.InvalidEndDateException; | ||
import com.zenjob.challenge.customexception.InvalidStartDateException; | ||
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 org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@RestControllerAdvice | ||
class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
@ExceptionHandler(InvalidStartDateException.class) | ||
ResponseEntity<Object> handleCustomException(InvalidStartDateException ex) { | ||
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.BAD_REQUEST.value(), ex.getMessage()); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(InvalidEndDateException.class) | ||
ResponseEntity<Object> handleCustomException(InvalidEndDateException ex) { | ||
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.BAD_REQUEST.value(), ex.getMessage()); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(InvalidActionException.class) | ||
ResponseEntity<Object> handleCustomException(InvalidActionException ex) { | ||
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.METHOD_NOT_ALLOWED.value(), ex.getMessage()); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.METHOD_NOT_ALLOWED); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
ResponseEntity<Object> handleAllExceptions(Exception ex) { | ||
ErrorResponseDto errorResponse = new ErrorResponseDto(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage()); | ||
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
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