Skip to content

TwilioExceptionFilter

Defined in: lib/exception/twilio-exception.filter.ts:157

Maps Twilio SDK RestException errors to Nest HTTP responses, preserving the original HTTP status, Twilio error code, message, moreInfo link, and details payload as a TwilioErrorResponse JSON body.

Uses @Catch() (no argument) so it can be registered globally without a hard runtime dependency on the Twilio SDK’s RestException class. It recognizes Twilio errors structurally via isTwilioRestException (see that function’s docs for why). Any exception that isn’t a Twilio RestException is delegated to Nest’s default handling via BaseExceptionFilter, so registering this filter globally never swallows unrelated errors.

Per-route registration

import { UseFilters } from '@nestjs/common';
import { TwilioExceptionFilter } from 'nestjs-twilio';
@Controller('sms')
@UseFilters(TwilioExceptionFilter)
export class SmsController {
constructor(@InjectTwilio() private readonly twilio: TwilioService) {}
@Post()
send() {
return this.twilio.client.messages.create({ ... });
}
}

Global registration via useGlobalFilters

import { HttpAdapterHost } from '@nestjs/core';
import { TwilioExceptionFilter } from 'nestjs-twilio';
const app = await NestFactory.create(AppModule);
const { httpAdapter } = app.get(HttpAdapterHost);
app.useGlobalFilters(new TwilioExceptionFilter(httpAdapter));
await app.listen(3000);

Global registration via the APP_FILTER provider

import { APP_FILTER } from '@nestjs/core';
import { TwilioExceptionFilter } from 'nestjs-twilio';
@Module({
providers: [{ provide: APP_FILTER, useClass: TwilioExceptionFilter }],
})
export class AppModule {}
  • BaseExceptionFilter<unknown>
  • ExceptionFilter<RestException>
new TwilioExceptionFilter(applicationRef?): TwilioExceptionFilter;

Defined in: node_modules/.pnpm/@nestjs+core@12.0.4_@nestjs+common@12.0.4_reflect-metadata@0.2.2_rxjs@7.8.2_supports-co_0d6aef3296214b44da550531eb925767/node_modules/@nestjs/core/exceptions/base-exception-filter.d.ts:8

Parameter Type
applicationRef? HttpServer<any, any, any>

TwilioExceptionFilter

BaseExceptionFilter<unknown>.constructor
catch(exception, host): void;

Defined in: lib/exception/twilio-exception.filter.ts:161

Parameter Type
exception unknown
host ArgumentsHost

void

ExceptionFilter.catch
BaseExceptionFilter.catch

handleUnknownError(
exception,
host,
applicationRef
): void;

Defined in: node_modules/.pnpm/@nestjs+core@12.0.4_@nestjs+common@12.0.4_reflect-metadata@0.2.2_rxjs@7.8.2_supports-co_0d6aef3296214b44da550531eb925767/node_modules/@nestjs/core/exceptions/base-exception-filter.d.ts:10

Parameter Type
exception unknown
host ArgumentsHost
applicationRef | HttpServer<any, any, any> | AbstractHttpAdapter<any, any, any>

void

BaseExceptionFilter.handleUnknownError

isExceptionObject(err): err is Error;

Defined in: node_modules/.pnpm/@nestjs+core@12.0.4_@nestjs+common@12.0.4_reflect-metadata@0.2.2_rxjs@7.8.2_supports-co_0d6aef3296214b44da550531eb925767/node_modules/@nestjs/core/exceptions/base-exception-filter.d.ts:11

Parameter Type
err any

err is Error

BaseExceptionFilter.isExceptionObject

isHttpError(err): err is { message: string; statusCode: number };

Defined in: node_modules/.pnpm/@nestjs+core@12.0.4_@nestjs+common@12.0.4_reflect-metadata@0.2.2_rxjs@7.8.2_supports-co_0d6aef3296214b44da550531eb925767/node_modules/@nestjs/core/exceptions/base-exception-filter.d.ts:16

Checks if the thrown error is a FastifyError or comes from the “http-errors” library.

Parameter Type Description
err any error object

err is { message: string; statusCode: number }

BaseExceptionFilter.isHttpError