Throw HTTP Exceptions

You can use ts-httpexceptions or similar module to throw an http exception. All exception will be intercepted by the Global error handler and are sent to the client.

Here an example:

import {Controller, Get, PathParams} from "@tsed/common";
import {BadRequest} from "ts-httpexceptions";

@Controller("/calendars")
export class CalendarCtrl {

    @Get("/:id")
    async get(
        @PathParams("id") id: number
    ): any {
    
        if (isNaN(+id)) {
            throw(new BadRequest("Not a number"));
        }
       
       return {id: id};
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

If id parameter is not an number, the method throw a Bad Request. This will produce a response with status code 400 and "Not a number" message.

Create custom exception

It also possible to create your own exception from any Exception of ts-httpexceptions and customize the response headers.

import {BadRequest} from "ts-httpexceptions";
import {IResponseError} from "@tsed/common";

export class RequiredUserName extends BadRequest implements IResponseError {
    headers = {};
    errors = [];

    constructor() {
        super("The name is required");
        this.headers["my-custom-header"] = "value";

        // you can also specify errors field for functional errors (like AJV validation).
        this.errors.push({
             dataPath: "",
             keyword: "required",
             message: "should have required property 'name'",
             modelName: "User",
             params: {
               missingProperty: "name"
             },
             schemaPath: "#/required"
        });
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

Then in your controller:

import {Controller, Post, BodyParams} from "@tsed/common";
import {RequiredUserName} from "./RequiredUserName";

@Controller("/calendars")
export class CalendarCtrl {

    @Post("/")
    async get(
        @BodyParams() user: User
    ): any {

        if (!user.name) {
            throw(new RequiredUserName());
        }

       return {id: id};
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18