Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
refactor: ajustar tratativas de erros
Browse files Browse the repository at this point in the history
  • Loading branch information
bfukumori committed Jul 11, 2024
1 parent 4c2ebb1 commit ace20c5
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 211 deletions.
15 changes: 4 additions & 11 deletions client.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@tripId = {{getTrips.response.body.trips[0].id}}
@participantId = {{getTrips.response.body.trips[0].participants[1].id}}

# @name createTrip
POST http://localhost:3333/trips HTTP/1.1
content-type: application/json
Expand All @@ -19,20 +22,17 @@ GET http://localhost:3333/trips HTTP/1.1
###

# @name getTripDetails
@tripId = {{getTrips.response.body.trips[0].id}}
GET http://localhost:3333/trips/{{tripId}} HTTP/1.1

###

# @name confirmTrip
@tripId = {{getTrips.response.body.trips[0].id}}

GET http://localhost:3333/trips/{{tripId}}/confirm HTTP/1.1

###

# @name updateTrip
@tripId = {{getTrips.response.body.trips[0].id}}

PUT http://localhost:3333/trips/{{tripId}} HTTP/1.1
content-type: application/json
Expand All @@ -46,7 +46,6 @@ content-type: application/json
###

# @name createActivity
@tripId = {{getTrips.response.body.trips[0].id}}

POST http://localhost:3333/trips/{{tripId}}/activities HTTP/1.1
content-type: application/json
Expand All @@ -59,14 +58,12 @@ content-type: application/json
###

# @name getActivities
@tripId = {{getTrips.response.body.trips[0].id}}

GET http://localhost:3333/trips/{{tripId}}/activities HTTP/1.1

###

# @name createLink
@tripId = {{getTrips.response.body.trips[0].id}}

POST http://localhost:3333/trips/{{tripId}}/links HTTP/1.1
content-type: application/json
Expand All @@ -79,34 +76,30 @@ content-type: application/json
###

# @name getLinks
@tripId = {{getTrips.response.body.trips[0].id}}

GET http://localhost:3333/trips/{{tripId}}/links HTTP/1.1

###

# @name getParticipants
@tripId = {{getTrips.response.body.trips[0].id}}

GET http://localhost:3333/trips/{{tripId}}/participants HTTP/1.1

###

# @name getParticipantDetails
@participantId = {{getParticipants.response.body.participants[0].id}}

GET http://localhost:3333/participants/{{participantId}} HTTP/1.1

###

# @name confirmParticipant
@participantId = {{getTrips.response.body.trips[0].participants[1].id}}

GET http://localhost:3333/participants/{{participantId}}/confirm HTTP/1.1

###

# @name createInvite
@tripId = {{getTrips.response.body.trips[0].id}}

POST http://localhost:3333/trips/{{tripId}}/invites HTTP/1.1
content-type: application/json
Expand Down
Binary file modified prisma/dev.db
Binary file not shown.
3 changes: 0 additions & 3 deletions src/errors/InvalidDate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ClientError } from './client-error.js';

export class InvalidDate extends ClientError {
code: number;

constructor(message: string) {
super(message);
this.name = 'InvalidDate';
this.code = 400;
}
}
3 changes: 0 additions & 3 deletions src/errors/ParticipantAlreadyConfirmed.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ClientError } from './client-error.js';

export class ParticipantAlreadyConfirmed extends ClientError {
code: number;

constructor() {
super('Participant already confirmed');
this.name = 'ParticipantAlreadyConfirmed';
this.code = 400;
}
}
3 changes: 0 additions & 3 deletions src/errors/ParticipantNotFound.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ClientError } from './client-error.js';

export class ParticipantNotFound extends ClientError {
code: number;

constructor() {
super('Participant not found');
this.name = 'ParticipantNotFound';
this.code = 400;
}
}
3 changes: 0 additions & 3 deletions src/errors/TripAlreadyConfirmed.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ClientError } from './client-error.js';

export class TripAlreadyConfirmed extends ClientError {
code: number;

constructor() {
super('Trip already confirmed');
this.name = 'TripAlreadyConfirmed';
this.code = 400;
}
}
3 changes: 0 additions & 3 deletions src/errors/TripNotFound.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ClientError } from './client-error.js';

export class TripNotFound extends ClientError {
code: number;

constructor() {
super('Trip not found');
this.name = 'TripNotFound';
this.code = 400;
}
}
2 changes: 1 addition & 1 deletion src/errors/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const errorHandler: FastifyErrorHandler = async (error, _, reply) => {
}

if (error instanceof ClientError) {
return reply.status(400).send({
return reply.status(422).send({
message: error.message,
});
}
Expand Down
8 changes: 1 addition & 7 deletions src/routes/confirm-participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function confirmParticipant(fastify: FastifyInstance) {
tags: ['participants'],
response: {
200: z.void(),
400: z.object({
422: z.object({
message: z.string(),
}),
},
Expand All @@ -37,15 +37,9 @@ export async function confirmParticipant(fastify: FastifyInstance) {

return reply.redirect(`${env.WEB_BASE_URL}/trips/${tripId}`);
} catch (error) {
if (error instanceof ParticipantNotFound) {
return reply.code(error.code).send({ message: error.message });
}

if (error instanceof ParticipantAlreadyConfirmed) {
return reply.redirect(`${env.WEB_BASE_URL}/trips`);
}

return reply.code(500).send({ message: 'Server error' });
}
}
);
Expand Down
9 changes: 1 addition & 8 deletions src/routes/confirm-trip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { z } from 'zod';

import { env } from '@/env/index.js';
import { TripAlreadyConfirmed } from '@/errors/TripAlreadyConfirmed.js';
import { TripNotFound } from '@/errors/TripNotFound.js';
import { makeConfirmTripUseCase } from '@/factories/makeConfirmTripUseCase.js';
import { sendTripConfirmationEmail } from '@/lib/mail.js';

Expand All @@ -19,7 +18,7 @@ export async function confirmTrip(fastify: FastifyInstance) {
tags: ['trips'],
response: {
200: z.void(),
400: z.object({
422: z.object({
message: z.string(),
}),
},
Expand Down Expand Up @@ -50,15 +49,9 @@ export async function confirmTrip(fastify: FastifyInstance) {
);
return reply.redirect(`${env.WEB_BASE_URL}/trips/${tripId}`);
} catch (error) {
if (error instanceof TripNotFound) {
return reply.code(error.code).send({ message: error.message });
}

if (error instanceof TripAlreadyConfirmed) {
return reply.redirect(`${env.WEB_BASE_URL}/trips/${tripId}`);
}

return reply.code(500).send({ message: 'Server error' });
}
}
);
Expand Down
23 changes: 7 additions & 16 deletions src/routes/create-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { FastifyInstance } from 'fastify';
import { ZodTypeProvider } from 'fastify-type-provider-zod';
import { z } from 'zod';

import { InvalidDate } from '@/errors/InvalidDate.js';
import { makeCreateActivityUseCase } from '@/factories/makeCreateActivityUseCase.js';

export async function createActivity(fastify: FastifyInstance) {
Expand All @@ -22,7 +21,7 @@ export async function createActivity(fastify: FastifyInstance) {
201: z.object({
activityId: z.string().uuid(),
}),
400: z.object({
422: z.object({
message: z.string(),
}),
},
Expand All @@ -35,21 +34,13 @@ export async function createActivity(fastify: FastifyInstance) {

const createActivityUseCase = await makeCreateActivityUseCase();

try {
const { activityId } = await createActivityUseCase.execute({
title,
occursAt: occurs_at,
tripId,
});
const { activityId } = await createActivityUseCase.execute({
title,
occursAt: occurs_at,
tripId,
});

return reply.code(201).send({ activityId });
} catch (error) {
if (error instanceof InvalidDate) {
return reply.code(error.code).send({ message: error.message });
}

return reply.code(500).send({ message: 'Server error' });
}
return reply.code(201).send({ activityId });
}
);
}
43 changes: 17 additions & 26 deletions src/routes/create-invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ZodTypeProvider } from 'fastify-type-provider-zod';
import { z } from 'zod';

import { env } from '@/env/index.js';
import { TripNotFound } from '@/errors/TripNotFound.js';
import { makeCreateInviteUseCase } from '@/factories/makeCreateInviteUseCase.js';
import { makeGetTripDetailsUseCase } from '@/factories/makeGetTripDetailsUseCase.js';
import { sendTripConfirmationEmail } from '@/lib/mail.js';
Expand All @@ -22,7 +21,7 @@ export async function createInvite(fastify: FastifyInstance) {
tags: ['invites'],
response: {
200: z.void(),
400: z.object({
422: z.object({
message: z.string(),
}),
},
Expand All @@ -36,34 +35,26 @@ export async function createInvite(fastify: FastifyInstance) {
const createInviteUseCase = await makeCreateInviteUseCase();
const getTripDetails = await makeGetTripDetailsUseCase();

try {
const { email: participantEmail } = await createInviteUseCase.execute({
email,
tripId,
});
const { email: participantEmail } = await createInviteUseCase.execute({
email,
tripId,
});

const { destination, startsAt, endsAt } = await getTripDetails.execute(
tripId
);
const { destination, startsAt, endsAt } = await getTripDetails.execute(
tripId
);

const confirmationLink = `${env.API_BASE_URL}/participants/${tripId}/confirm`;
const confirmationLink = `${env.API_BASE_URL}/participants/${tripId}/confirm`;

await sendTripConfirmationEmail({
destination,
startsAt,
endsAt,
participantEmail,
confirmationLink,
});
await sendTripConfirmationEmail({
destination,
startsAt,
endsAt,
participantEmail,
confirmationLink,
});

return reply.code(204).send();
} catch (error) {
if (error instanceof TripNotFound) {
return reply.code(error.code).send({ message: error.message });
}

return reply.code(500).send({ message: 'Server error' });
}
return reply.code(204).send();
}
);
}
22 changes: 7 additions & 15 deletions src/routes/create-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function createLink(fastify: FastifyInstance) {
201: z.object({
linkId: z.string(),
}),
400: z.object({
422: z.object({
message: z.string(),
}),
},
Expand All @@ -35,21 +35,13 @@ export async function createLink(fastify: FastifyInstance) {

const createLinkUseCase = await makeCreateLinkUseCase();

try {
const { linkId } = await createLinkUseCase.execute({
title,
url,
tripId,
});
const { linkId } = await createLinkUseCase.execute({
title,
url,
tripId,
});

return reply.code(201).send({ linkId });
} catch (error) {
if (error instanceof TripNotFound) {
return reply.code(error.code).send({ message: error.message });
}

return reply.code(500).send({ message: 'Server error' });
}
return reply.code(201).send({ linkId });
}
);
}
Loading

0 comments on commit ace20c5

Please sign in to comment.