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); } 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': 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."}`); }); }); 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); } };