inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

Slack 클론 코딩[백엔드 with NestJS + TypeORM]

class-validator

HttpExceptionFilter에 단순 HttpException 예외처리도 필요하지 않나요?

해결된 질문

365

최석우

작성한 질문수 20

0

HttpExceptionFilter에서 에러처리시

exception.getStatus() 로 err 변수값을 넣을떄

HttpException 의 경우 string 타입

BadRequestException, UnauthorizedException 와 같이 미리 정의된 경우 { message: any; statusCode: number } 타입

class-validator의 경우 { error: string; statusCode: 400; message: string[] } 타입의 3가지 형태가 되는데

이번강의에서 string 타입 자체를 제거하셧는데 HttpException 를 사용하게되면 에러 메시지가 제대로 안 날라 가게 됩니다.

class-validator를 도입하면서 HttpException 케이스를 제거 하신거 같은데 해당 예외도 포함은 되어있어야 하지 않나요?
class-validator 가 완전히 HttpException 를 대체하게 한다면 HttpException를 사용못하게 막을 방법이 있을까요?

import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
} from '@nestjs/common';
import { Response } from 'express';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const status = exception.getStatus();
    const err = exception.getResponse() as
      | string
      | { message: any; statusCode: number }
      | { error: string; statusCode: 400; message: string[] };

    //class-validator
    if (typeof err !== 'string' && err.statusCode === 400) {
      return response.status(status).json({
        success: false,
        code: err.statusCode,
        data: err.message,
      });
    }

    //HttpException
    if (typeof err == 'string') {
      return response.status(status).json({
        success: false,
        code: status,
        data: err,
      });
    }

    //BadRequestException, UnauthorizedException
    return response.status(status).json({
      success: false,
      code: status,
      data: err.message,
    });
  }
}

node.js express nestjs typeorm

답변 1

0

제로초(조현영)

네, err 자체가 객체가 아니라 string인 케이스도 처리 필요하겠네요.

강의자료는 어디서 다운받나요?

0

114

4

질문 있습니다.

0

299

3

(강의 5:42 질문) providers를 통한 여러 개의 인스턴스 생성 & exports 통한 싱글톤 생성

0

154

2

코드 편집기 확장 프로그램

0

209

2

(질문)비밀 저장소에 접근하기 위한 인증 정보는 로컬 .env에 저장하는지?

0

146

2

(질문)외부 저장소를 통한 환경변수 불러오기 비동기 질문

0

169

3

로그인을 해도 LoggedInGuard쪽에서 false값이 나옵니다.

0

152

2

로그인방법이 고민됩니다.

0

190

2

yarn seed 명령어 실행 시 데이터 삽입 안됨

0

297

4

yarn run db:create 시에 발생하는 데코레이터 오류

0

242

2

npm run db:create 시에 발생하는 decorating 오류

0

232

2

RxJS 디버깅 질문 있습니다.

0

189

3

CacheManager에 대해 질문 있습니다.

0

175

2

로깅은 어떻게 하는게 효율적일까요?

0

224

1

CORS 질문 있습니다.

0

418

2

쿠키 옵션에 대해서 질문 있습니다.

0

184

2

로그아웃 요청이 403 forbidden 에러가 나는데 왜그런걸까요??

0

449

1

401 unauthorized문제

0

289

1

가드의 장점에 대해서 질문이 있습니다.

0

225

1

로그 관리에 대해 질문 있습니다.

0

252

2

CORS 에러 질문 있습니다.

0

319

2

배포 환경 DB 연결 질문 있습니다.

0

411

2

socket io 미 연결 문제 (nest & flutter)

1

1156

3

no elements in sequence 에러 관해서 질문이 있습니다.

0

454

1