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
Was this helpful?