Skip to content

Commit

Permalink
fix: error handler (#15)
Browse files Browse the repository at this point in the history
* chore: reorder imports

* chore: add a error handle

* chore: format code

* chore: add logger
  • Loading branch information
Michael-Liendo authored Jul 19, 2024
1 parent cbfee7a commit ef1b342
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 47 deletions.
26 changes: 10 additions & 16 deletions server/src/controllers/Auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { IUserForLogin } from '@linx/shared';
import validator from 'validator';

import Services from '../../services';
import { BadRequestError } from '../../utils/errorHandler';

import type { IUserForLogin } from '@linx/shared';
import type { FastifyRequest } from 'fastify';
import Services from '../../services';
import type { Reply } from '../../types';

export default async function login(request: FastifyRequest, reply: Reply) {
Expand All @@ -17,19 +18,12 @@ export default async function login(request: FastifyRequest, reply: Reply) {
throw new BadRequestError('Please provide a valid email address');
}

try {
const user = await Services.auth.login({
email,
password,
});
const user = await Services.auth.login({
email,
password,
});

return reply
.code(201)
.send({ message: 'User created', data: { token: user } });
} catch (error) {
return reply.code(error).send({
message: 'Internal server error',
error: error.message as string,
});
}
return reply
.code(201)
.send({ message: 'User created', data: { token: user } });
}
25 changes: 9 additions & 16 deletions server/src/controllers/Auth/register.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import validator from 'validator';

import Services from '../../services';
import type { Reply, Request } from '../../types';
import { BadRequestError } from '../../utils/errorHandler';

import type { IUserForRegister } from '@linx/shared';
import type { Reply, Request } from '../../types';

export default async function register(request: Request, reply: Reply) {
const { first_name, last_name, email, password } =
Expand All @@ -22,19 +22,12 @@ export default async function register(request: Request, reply: Reply) {
throw new BadRequestError('Password must be at least 4 characters long');
}

try {
const user = await Services.auth.register({
first_name,
last_name,
email,
password,
});

return reply.code(201).send({ message: 'User created', data: { ...user } });
} catch (error) {
return reply.code(500).send({
message: 'Internal server error',
error: error.message as string,
});
}
const user = await Services.auth.register({
first_name,
last_name,
email,
password,
});

return reply.code(201).send({ message: 'User created', data: { ...user } });
}
9 changes: 1 addition & 8 deletions server/src/controllers/User/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,5 @@ import type { Reply, Request } from '../../types';

export default async function me(request: Request, reply: Reply) {
const { password, ...user } = request.user;
try {
return reply.code(200).send({ message: 'Ok', data: { user } });
} catch (error) {
return reply.code(500).send({
message: 'Internal server error',
error: error.message as string,
});
}
return reply.code(200).send({ message: 'Ok', data: { user } });
}
6 changes: 4 additions & 2 deletions server/src/middlewares/checkJwt.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { DoneFuncWithErrOrRes, FastifyInstance } from 'fastify';
import fastifyPlugin from 'fastify-plugin';

import Services from '../services';
import type { Request } from '../types';
import { UnauthorizedError } from '../utils/errorHandler';
import { Jwt } from '../utils/jwt';

import type { DoneFuncWithErrOrRes, FastifyInstance } from 'fastify';
import type { Request } from '../types';

function getUser(fastify: FastifyInstance, _, done: DoneFuncWithErrOrRes) {
fastify.decorateRequest('user', null);

Expand Down
17 changes: 16 additions & 1 deletion server/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import auth from './auth';
import user from './user';

import type {
DoneFuncWithErrOrRes,
FastifyInstance,
RegisterOptions,
} from 'fastify';
import user from './user';
import type { ErrorWithDetails } from '../utils/errorHandler';

export default function routes(
fastify: FastifyInstance,
_: RegisterOptions,
done: DoneFuncWithErrOrRes,
) {
fastify.setErrorHandler((error: ErrorWithDetails, _, reply) => {
if (error.statusCode >= 500) {
fastify.log.error(error);
} else if (error.statusCode >= 400) {
fastify.log.info(error);
}

return reply.code(error.statusCode || 500).send({
error: error.name || 'INTERNAL_SERVER_ERROR',
message: error.message,
details: error.details,
});
});

fastify.get('/', async () => {
return { hello: 'world' };
});
Expand Down
5 changes: 3 additions & 2 deletions server/src/routes/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import me from '../../controllers/User/me';
import checkJwt from '../../middlewares/checkJwt';

import type { IReply } from '@linx/shared';
import type {
DoneFuncWithErrOrRes,
FastifyInstance,
RegisterOptions,
} from 'fastify';
import me from '../../controllers/User/me';
import checkJwt from '../../middlewares/checkJwt';

export default function user(
fastify: FastifyInstance,
Expand Down
8 changes: 6 additions & 2 deletions server/src/utils/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ export abstract class ErrorWithDetails extends Error {
) {
super(message);
}
abstract statusCode: StatusCodes;
abstract statusCode;
}

export class BadRequestError extends ErrorWithDetails {
name = 'BAD_REQUEST';
statusCode = StatusCodes.BAD_REQUEST;
details = 'You need add';
}
export class ConflictError extends ErrorWithDetails {
name = 'CONFLICT';
statusCode = StatusCodes.CONFLICT;
}
export class ForbiddenError extends ErrorWithDetails {
name = 'FORBIDDEN';
statusCode = StatusCodes.FORBIDDEN;
}
export class NotFoundError extends ErrorWithDetails {
name = 'NOT_FOUND';
statusCode = StatusCodes.NOT_FOUND;
}
export class UnauthorizedError extends ErrorWithDetails {
name = 'UNAUTHORIZED';
statusCode = StatusCodes.UNAUTHORIZED;
}

0 comments on commit ef1b342

Please sign in to comment.