Outline
export function TypedException<T extends object>(
status: number | "2XX" | "3XX" | "4XX" | "5XX",
description?: string,
): MethodDecorator;
Exception decorator only for swagger documents.
TypedException
is a decorator function describing HTTP exception and its type which could be occured in a controller method. For reference, this decorator function does not affect to the method's behavior, but affects to the swagger documents generation, or SDK functions when propagation mode being used.
How to use
import { Controller } from "@nestjs/common";
import typia, { TypeGuardError } from "typia";
import {
TypedBody,
TypedException,
TypedParam,
TypedRoute,
} from "@nestia/core";
import { IBbsArticle } from "@api/lib/structures/IBbsArticle";
import { IInternalServerError } from "@api/lib/structures/IInternalServerError";
import { INotFound } from "@api/lib/structures/INotFound";
import { IUnprocessibleEntity } from "@api/lib/structures/IUnprocessibleEntity";
@Controller("exception")
export class ExceptionController {
@TypedRoute.Post(":section/typed")
@TypedException<TypeGuardError>(400, "invalid request")
@TypedException<INotFound>(404, "unable to find the matched section")
@TypedException<IUnprocessibleEntity>(428)
@TypedException<IInternalServerError>("5XX", "internal server error")
public async typed(
@TypedParam("section") section: string,
@TypedBody() input: IBbsArticle.IStore,
): Promise<IBbsArticle> {
section;
input;
return typia.random<IBbsArticle>();
}
}
Just call TypedException()
function with target type and status code.
If you want to add description, you can add it as second parameter.
For reference, swagger allows to use special pattern like 2XX
, 3XX
, 4XX
, 5XX
for status code.
Swagger Example
Here is an example of swagger documents utilizing the @TypedException()
decorator.