-
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.
feat: new approach for Exception Handling using sealed interfaces
- Loading branch information
1 parent
bd6fa05
commit 6cbf202
Showing
16 changed files
with
146 additions
and
56 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...dev/earlspilner/users/service/KeyGen.java → ...er/users/configuration/KeyGeneration.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
35 changes: 35 additions & 0 deletions
35
...-api-users-service/src/main/java/dev/earlspilner/users/configuration/ResponseHandler.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,35 @@ | ||
package dev.earlspilner.users.configuration; | ||
|
||
import dev.earlspilner.users.rest.advice.Failure; | ||
import dev.earlspilner.users.rest.advice.Result; | ||
import dev.earlspilner.users.rest.advice.Success; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
@Component | ||
public class ResponseHandler { | ||
|
||
public <T> ResponseEntity<?> handleResult(Result<T> result) { | ||
if (result instanceof Success<T> success) { | ||
return new ResponseEntity<>(success.value(), HttpStatus.OK); | ||
} else if (result instanceof Failure<?> failure) { | ||
return new ResponseEntity<>(failure.toProblemDetail(), HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
public <T> ResponseEntity<?> handleResult(Result<T> result, HttpStatus httpStatus) { | ||
if (result instanceof Success<T> success) { | ||
return new ResponseEntity<>(success.value(), HttpStatus.OK); | ||
} else if (result instanceof Failure<?> failure) { | ||
return new ResponseEntity<>(failure.toProblemDetail(), httpStatus); | ||
} | ||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
library-api-users-service/src/main/java/dev/earlspilner/users/rest/advice/Failure.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,15 @@ | ||
package dev.earlspilner.users.rest.advice; | ||
|
||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ProblemDetail; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
public record Failure<T>(String exMessage, int httpStatus) implements Result<T> { | ||
|
||
public ProblemDetail toProblemDetail() { | ||
return ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(httpStatus), exMessage); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
library-api-users-service/src/main/java/dev/earlspilner/users/rest/advice/Result.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,6 @@ | ||
package dev.earlspilner.users.rest.advice; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
public sealed interface Result<T> permits Success, Failure { } |
6 changes: 6 additions & 0 deletions
6
library-api-users-service/src/main/java/dev/earlspilner/users/rest/advice/Success.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,6 @@ | ||
package dev.earlspilner.users.rest.advice; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
public record Success<T>(T value) implements Result<T> { } |
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
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
3 changes: 2 additions & 1 deletion
3
...est/advice/custom/CustomJwtException.java → ...s/rest/old/custom/CustomJwtException.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
3 changes: 2 additions & 1 deletion
3
...ustom/UnauthorizedOperationException.java → ...ustom/UnauthorizedOperationException.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
3 changes: 2 additions & 1 deletion
3
...st/advice/custom/UserExistsException.java → .../rest/old/custom/UserExistsException.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
3 changes: 2 additions & 1 deletion
3
.../advice/custom/UserNotFoundException.java → ...est/old/custom/UserNotFoundException.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
2 changes: 1 addition & 1 deletion
2
library-api-users-service/src/main/java/dev/earlspilner/users/security/JwtTokenFilter.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
2 changes: 1 addition & 1 deletion
2
library-api-users-service/src/main/java/dev/earlspilner/users/security/JwtTokenProvider.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
11 changes: 6 additions & 5 deletions
11
library-api-users-service/src/main/java/dev/earlspilner/users/service/UserService.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 |
---|---|---|
@@ -1,16 +1,17 @@ | ||
package dev.earlspilner.users.service; | ||
|
||
import dev.earlspilner.users.dto.UserDto; | ||
import dev.earlspilner.users.rest.advice.Result; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
/** | ||
* @author Alexander Dudkin | ||
*/ | ||
public interface UserService { | ||
UserDto saveUser(UserDto dto); | ||
UserDto getUser(String username); | ||
Page<UserDto> getUsers(Pageable pageable); | ||
UserDto updateUser(String username, UserDto dto); | ||
void deleteUser(Integer id); | ||
Result<UserDto> saveUser(UserDto dto); | ||
Result<UserDto> getUser(String username); | ||
Page<Result<UserDto>> getUsers(Pageable pageable); | ||
Result<UserDto> updateUser(String username, UserDto dto); | ||
Result<Void> deleteUser(Integer id); | ||
} |
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