Skip to content

Commit

Permalink
Merge pull request #317 from SaiSamyuG/development
Browse files Browse the repository at this point in the history
[Issue #275] Fix. Error response of Update User API.
  • Loading branch information
Sunagatov committed Jun 15, 2024
2 parents 75847ae + cc13143 commit c9118e3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.zufar.icedlatte.filestorage.file.FileDeleter;
import com.zufar.icedlatte.user.api.avatar.UserAvatarLinkProvider;
import com.zufar.icedlatte.user.api.avatar.UserAvatarUploader;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -68,7 +69,7 @@ public ResponseEntity<UserDto> getUserById() {

@Override
@PutMapping
public ResponseEntity<UserDto> editUserById(UpdateUserAccountRequest updateUserAccountRequest) {
public ResponseEntity<UserDto> editUserById(@Valid @RequestBody UpdateUserAccountRequest updateUserAccountRequest) {
UUID userId = securityPrincipalProvider.getUserId();
log.info("Received the request to edit the User with userId - {}.", userId);
UserDto updatedUserDto = updateUserOperationPerformer.updateUser(updateUserAccountRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand Down Expand Up @@ -47,4 +48,17 @@ public ApiErrorResponse handleInvalidOldPasswordException(final InvalidOldPasswo
apiErrorResponse.message(), errorDebugMessageCreator.buildErrorDebugMessage(exception));
return apiErrorResponse;
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ApiErrorResponse handleValidationExceptions(MethodArgumentNotValidException ex) {
StringBuilder errorMessage = new StringBuilder();
ex.getBindingResult().getFieldErrors().forEach(error -> {
if (errorMessage.length() > 0) {
errorMessage.append(" and ");
}
errorMessage.append(error.getDefaultMessage());
});
return apiErrorResponseCreator.buildResponse(errorMessage.toString(), HttpStatus.BAD_REQUEST);
}
}
5 changes: 2 additions & 3 deletions src/main/resources/api-specs/user-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,15 @@ components:
UpdateUserAccountRequest:
type: "object"
description: "A user profile object details to update"
required:
- firstName
- lastName
properties:
firstName:
type: "string"
description: "The first name of the user"
x-field-extra-annotation: "@jakarta.validation.constraints.NotNull(message = \"First name is the mandatory attribute\")"
lastName:
type: "string"
description: "The last name of the user"
x-field-extra-annotation: "@jakarta.validation.constraints.NotNull(message = \"Last name is the mandatory attribute\")"
birthDate:
type: "string"
description: "The birth date of the user"
Expand Down

0 comments on commit c9118e3

Please sign in to comment.