-
Notifications
You must be signed in to change notification settings - Fork 1
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
3a62904
commit dd8c8fe
Showing
10 changed files
with
553 additions
and
19 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/main/java/by/andd3dfx/templateapp/error/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,60 @@ | ||
package by.andd3dfx.templateapp.error; | ||
|
||
import by.andd3dfx.templateapp.error.dto.ExceptionResponse; | ||
import by.andd3dfx.templateapp.error.exception.NotFoundException; | ||
import by.andd3dfx.templateapp.error.exception.UnauthorizedException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* Add more methods if needed. | ||
*/ | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
/** | ||
* 401. | ||
*/ | ||
@ExceptionHandler(UnauthorizedException.class) | ||
public ResponseEntity<ExceptionResponse> handleUnauthorizedException(UnauthorizedException ex) { | ||
return buildResponseEntity(ex, HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
/** | ||
* 404. | ||
*/ | ||
@ExceptionHandler(NotFoundException.class) | ||
public ResponseEntity handleResourceNotFoundException(NotFoundException ex) { | ||
return buildResponseEntity(ex, HttpStatus.NOT_FOUND); | ||
} | ||
|
||
/** | ||
* 409. | ||
*/ | ||
@ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class}) | ||
public ResponseEntity handleIllegalExceptions(RuntimeException ex) { | ||
return buildResponseEntity(ex, HttpStatus.CONFLICT); | ||
} | ||
|
||
/** | ||
* 500. | ||
* Throw exception for any other unpredicted reason. | ||
* Avoid modification of this method to make problem visible in logs. | ||
* Don't delete this method to avoid descendants declare it and catch 'any error'. | ||
*/ | ||
@ExceptionHandler(Exception.class) | ||
public final ResponseEntity handleOtherExceptions(Exception ex) throws Exception { | ||
throw ex; | ||
} | ||
|
||
private ResponseEntity<ExceptionResponse> buildResponseEntity(Exception ex, HttpStatus httpStatus) { | ||
return ResponseEntity | ||
.status(httpStatus) | ||
.body(new ExceptionResponse(ex.getMessage(), httpStatus.name(), LocalDateTime.now())); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/by/andd3dfx/templateapp/error/dto/ExceptionResponse.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 by.andd3dfx.templateapp.error.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ExceptionResponse { | ||
|
||
private String message; | ||
private String code; | ||
private LocalDateTime timestamp; | ||
} |
2 changes: 1 addition & 1 deletion
2
.../exceptions/ArticleNotFoundException.java → ...r/exception/ArticleNotFoundException.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
8 changes: 8 additions & 0 deletions
8
src/main/java/by/andd3dfx/templateapp/error/exception/NotFoundException.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 by.andd3dfx.templateapp.error.exception; | ||
|
||
public abstract class NotFoundException extends RuntimeException { | ||
|
||
public NotFoundException(String name, Long id) { | ||
super(String.format("Could not find %s by id=%d", name, id)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/by/andd3dfx/templateapp/error/exception/UnauthorizedException.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 by.andd3dfx.templateapp.error.exception; | ||
|
||
public class UnauthorizedException extends RuntimeException { | ||
|
||
public UnauthorizedException(String message) { | ||
super(message); | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
src/main/java/by/andd3dfx/templateapp/exceptions/NotFoundException.java
This file was deleted.
Oops, something went wrong.
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.