Exception filters

Exception filters

HTTP exception filter λ ˆμ΄μ–΄μ™€ ν•΄λ‹Ή μ›Ή μ†ŒμΌ“ λ ˆμ΄μ–΄μ˜ μœ μΌν•œ 차이점은 HttpException을 λ°œμƒμ‹œν‚€λŠ” λŒ€μ‹  WsException을 μ‚¬μš©ν•΄μ•Ό ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

throw new WsException('Invalid credentials.');

info 힌트 WsException ν΄λž˜μŠ€λŠ”@nestjs/websockets νŒ¨ν‚€μ§€μ—μ„œ κ°€μ Έμ˜΅λ‹ˆλ‹€.

NestλŠ” λ°œμƒλœ μ˜ˆμ™Έλ₯Ό μ²˜λ¦¬ν•˜κ³  결과적으둜 λ‹€μŒκ³Ό 같은 ꡬ쑰의 exceptionλ©”μ‹œμ§€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

{
  status: 'error',
  message: 'Invalid credentials.'
}

Filters

맞좀 ν•„ν„° κΈ°λŠ₯도 μ§€μ›λ˜λ©° λ™μΌν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€. λ‹€μŒμ€ μˆ˜λ™μœΌλ‘œ μΈμŠ€ν„΄μŠ€ν™”λœ λ©”μ†Œλ“œ λ²”μœ„ ν•„ν„°λ₯Ό μ‚¬μš©ν•˜λŠ” μ˜ˆμž…λ‹ˆλ‹€ (클래슀 λ²”μœ„λ„ μž‘λ™).

@UseFilters(new WsExceptionFilter())
@SubscribeMessage('events')
onEvent(client, data: any): WsResponse<any> {
  const event = 'events';
  return { event, data };
}

Inheritance

일반적으둜 μ‘μš© ν”„λ‘œκ·Έλž¨ μš”κ΅¬ 사항을 μΆ©μ‘±ν•˜λ„λ‘ μ œμž‘λœ μ™„μ „νžˆ μ‚¬μš©μž μ§€μ •λœ μ˜ˆμ™Έ ν•„ν„°λ₯Ό λ§Œλ“­λ‹ˆλ‹€. 이미 κ΅¬ν˜„ 된 핡심 μ˜ˆμ™Έ ν•„ν„°λ₯Ό μž¬μ‚¬μš©ν•˜κ³  νŠΉμ • μš”μΈμ— 따라 λ™μž‘μ„ μž¬μ •μ˜ν•˜λ €λŠ” 경우 μ‚¬μš© 사둀가 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

μ˜ˆμ™Έ 처리λ₯Ό κΈ°λ³Έ 필터에 μœ„μž„ν•˜λ €λ©΄ BaseWsExceptionFilterλ₯Ό ν™•μž₯ν•˜κ³  상속 된 catch()λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄μ•Όν•©λ‹ˆλ‹€.

@@filename()
import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseWsExceptionFilter } from '@nestjs/websockets';

@Catch()
export class AllExceptionsFilter extends BaseWsExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    super.catch(exception, host);
  }
}
@@switch
import { Catch } from '@nestjs/common';
import { BaseWsExceptionFilter } from '@nestjs/websockets';

@Catch()
export class AllExceptionsFilter extends BaseWsExceptionFilter {
  catch(exception, host) {
    super.catch(exception, host);
  }
}

λΆ„λͺ…νžˆ, 당신은 λ§žμΆ€ν˜• λΉ„μ¦ˆλ‹ˆμŠ€ λ…Όλ¦¬λ‘œ λ‹€μ–‘ν•œ κ΅¬ν˜„μ„ κ°•ν™”ν•΄μ•Όν•©λ‹ˆλ‹€ (예: λ‹€μ–‘ν•œ 쑰건 μΆ”κ°€).

Last updated