From 2a522d2d05a9b17d9920195203e0d7dbcb3e929c Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 10:32:55 +0300 Subject: [PATCH 1/4] Update Resume Resource Errors: Surround Error Responses With Message Field Signed-off-by: Tal Jacob --- nextstep-backend/src/controllers/resources_controller.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nextstep-backend/src/controllers/resources_controller.ts b/nextstep-backend/src/controllers/resources_controller.ts index c59a52b..b815f8a 100644 --- a/nextstep-backend/src/controllers/resources_controller.ts +++ b/nextstep-backend/src/controllers/resources_controller.ts @@ -54,7 +54,7 @@ const createResumeResource = async (req: Request, res: Response) => { return res.status(201).send(resumeFilename); } 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 01c6bbd73786b8354059efdf19b99bdefa46695e Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 10:33:35 +0300 Subject: [PATCH 2/4] Update Resume Resource 400 Errors In Swagger: Surround Error Responses With Message Field Signed-off-by: Tal Jacob --- nextstep-backend/src/openapi/swagger.yaml | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/nextstep-backend/src/openapi/swagger.yaml b/nextstep-backend/src/openapi/swagger.yaml index 7827a98..6dd25c1 100644 --- a/nextstep-backend/src/openapi/swagger.yaml +++ b/nextstep-backend/src/openapi/swagger.yaml @@ -816,14 +816,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 PDF, DOC, DOCX and TXT/TEXT files are allowed" + value: + message: "Invalid file type. Only PDF, DOC, DOCX and TXT/TEXT files are allowed" noFileUploaded: - value: "No file uploaded" + value: + message: "No file uploaded" '401': description: Unauthorized '500': From 8b5bac979935fb1c43bcb924789249bd116f0fb1 Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 10:34:26 +0300 Subject: [PATCH 3/4] Show Resume Validation Bad Request Errors In Resume Page Signed-off-by: Tal Jacob --- nextstep-frontend/src/pages/Resume.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nextstep-frontend/src/pages/Resume.tsx b/nextstep-frontend/src/pages/Resume.tsx index 2091281..9ff670e 100644 --- a/nextstep-frontend/src/pages/Resume.tsx +++ b/nextstep-frontend/src/pages/Resume.tsx @@ -285,8 +285,14 @@ const Resume: React.FC = () => { setLoading(false); }; - } catch (err) { - setError(err instanceof Error ? err.message : 'An error occurred'); + } 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(err instanceof Error ? err.message : 'An error occurred'); + } setLoading(false); } }; From 69536044122a1ccac98e67a1919be06cbf82e73d Mon Sep 17 00:00:00 2001 From: Tal Jacob Date: Mon, 2 Jun 2025 10:34:42 +0300 Subject: [PATCH 4/4] Update Resource Resumes Tests With New Error Schema Signed-off-by: Tal Jacob --- nextstep-backend/src/tests/resources_resumes.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nextstep-backend/src/tests/resources_resumes.test.ts b/nextstep-backend/src/tests/resources_resumes.test.ts index 6af14f4..cc6dd28 100644 --- a/nextstep-backend/src/tests/resources_resumes.test.ts +++ b/nextstep-backend/src/tests/resources_resumes.test.ts @@ -143,7 +143,7 @@ describe('Resume API Tests', () => { .attach('file', Buffer.from('invalid content'), { filename: 'test.png' }) .expect(400); - expect(response.text).toBe('Invalid file type. Only PDF, DOC, DOCX and TXT/TEXT files are allowed.'); + expect(response.text).toBe(`{"message":"Invalid file type. Only PDF, DOC, DOCX and TXT/TEXT files are allowed."}`); }); it('should handle missing file', async () => { @@ -152,7 +152,7 @@ describe('Resume API Tests', () => { .set('Authorization', `Bearer ${testToken}`) .expect(400); - expect(response.text).toBe('No file uploaded.'); + expect(response.text).toBe(`{"message":"No file uploaded."}`); }); });