# Exceptions

Ts.ED http exceptions provide classes to throw standard HTTP exceptions. These exceptions can be used on Controller, Middleware or injectable Service. Emitted exceptions will be handled by the PlatformExceptions and formatted to a response with the right status code and headers.

TIP

This module can be used in standalone with a pure Express/Node.js application.

# Installation

npm install @tsed/exceptions
// or
yarn add @tsed/exceptions
1
2
3

# Throwing standard exceptions

Here is two examples to throw exceptions based on this package in Ts.ED context or Express.js context:

    # Custom exception

    It's possible to create your own exception by creating a class which inherit from Exception or one of the built-in exception like BadRequest .

    Example:

    import {BadRequest} from "@tsed/exceptions";
    
    export class IDFormatException extends BadRequest {
      constructor() {
        super("ID format is not valid");
      }
    }
    
    1
    2
    3
    4
    5
    6
    7

    Since IDFormatException extends the BadRequest , it will work seamlessly with the built-in exception handler, and therefore we can use it inside a controller method.

    import {PathParams} from "@tsed/platform-params";
    import {Get} from "@tsed/schema";
    import {Controller, Inject} from "@tsed/di";
    import {CalendarsService} from "../services/CalendarsService";
    import {IDFormatException} from "../errors/IDFormatException";
    
    @Controller("/calendars")
    export class CalendarCtrl {
      @Inject()
      calendarsService: CalendarsService;
    
      @Get("/:id")
      async get(@PathParams("id") id: number) {
        if (isNaN(+id)) {
          throw new IDFormatException();
        }
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    # Built-in exceptions

    Ts.ED provides a set of standard exceptions that inherit from the base Exception . These are exposed from the @tsed/exceptions package, and represent many of the most common HTTP exceptions:

    # Redirections (3xx)

    Loading in progress...

    # Client errors (4xx)

    Loading in progress...

    # Server errors (5xx)

    Loading in progress...

    # Exception filter

    All errors are intercepted by the PlatformExceptionMiddleware .

    By default, all HTTP Exceptions are automatically sent to the client, and technical errors are sent as Internal Server Error.

    The Platform API provides Catch decorator to catch error. It lets you control the exact flow of control and the content of the response sent back to the client.

    Let's create an exception filter that is responsible for catching exceptions which are an instance of the Exception class, and implementing custom response logic for them.

    To do this, we'll need to access the underlying platform Request and Response objects by using the Context decorator. We'll access the Request object, so we can pull out the original url and include that in the logging information. We'll use the Response object to take direct control of the response that is sent, using the response.body() method.

    import {PlatformContext, ResponseErrorObject} from "@tsed/common";
    import {Catch, ExceptionFilterMethods} from "@tsed/platform-exceptions";
    import {Exception} from "@tsed/exceptions";
    
    @Catch(Exception)
    export class HttpExceptionFilter implements ExceptionFilterMethods {
      catch(exception: Exception, ctx: PlatformContext) {
        const {response, logger} = ctx;
        const error = this.mapError(exception);
        const headers = this.getHeaders(exception);
    
        logger.error({
          error
        });
    
        response.setHeaders(headers).status(error.status).body(error);
      }
    
      mapError(error: any) {
        return {
          name: error.origin?.name || error.name,
          message: error.message,
          status: error.status || 500,
          errors: this.getErrors(error)
        };
      }
    
      protected getErrors(error: any) {
        return [error, error.origin].filter(Boolean).reduce((errs, {errors}: ResponseErrorObject) => {
          return [...errs, ...(errors || [])];
        }, []);
      }
    
      protected getHeaders(error: any) {
        return [error, error.origin].filter(Boolean).reduce((obj, {headers}: ResponseErrorObject) => {
          return {
            ...obj,
            ...(headers || {})
          };
        }, {});
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42

    note

    All exception filters should implement the generic ExceptionFilterMethods<T> interface. This requires you to provide the catch(exception: T, ctx: Context) method with its indicated signature. T indicates the type of the exception.

    The @Catch(Exception) decorator binds the required metadata to the exception filter, telling Ts.ED that this particular filter is looking for exceptions of type Exception and nothing else. The Catch decorator may take a single parameter, or a comma-separated list. This lets you set up the filter for several types of exceptions at once.

    If you want to catch all errors, just use the Catch decorator with the Error class:

    import {PlatformContext, ResponseErrorObject} from "@tsed/common";
    import {Catch, ExceptionFilterMethods} from "@tsed/platform-exceptions";
    import {Exception} from "@tsed/exceptions";
    
    @Catch(Error)
    export class ErrorFilter implements ExceptionFilterMethods {
      catch(exception: Exception, ctx: PlatformContext) {
        const {response, logger} = ctx;
        const error = this.mapError(exception);
        const headers = this.getHeaders(exception);
    
        logger.error({
          error
        });
    
        response
          .setHeaders(headers)
          .status(error.status || 500)
          .body(error);
      }
    
      mapError(error: any) {
        return {
          name: error.origin?.name || error.name,
          message: error.message,
          status: error.status || 500,
          errors: this.getErrors(error)
        };
      }
    
      protected getErrors(error: any) {
        return [error, error.origin].filter(Boolean).reduce((errs, {errors}: ResponseErrorObject) => {
          return [...errs, ...(errors || [])];
        }, []);
      }
    
      protected getHeaders(error: any) {
        return [error, error.origin].filter(Boolean).reduce((obj, {headers}: ResponseErrorObject) => {
          return {
            ...obj,
            ...(headers || {})
          };
        }, {});
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45

    # 404 ResourceNotFound

    Ts.ED throw a ResourceNotFound error when nothing routes are resolved by the router. By using Exception filter, it is now possible to manage this error and customize the response sent to your consumer.

    Create a new ResourceNotFoundFilter in the filters directories and copy/paste this example:

    import {PlatformContext, ResourceNotFound} from "@tsed/common";
    import {Catch, ExceptionFilterMethods} from "@tsed/platform-exceptions";
    
    @Catch(ResourceNotFound)
    export class ResourceNotFoundFilter implements ExceptionFilterMethods {
      async catch(exception: ResourceNotFound, ctx: PlatformContext) {
        const {response} = ctx;
    
        const obj = {
          status: exception.status,
          message: exception.message,
          url: exception.url
        };
        // Json response
        response.status(exception.status).body(obj);
    
        // Or with ejs/handlers/etc...
        const html = await response.render("404.ejs", obj);
        response.status(exception.status).body(html);
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21

    WARNING

    response.render() requires to configure the template engine before. See our page over Templating engine installation for more details.

    Then import the custom filter in your server:

    import {Inject} from "@tsed/di";
    import {Configuration, PlatformApplication} from "@tsed/common";
    import "./filters/ResourceNotFoundFilter"; // Importing filter with ES6 import is enough
    
    @Configuration({
      // ...
    })
    export class Server {}
    
    1
    2
    3
    4
    5
    6
    7
    8

    Last Updated: 2/5/2023, 1:16:22 PM

    Other topics