-
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.
feature: Add unified global exception handling using AlertDto
Task: 8696zwhj1
- Loading branch information
Showing
16 changed files
with
389 additions
and
86 deletions.
There are no files selected for viewing
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
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
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/meowhub/backend/shared/constants/AlertConstants.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,20 @@ | ||
package meowhub.backend.shared.constants; | ||
|
||
public class AlertConstants { | ||
//title | ||
public static final String USER_WITH_LOGIN_NOT_FOUND_TITLE = "User not found"; | ||
public static final String RESOURCE_NOT_FOUND_TITLE = "Resource not found"; | ||
public static final String NOT_UNIQUE_OBJECT_TITLE = "Not unique object"; | ||
public static final String ILLEGAL_ARGUMENT_TITLE = "Illegal argument"; | ||
|
||
//message | ||
public static final String USER_WITH_LOGIN_NOT_FOUND = "User with login '%s' not found"; | ||
public static final String RESOURCE_NOT_FOUND = "%s not found for %s = '%s'"; | ||
public static final String UNKNOWN_ERROR = "Unknown error"; | ||
public static final String BAD_CREDENTIALS = "Bad credentials"; | ||
public static final String NOT_UNIQUE_OBJECT = "%s:'%s' is not unique"; | ||
public static final String ILLEGAL_ARGUMENT = "%s cannot be equal %s"; | ||
|
||
private AlertConstants() { | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
backend/src/main/java/meowhub/backend/shared/constants/AlertLevel.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,5 @@ | ||
package meowhub.backend.shared.constants; | ||
|
||
public enum AlertLevel { | ||
WARNING, INFO, ERROR | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/meowhub/backend/shared/dtos/AlertDto.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,20 @@ | ||
package meowhub.backend.shared.dtos; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import meowhub.backend.shared.constants.AlertLevel; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class AlertDto { | ||
private String title; | ||
private String message; | ||
private AlertLevel level; | ||
private LocalDateTime timestamp; | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/meowhub/backend/shared/exceptions/NotUniqueObjectException.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,7 @@ | ||
package meowhub.backend.shared.exceptions; | ||
|
||
public class NotUniqueObjectException extends RuntimeException { | ||
public NotUniqueObjectException(String message) { | ||
super(message); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
backend/src/main/java/meowhub/backend/shared/handlers/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,48 @@ | ||
package meowhub.backend.shared.handlers; | ||
|
||
import meowhub.backend.shared.dtos.AlertDto; | ||
import meowhub.backend.shared.exceptions.NotUniqueObjectException; | ||
import meowhub.backend.shared.utils.AlertUtils; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.webjars.NotFoundException; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(UsernameNotFoundException.class) | ||
public ResponseEntity<AlertDto> handleUsernameNotFoundException(UsernameNotFoundException ex) { | ||
return new ResponseEntity<>(AlertUtils.userNotFoundException(ex.getMessage()), HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler(NotUniqueObjectException.class) | ||
public ResponseEntity<Object> handleNotUniqueObjectException(NotUniqueObjectException ex) { | ||
return new ResponseEntity<>(AlertUtils.notUniqueObjectException(ex.getMessage()), HttpStatus.NOT_ACCEPTABLE); | ||
} | ||
|
||
@ExceptionHandler(NotFoundException.class) | ||
public ResponseEntity<AlertDto> handleNotFoundException(NotFoundException ex) { | ||
return new ResponseEntity<>(AlertUtils.resourceNotFoundException(ex.getMessage()), HttpStatus.NOT_FOUND); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<AlertDto> handleUnknownException(Exception ex) { | ||
ex.printStackTrace(); | ||
return new ResponseEntity<>(AlertUtils.unknownException(), HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
@ExceptionHandler(BadCredentialsException.class) | ||
public ResponseEntity<AlertDto> handleBadCredentialsException() { | ||
return new ResponseEntity<>(AlertUtils.badCredentialsException(), HttpStatus.UNAUTHORIZED); | ||
} | ||
|
||
@ExceptionHandler(IllegalArgumentException.class) | ||
public ResponseEntity<AlertDto> handleIllegalArgumentException(IllegalArgumentException ex) { | ||
return new ResponseEntity<>(AlertUtils.illegalArgumentException(ex.getMessage()), HttpStatus.NOT_ACCEPTABLE); | ||
} | ||
|
||
} |
Oops, something went wrong.