diff --git a/src/main/java/hng_java_boilerplate/exception/GlobalExceptionHandler.java b/src/main/java/hng_java_boilerplate/exception/GlobalExceptionHandler.java index 54c123ab..08715f4c 100644 --- a/src/main/java/hng_java_boilerplate/exception/GlobalExceptionHandler.java +++ b/src/main/java/hng_java_boilerplate/exception/GlobalExceptionHandler.java @@ -4,6 +4,7 @@ import com.stripe.exception.SignatureVerificationException; import com.stripe.exception.StripeException; import dev.samstevens.totp.exceptions.QrGenerationException; +import hng_java_boilerplate.profile.dto.response.ProfilePictureResponse; import hng_java_boilerplate.squeeze.dto.ResponseMessageDto; import hng_java_boilerplate.user.dto.response.ErrorResponse; import io.jsonwebtoken.ExpiredJwtException; @@ -126,4 +127,10 @@ private ErrorResponseDto setResponse(String message, String error, int statusCod errorResponseDTO.setStatus_code(statusCode); return errorResponseDTO; } + + @ExceptionHandler() + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public ErrorResponseDto handleProfilePictureUploadException(ProfilePictureUploadException ex) { + return setResponse("Image Not Uploaded",HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),HttpStatus.INTERNAL_SERVER_ERROR.value()); + } } diff --git a/src/main/java/hng_java_boilerplate/exception/ProfilePictureUploadException.java b/src/main/java/hng_java_boilerplate/exception/ProfilePictureUploadException.java new file mode 100644 index 00000000..fd1586d7 --- /dev/null +++ b/src/main/java/hng_java_boilerplate/exception/ProfilePictureUploadException.java @@ -0,0 +1,7 @@ +package hng_java_boilerplate.exception; + +public class ProfilePictureUploadException extends RuntimeException { + public ProfilePictureUploadException(String message) { + super(message); + } +} diff --git a/src/main/java/hng_java_boilerplate/profile/serviceImpl/ProfileServiceImpl.java b/src/main/java/hng_java_boilerplate/profile/serviceImpl/ProfileServiceImpl.java index 036f89a2..1fe02bec 100644 --- a/src/main/java/hng_java_boilerplate/profile/serviceImpl/ProfileServiceImpl.java +++ b/src/main/java/hng_java_boilerplate/profile/serviceImpl/ProfileServiceImpl.java @@ -5,6 +5,7 @@ import com.amazonaws.services.s3.model.PutObjectRequest; import hng_java_boilerplate.exception.BadRequestException; import hng_java_boilerplate.exception.NotFoundException; +import hng_java_boilerplate.exception.ProfilePictureUploadException; import hng_java_boilerplate.profile.dto.request.DeactivateUserRequest; import hng_java_boilerplate.profile.dto.request.UpdateUserProfileDto; import hng_java_boilerplate.profile.dto.response.*; @@ -131,18 +132,15 @@ public ProfileResponse getUserProfile(String userId) { @Override public ResponseEntity uploadProfileImage(MultipartFile file) { if (file.isEmpty() || !isValidImage(file)) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body(new ProfilePictureResponse(false, "Invalid file type or missing image. Only JPG or JPEG formats are allowed.", null)); + throw new BadRequestException("Invalid file type or missing image. Only JPG or JPEG formats are allowed"); } try { String filename = UUID.randomUUID() + "_" + file.getOriginalFilename(); uploadFileToS3(file, filename); - String fileUrl = amazonS3.getUrl(bucketName, filename).toString(); return ResponseEntity.ok(new ProfilePictureResponse(true, "Profile image uploaded successfully", fileUrl)); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(new ProfilePictureResponse(false, "An error occurred while uploading your profile image. Please try again later.", null)); + throw new ProfilePictureUploadException("An error occurred while uploading your profile image: " + e.getMessage()); } }