Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed error messages when throwing #12

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"@fastify/swagger": "^8.10.0",
"@fastify/swagger-ui": "^1.9.3",
"@prisma/client": "^5.3.1",
"ajv-errors": "^3.0.0",
"@vercel/blob": "^0.12.5",
"ajv-errors": "^3.0.0",
Anders164a marked this conversation as resolved.
Show resolved Hide resolved
"bcrypt": "^5.1.1",
"dotenv": "^16.3.1",
"fastify": "^4.23.2",
Expand Down
20 changes: 12 additions & 8 deletions src/modules/auth/__test__/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ describe('POST /api/auth/login', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Email and/or password incorrect',
error: 'UnauthorizedError',
errors: {
_: ['Email and/or password incorrect'],
},
statusCode: 401,
});
});
Expand All @@ -74,8 +76,10 @@ describe('POST /api/auth/login', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Email and/or password incorrect',
error: 'UnauthorizedError',
errors: {
_: ['Email and/or password incorrect'],
},
statusCode: 401,
});
});
Expand All @@ -89,7 +93,7 @@ describe('POST /api/auth/login', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Email is required', 'Password is required'],
},
Expand All @@ -109,7 +113,7 @@ describe('POST /api/auth/login', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Email er påkrævet', 'Adgangskode er påkrævet'],
},
Expand All @@ -128,7 +132,7 @@ describe('POST /api/auth/login', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Email is required'],
},
Expand All @@ -147,7 +151,7 @@ describe('POST /api/auth/login', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Password is required'],
},
Expand Down
36 changes: 24 additions & 12 deletions src/modules/auth/__test__/refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ describe('POST /api/auth/refresh', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand All @@ -111,8 +113,10 @@ describe('POST /api/auth/refresh', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand Down Expand Up @@ -145,8 +149,10 @@ describe('POST /api/auth/refresh', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand All @@ -168,8 +174,10 @@ describe('POST /api/auth/refresh', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand All @@ -185,8 +193,10 @@ describe('POST /api/auth/refresh', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand All @@ -207,8 +217,10 @@ describe('POST /api/auth/refresh', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand Down
26 changes: 14 additions & 12 deletions src/modules/auth/__test__/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
message: 'Email is already in use',
error: 'BadRequestError',
errors: {
_: ['Email is already in use'],
},
statusCode: 400,
});
});
Expand All @@ -68,7 +70,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
email: ['Email must be of correct format'],
},
Expand All @@ -88,7 +90,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Email is required'],
},
Expand All @@ -109,7 +111,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
email: ['Email must be of correct format'],
},
Expand All @@ -129,7 +131,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Password is required'],
},
Expand All @@ -150,7 +152,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
password: ['Password must be atleast 8 characters'],
},
Expand All @@ -171,7 +173,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
password: ['Password must be atleast 8 characters'],
},
Expand All @@ -191,7 +193,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Name is required'],
},
Expand All @@ -212,7 +214,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
name: ['You must choose a name'],
},
Expand All @@ -229,7 +231,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Email is required', 'Password is required', 'Name is required'],
},
Expand All @@ -249,7 +251,7 @@ describe('POST /api/auth/register', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toMatchObject({
error: 'Bad Request',
error: 'ValidationError',
errors: {
_: ['Email er påkrævet', 'Adgangskode er påkrævet', 'Navn er påkrævet'],
},
Expand Down
12 changes: 8 additions & 4 deletions src/modules/auth/__test__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ describe('GET /api/auth/user', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand All @@ -74,8 +76,10 @@ describe('GET /api/auth/user', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toMatchObject({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand Down
30 changes: 20 additions & 10 deletions src/modules/item/__test__/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ describe('POST /api/item', () => {

expect(response.statusCode).toBe(401);
expect(response.json()).toEqual({
error: 'Unauthorized',
message: 'Unauthorized',
error: 'UnauthorizedError',
errors: {
_: ['Unauthorized'],
},
statusCode: 401,
});
});
Expand All @@ -97,8 +99,10 @@ describe('POST /api/item', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toEqual({
error: 'Bad Request',
message: 'clientPayload is required',
error: 'BadRequestError',
errors: {
_: ['clientPayload is required'],
},
statusCode: 400,
});
});
Expand All @@ -125,8 +129,10 @@ describe('POST /api/item', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toEqual({
error: 'Bad Request',
message: 'clientPayload.parentId is required',
error: 'BadRequestError',
errors: {
_: ['clientPayload.parentId is required'],
},
statusCode: 400,
});
});
Expand All @@ -146,8 +152,10 @@ describe('POST /api/item', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toEqual({
error: 'Bad Request',
message: expect.stringContaining('Unexpected token'),
error: 'BadRequestError',
errors: {
_: [expect.stringContaining('Unexpected token')],
},
statusCode: 400,
});
});
Expand Down Expand Up @@ -179,8 +187,10 @@ describe('POST /api/item', () => {

expect(response.statusCode).toBe(400);
expect(response.json()).toEqual({
error: 'Bad Request',
message: 'Vercel Blob: Missing callback signature',
error: 'BadRequestError',
errors: {
_: ['Vercel Blob: Missing callback signature'],
},
statusCode: 400,
});
});
Expand Down
10 changes: 8 additions & 2 deletions src/plugins/error.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ export default fastifyPlugin(async (fastify: FastifyInstance) => {
});

return reply.status(400).send({
error: 'Bad Request',
error: 'ValidationError',
errors: errors,
statusCode: 400,
});
}
return reply.send(error);
const statusCode = error.statusCode ?? /* istanbul ignore next */ 500;

return reply.status(statusCode).send({
error: error.name,
errors: { _: [error.message] },
statusCode: error.statusCode,
});
});
});