From 4e0aa6f4e0add805b0fafb99f21131bf6c258ef1 Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 09:58:29 +0300 Subject: [PATCH 1/4] Update Image Resource Errors: Surround Error Responses With Message Field Signed-off-by: Tal Jacob --- nextstep-backend/src/controllers/resources_controller.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nextstep-backend/src/controllers/resources_controller.ts b/nextstep-backend/src/controllers/resources_controller.ts index c59a52b..a5f7a0b 100644 --- a/nextstep-backend/src/controllers/resources_controller.ts +++ b/nextstep-backend/src/controllers/resources_controller.ts @@ -16,7 +16,11 @@ const createUserImageResource = async (req: CustomRequest, res: Response) => { return res.status(201).send(updatedUser); } catch (error) { - handleError(error, res); + if (error instanceof multer.MulterError || error instanceof TypeError) { + return res.status(400).send({ message: error.message }); + } else { + handleError(error, res); + } } }; @@ -26,7 +30,7 @@ const createImageResource = async (req: Request, res: Response) => { return res.status(201).send(imageFilename); } catch (error) { if (error instanceof multer.MulterError || error instanceof TypeError) { - return res.status(400).send(error.message); + return res.status(400).send({ message: error.message }); } else { handleError(error, res); } From ea616f0f39283a80455192b9aeb5ebb18bb4424d Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 09:58:52 +0300 Subject: [PATCH 2/4] Update Image Resource 400 Errors In Swagger: Surround Error Responses With Message Field Signed-off-by: Tal Jacob --- nextstep-backend/src/openapi/swagger.yaml | 36 ++++++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/nextstep-backend/src/openapi/swagger.yaml b/nextstep-backend/src/openapi/swagger.yaml index 7827a98..8e4f2f1 100644 --- a/nextstep-backend/src/openapi/swagger.yaml +++ b/nextstep-backend/src/openapi/swagger.yaml @@ -710,14 +710,24 @@ paths: '400': description: Bad request content: - text/plain: + application/json: + schema: + type: object + properties: + message: + type: string + description: Error message indicating the specific issue + example: "File too large" examples: fileTooLarge: - value: "File too large" + value: + message: "File too large" invalidFileType: - value: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/" + value: + message: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/" noFileUploaded: - value: "No file uploaded" + value: + message: "No file uploaded" '404': description: User not found '500': @@ -751,14 +761,24 @@ paths: '400': description: Bad request content: - text/plain: + application/json: + schema: + type: object + properties: + message: + type: string + description: Error message indicating the specific issue + example: "File too large" examples: fileTooLarge: - value: "File too large" + value: + message: "File too large" invalidFileType: - value: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/" + value: + message: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/" noFileUploaded: - value: "No file uploaded" + value: + message: "No file uploaded" '500': description: Internal server error From 3ec7a78bac321c71ad38105c9814926a1782ee03 Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 09:59:54 +0300 Subject: [PATCH 3/4] Show Image Validation Bad Request Errors In Profile Page Signed-off-by: Tal Jacob --- nextstep-frontend/src/pages/Profile.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nextstep-frontend/src/pages/Profile.tsx b/nextstep-frontend/src/pages/Profile.tsx index 44ee4c1..cb72e70 100644 --- a/nextstep-frontend/src/pages/Profile.tsx +++ b/nextstep-frontend/src/pages/Profile.tsx @@ -75,11 +75,15 @@ const Profile: React.FC = () => { setSuccess(false); window.location.reload(); }, 3000); + } + } catch (err: any) { + if (err.response && err.response.status === 400 && + err.response.data && err.response.data && + err.response.data.message) { + setError(err.response.data.message); } else { setError('Error uploading image. Please try again.'); } - } catch (err) { - setError('Error uploading image. Please try again.'); } }; From 3ceb6d62797a5a63a357e4adc4a0e440346bbac6 Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 10:08:14 +0300 Subject: [PATCH 4/4] Update Resource Images Tests With New Error Schema Signed-off-by: Tal Jacob --- nextstep-backend/src/tests/resources_images.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nextstep-backend/src/tests/resources_images.test.ts b/nextstep-backend/src/tests/resources_images.test.ts index 80884c7..d1ef1eb 100644 --- a/nextstep-backend/src/tests/resources_images.test.ts +++ b/nextstep-backend/src/tests/resources_images.test.ts @@ -65,7 +65,7 @@ describe('Resources Service - Upload Image', () => { .attach('file', nonImageBlob, 'test-non-image.pdf'); expect(res.statusCode).toBe(400); - expect(res.text).toBe("Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/"); + expect(res.text).toBe(`{"message":"Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/"}`); }); it('should fail to upload an image larger than the max size', async () => { @@ -76,7 +76,7 @@ describe('Resources Service - Upload Image', () => { .attach('file', largeImageBlob, 'large-test-image.jpg'); expect(res.statusCode).toBe(400); - expect(res.text).toBe("File too large"); + expect(res.text).toBe(`{"message":"File too large"}`); }); afterAll(async () => {