Skip to content

Commit

Permalink
Merge pull request #22 from 5th-Neordinary-HACKATHON-MEETA/issue/19
Browse files Browse the repository at this point in the history
Feat: exception 핸들러 적용 - #19
  • Loading branch information
cookie-meringue authored Nov 25, 2023
2 parents e6a9aa5 + 94702b7 commit 04d4619
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
Injectable,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { JwtService } from '@nestjs/jwt';
import { Response } from 'express';
Expand All @@ -13,6 +9,7 @@ import { ResponseBody, SuccessResponse } from '../common/response/response';
import { RESPONSE_CODE } from '../common/response/response.code';
import { JoinDto } from './dtos/request/join.dto';
import { LoginDto } from './dtos/request/login.dto';
import { Exception } from '../common/response/exception';

@Injectable()
export class AuthService {
Expand All @@ -28,7 +25,7 @@ export class AuthService {
where: [{ id: joinDto.id }, { nickname: joinDto.nickname }],
})
) {
throw new UnauthorizedException('user already exists.');
throw new Exception(RESPONSE_CODE[4090], null);
}

const userInDto: User = this.usersRepository.create(joinDto);
Expand All @@ -38,11 +35,13 @@ export class AuthService {

async login(loginDto: LoginDto, response: Response): Promise<ResponseBody> {
const userData = await this.usersRepository.findOne({
where: [{ id: loginDto.id }, { password: loginDto.password }],
select: { id: true, password: true, nickname: true },
where: {
id: loginDto.id,
password: loginDto.password,
},
});
if (!userData) {
throw new NotFoundException('user not found.');
throw new Exception(RESPONSE_CODE[4040], null);
}

const payload: Payload = {
Expand Down
10 changes: 10 additions & 0 deletions src/common/response/response.code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ export const RESPONSE_CODE: Record<number, ResponseCode> = {
message: '서버 에러',
status: HttpStatus.INTERNAL_SERVER_ERROR,
},
4040: {
code: 4040,
message: '유저 정보가 존재하지 않습니다.',
status: HttpStatus.NOT_FOUND,
},
4090: {
code: 4090,
message: '이미 존재하는 유저입니다.',
status: HttpStatus.CONFLICT,
},
};

0 comments on commit 04d4619

Please sign in to comment.