Boa noite pessoal,
Estou precisando verificar se já tem algum usuário com o mesmo email cadastro. Estou tendo o seguinte erro: [ERROR] 225009 UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<AppError>".
Código CreateUserService.ts
import { User } from '@prisma/client';
import { AppError } from '../shared/errors/AppError';
import prismaClient from '../prisma';
import { hash } from 'bcryptjs';
import { CreateUserDTO } from '../dtos/user/CreateUserDto';
class CreateUserService {
async execute({
name,
email,
password,
admin,
}: CreateUserDTO): Promise<User> {
const passwodHash = await hash(password, 8);
const userAlreadyExists = await prismaClient.user.findUnique({
where: {
email,
},
});
if (userAlreadyExists) {
throw new AppError('User already exists!');
}
const user = await prismaClient.user.create({
data: {
name,
email,
password: passwodHash,
admin,
},
});
return user;
}
}
export { CreateUserService };
CÓDIGO AppError.ts:
class AppError {
public readonly message: string;
public readonly statusCode: number;
constructor(message: string, statusCode = 400) {
this.message = message;
this.statusCode = statusCode;
}
}
export { AppError };
Se alguém puder me ajudar, agradeço!