Skip to content

Commit c709aa1

Browse files
authored
Merge pull request #21 from NextStepFinalProject/NXD-23
Nxd 23 - Show Error Reason When Uploading An Image
2 parents 1676a1b + 3ceb6d6 commit c709aa1

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

nextstep-backend/src/controllers/resources_controller.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ const createUserImageResource = async (req: CustomRequest, res: Response) => {
1616

1717
return res.status(201).send(updatedUser);
1818
} catch (error) {
19-
handleError(error, res);
19+
if (error instanceof multer.MulterError || error instanceof TypeError) {
20+
return res.status(400).send({ message: error.message });
21+
} else {
22+
handleError(error, res);
23+
}
2024
}
2125
};
2226

@@ -26,7 +30,7 @@ const createImageResource = async (req: Request, res: Response) => {
2630
return res.status(201).send(imageFilename);
2731
} catch (error) {
2832
if (error instanceof multer.MulterError || error instanceof TypeError) {
29-
return res.status(400).send(error.message);
33+
return res.status(400).send({ message: error.message });
3034
} else {
3135
handleError(error, res);
3236
}

nextstep-backend/src/openapi/swagger.yaml

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,14 +710,24 @@ paths:
710710
'400':
711711
description: Bad request
712712
content:
713-
text/plain:
713+
application/json:
714+
schema:
715+
type: object
716+
properties:
717+
message:
718+
type: string
719+
description: Error message indicating the specific issue
720+
example: "File too large"
714721
examples:
715722
fileTooLarge:
716-
value: "File too large"
723+
value:
724+
message: "File too large"
717725
invalidFileType:
718-
value: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/"
726+
value:
727+
message: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/"
719728
noFileUploaded:
720-
value: "No file uploaded"
729+
value:
730+
message: "No file uploaded"
721731
'404':
722732
description: User not found
723733
'500':
@@ -751,14 +761,24 @@ paths:
751761
'400':
752762
description: Bad request
753763
content:
754-
text/plain:
764+
application/json:
765+
schema:
766+
type: object
767+
properties:
768+
message:
769+
type: string
770+
description: Error message indicating the specific issue
771+
example: "File too large"
755772
examples:
756773
fileTooLarge:
757-
value: "File too large"
774+
value:
775+
message: "File too large"
758776
invalidFileType:
759-
value: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/"
777+
value:
778+
message: "Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/"
760779
noFileUploaded:
761-
value: "No file uploaded"
780+
value:
781+
message: "No file uploaded"
762782
'500':
763783
description: Internal server error
764784

nextstep-backend/src/tests/resources_images.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('Resources Service - Upload Image', () => {
6565
.attach('file', nonImageBlob, 'test-non-image.pdf');
6666

6767
expect(res.statusCode).toBe(400);
68-
expect(res.text).toBe("Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/");
68+
expect(res.text).toBe(`{"message":"Invalid file type. Only images are allowed: /jpeg|jpg|png|gif/"}`);
6969
});
7070

7171
it('should fail to upload an image larger than the max size', async () => {
@@ -76,7 +76,7 @@ describe('Resources Service - Upload Image', () => {
7676
.attach('file', largeImageBlob, 'large-test-image.jpg');
7777

7878
expect(res.statusCode).toBe(400);
79-
expect(res.text).toBe("File too large");
79+
expect(res.text).toBe(`{"message":"File too large"}`);
8080
});
8181

8282
afterAll(async () => {

nextstep-frontend/src/pages/Profile.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@ const Profile: React.FC = () => {
7575
setSuccess(false);
7676
window.location.reload();
7777
}, 3000);
78+
}
79+
} catch (err: any) {
80+
if (err.response && err.response.status === 400 &&
81+
err.response.data && err.response.data &&
82+
err.response.data.message) {
83+
setError(err.response.data.message);
7884
} else {
7985
setError('Error uploading image. Please try again.');
8086
}
81-
} catch (err) {
82-
setError('Error uploading image. Please try again.');
8387
}
8488
};
8589

0 commit comments

Comments
 (0)