From a4bdcda16508213abb6062c0bc80ad032ac6121c Mon Sep 17 00:00:00 2001 From: KamilM1205 Date: Sun, 29 Jun 2025 23:52:52 +0400 Subject: [PATCH] Add: Response exceptions. Handling exceptions. --- .../controller/UserController.java | 69 +++++++++++++++++-- .../controller/advice/ErrorDetails.java | 13 ++++ .../UserControllerExceptionHandler.java | 40 +++++++++++ .../exceptions/BadRequestException.java | 4 ++ .../exceptions/ForbiddenException.java | 4 ++ .../exceptions/UnauthorizedException.java | 4 ++ .../UserAlreadyExistsException.java | 4 ++ .../exceptions/UserNotFoundException.java | 4 ++ 8 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ru/team58/profileservice/controller/advice/ErrorDetails.java create mode 100644 src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java create mode 100644 src/main/java/ru/team58/profileservice/controller/exceptions/BadRequestException.java create mode 100644 src/main/java/ru/team58/profileservice/controller/exceptions/ForbiddenException.java create mode 100644 src/main/java/ru/team58/profileservice/controller/exceptions/UnauthorizedException.java create mode 100644 src/main/java/ru/team58/profileservice/controller/exceptions/UserAlreadyExistsException.java create mode 100644 src/main/java/ru/team58/profileservice/controller/exceptions/UserNotFoundException.java diff --git a/src/main/java/ru/team58/profileservice/controller/UserController.java b/src/main/java/ru/team58/profileservice/controller/UserController.java index 70378ed..2c18b1b 100644 --- a/src/main/java/ru/team58/profileservice/controller/UserController.java +++ b/src/main/java/ru/team58/profileservice/controller/UserController.java @@ -2,6 +2,9 @@ package ru.team58.profileservice.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import ru.team58.profileservice.controller.exceptions.BadRequestException; +import ru.team58.profileservice.controller.exceptions.UnauthorizedException; +import ru.team58.profileservice.controller.exceptions.UserNotFoundException; import ru.team58.profileservice.controller.schemes.CreateUserRequest; import ru.team58.profileservice.controller.schemes.GetUserResponse; import ru.team58.profileservice.controller.schemes.UpdateUserRequest; @@ -9,6 +12,7 @@ import ru.team58.profileservice.controller.schemes.UserResponse; import ru.team58.profileservice.mapper.UserMapper; import ru.team58.profileservice.service.UserDTO; import ru.team58.profileservice.service.UserService; +import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException; import java.security.Principal; import java.util.UUID; @@ -20,33 +24,88 @@ public class UserController { @GetMapping("/") public UserResponse getMe(Principal principal) { + if (principal == null) { + throw new UnauthorizedException(); + } + + try { + userService.getByUsername(principal.getName()); + } catch (Exception ex) { + throw new UserNotFoundException(); + } + return UserMapper.INSTANCE.toUserResponse(userService.getByUsername(principal.getName())); } @PutMapping("/") public void updateMe(Principal principal, @RequestBody UpdateUserRequest request) { + if (principal == null) { + throw new UnauthorizedException(); + } + + try { + userService.getByUsername(principal.getName()); + } catch (Exception ex) { + throw new UserNotFoundException(); + } + userService.updateUser(UserMapper.INSTANCE.toUserDTO(request)); } @DeleteMapping("/") public void deleteMe(Principal principal) { - UserDTO user = userService.getByUsername(principal.getName()); + if (principal == null) { + throw new UnauthorizedException(); + } + + UserDTO user; + + try { + user = userService.getByUsername(principal.getName()); + } catch (Exception ex) { + throw new UserNotFoundException(); + } + userService.deleteUser(user.getId()); } @PostMapping("/") public void createMe(@RequestBody CreateUserRequest request) { - userService.createUser(UserMapper.INSTANCE.toUserDTO(request)); + UserDTO user = UserMapper.INSTANCE.toUserDTO(request); + + try { + userService.createUser(user); + } catch (UserAlreadyExistsException ex) { + throw new ru.team58.profileservice.controller.exceptions.UserAlreadyExistsException(); + } catch (IllegalArgumentException ex) { + throw new BadRequestException(); + } } @GetMapping("/{id}") public GetUserResponse getUser(@PathVariable UUID id) { - UserDTO user = userService.getById(id); + UserDTO user; + + try { + user = userService.getById(id); + } catch (ru.team58.profileservice.service.exceptions.UserNotFoundException ex) { + throw new UserNotFoundException(); + } + return UserMapper.INSTANCE.toGetUserResponse(user); } @DeleteMapping("/{id}") - public void deleteUser(@PathVariable UUID id) { - userService.deleteUser(id); + public void deleteUser(Principal principal, @PathVariable UUID id) { + // TODO: Add checking role + if (principal == null) { + throw new UnauthorizedException(); + } + + try { + userService.deleteUser(id); + } catch (ru.team58.profileservice.service.exceptions.UserNotFoundException ex) { + throw new UserNotFoundException(); + } } } diff --git a/src/main/java/ru/team58/profileservice/controller/advice/ErrorDetails.java b/src/main/java/ru/team58/profileservice/controller/advice/ErrorDetails.java new file mode 100644 index 0000000..43ffcbb --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/advice/ErrorDetails.java @@ -0,0 +1,13 @@ +package ru.team58.profileservice.controller.advice; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@AllArgsConstructor +public class ErrorDetails { + private String message; + private int status; +} diff --git a/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java b/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java new file mode 100644 index 0000000..bd0d990 --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java @@ -0,0 +1,40 @@ +package ru.team58.profileservice.controller.advice; + +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 ru.team58.profileservice.controller.exceptions.*; + +@ControllerAdvice +public class UserControllerExceptionHandler { + @ExceptionHandler(UserNotFoundException.class) + public ResponseEntity handleUserNotFoundException(UserNotFoundException ex) { + ErrorDetails errorDetails = new ErrorDetails("User not found", HttpStatus.FORBIDDEN.value()); + return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN); + } + + @ExceptionHandler(UserAlreadyExistsException.class) + public ResponseEntity handleUserAlreadyExistsException(UserAlreadyExistsException ex) { + ErrorDetails errorDetails = new ErrorDetails("User already exists", HttpStatus.BAD_REQUEST.value()); + return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(BadRequestException.class) + public ResponseEntity handleBadRequestException(BadRequestException ex) { + ErrorDetails errorDetails = new ErrorDetails("Bad Request", HttpStatus.BAD_REQUEST.value()); + return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(ForbiddenException.class) + public ResponseEntity handleForbiddenException(ForbiddenException ex) { + ErrorDetails errorDetails = new ErrorDetails("Forbidden", HttpStatus.FORBIDDEN.value()); + return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN); + } + + @ExceptionHandler(UnauthorizedException.class) + public ResponseEntity handleUnauthorizedException(UnauthorizedException ex) { + ErrorDetails errorDetails = new ErrorDetails("Unauthorized", HttpStatus.UNAUTHORIZED.value()); + return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED); + } +} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/BadRequestException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/BadRequestException.java new file mode 100644 index 0000000..48fd739 --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/exceptions/BadRequestException.java @@ -0,0 +1,4 @@ +package ru.team58.profileservice.controller.exceptions; + +public class BadRequestException extends RuntimeException { +} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/ForbiddenException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/ForbiddenException.java new file mode 100644 index 0000000..4461ccc --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/exceptions/ForbiddenException.java @@ -0,0 +1,4 @@ +package ru.team58.profileservice.controller.exceptions; + +public class ForbiddenException extends RuntimeException { +} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/UnauthorizedException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/UnauthorizedException.java new file mode 100644 index 0000000..c787bb2 --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/exceptions/UnauthorizedException.java @@ -0,0 +1,4 @@ +package ru.team58.profileservice.controller.exceptions; + +public class UnauthorizedException extends RuntimeException { +} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/UserAlreadyExistsException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/UserAlreadyExistsException.java new file mode 100644 index 0000000..9108862 --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/exceptions/UserAlreadyExistsException.java @@ -0,0 +1,4 @@ +package ru.team58.profileservice.controller.exceptions; + +public class UserAlreadyExistsException extends BadRequestException{ +} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/UserNotFoundException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/UserNotFoundException.java new file mode 100644 index 0000000..0e95904 --- /dev/null +++ b/src/main/java/ru/team58/profileservice/controller/exceptions/UserNotFoundException.java @@ -0,0 +1,4 @@ +package ru.team58.profileservice.controller.exceptions; + +public class UserNotFoundException extends ForbiddenException { +}