Skip to content

Commit

Permalink
Criando classe de erro
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-vieira committed Sep 30, 2020
1 parent b5e3581 commit 618b305
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
12 changes: 12 additions & 0 deletions nivel-02/02-iniciando-back-end-do-app/src/errors/AppError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AppError {
public readonly message: string;

public readonly statusCode: number;

constructor(message: string, statusCode = 400) {
this.message = message;
this.statusCode = statusCode;
}
}

export default AppError;
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { NextFunction, Request, Response } from 'express';
import { verify } from 'jsonwebtoken';

import AppError from '../errors/AppError';

import authConfig from '../config/auth';

interface TokenPayload {
Expand All @@ -17,7 +19,7 @@ export default function ensureAuthenticated(
const authHeader = request.headers.authorization;

if (!authHeader) {
throw new Error('JWT token is missing');
throw new AppError('JWT token is missing', 401);
}

const [, token] = authHeader.split(' ');
Expand All @@ -33,6 +35,6 @@ export default function ensureAuthenticated(

return next();
} catch {
throw new Error('Invalid JWT token');
throw new AppError('Invalid JWT token', 401);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sessionsRouter.post('/', async (request, response) => {

return response.json({ user: userWithoutPassword, token });
} catch (err) {
return response.status(400).json({ error: err.message });
return response.status(err.statusCode).json({ error: err.message });
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { compare } from 'bcryptjs';
import { sign } from 'jsonwebtoken';
import authConfig from '../config/auth';

import AppError from '../errors/AppError';

import User from '../models/User';

interface Request {
Expand All @@ -22,13 +24,13 @@ class AuthenticateUserService {
const user = await usersRepository.findOne({ where: { email } });

if (!user) {
throw Error('Incorrect email/password combination');
throw new AppError('Incorrect email/password combination', 401);
}

const passwordMatched = await compare(password, user.password);

if (!passwordMatched) {
throw Error('Incorrect email/password combination');
throw new AppError('Incorrect email/password combination', 401);
}

const { secret, expiresIn } = authConfig.jwt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { startOfHour } from 'date-fns';
import { getCustomRepository } from 'typeorm';

import AppError from '../errors/AppError';

import Appointment from '../models/Appointment';
import AppointmentsRepository from '../repositories/AppointmentsRepository';

Expand All @@ -20,7 +22,7 @@ class CreateAppointmentService {
);

if (findAppointmentInSameDate) {
throw Error('This appointment is already booked');
throw new AppError('This appointment is already booked');
}

const appointment = appointmentsRepository.create({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getRepository } from 'typeorm';
import { hash } from 'bcryptjs';

import AppError from '../errors/AppError';

import User from '../models/User';

interface Request {
Expand All @@ -18,7 +20,7 @@ class CreateUserService {
});

if (checkUserExists) {
throw Error('Email address already used.');
throw new AppError('Email address already used.');
}

const hashedPassword = await hash(password, 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { getRepository } from 'typeorm';
import path from 'path';
import fs from 'fs';

import AppError from '../errors/AppError';

import uploadConfig from '../config/upload';
import User from '../models/User';

Expand All @@ -17,7 +19,7 @@ class UpdateUserAvatarService {
const user = await usersRepository.findOne(user_id);

if (!user) {
throw new Error('Only authenticated user can change avatar');
throw new AppError('Only authenticated user can change avatar', 401);
}

if (user.avatar) {
Expand Down

0 comments on commit 618b305

Please sign in to comment.