未経験からのNestJS入門|例外
概要
NestJSには、アプリケーション全体で未処理の例外を処理することができる例外レイヤーという機能が存在しています。
これはアプリケーション全体を通して処理されない場合に、例外がキャッチされ適切な応答に自動的にレスポンスとして送信されます。
例外をスローする
NestJS ではデフォルトで HttpException の名前でエラークラスが提供されており、@nestjs/common パッケージから提供されています。
それぞれ利用するのはとても簡単で、ルーティングを処理するコントローラーの関数で、エラークラスを呼び出してあげるだけです。
例として以下のような感じです。前回作成した users コントローラーをそのまま流用します。
// /src/users/users.controller.ts
import { Controller, Get, HttpException, HttpStatus } from '@nestjs/common';
import { UsersService } from './users.service'
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) { }
@Get()
getUser(): string{
return this.usersService.getUser();
}
@Get('throw')
getException(): string{
throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
}
}
コードの追記が完了したら、http://localhost:3000/users/throw にアクセスしてください。
コード解説
前述の通り、NestJs は@nesjs/common パッケージにて HttpException の名前でエラークラスを提供してくれています。
ここでは、HttpException エラークラスをインポートします。
ここで HttpStatus も一緒にインポートしています。
HttpStatus は、ヘルパー列挙型でエラーコードを提供してくれています。
可読性が上がるので積極的に利用していきましょう。
import { Controller, Get, HttpException, HttpStatus } from '@nestjs/common';
HttpException のコンストラクタを呼び出してあげます。
それぞれ以下の引数を設定できます。
- string 型の、JSON レスポンスボディを設定します。デフォルトではレスポンス JSON の message プロパティに設定されます。
- string 型の、HTTP ステータスコードを設定します。デフォルトではレスポンス JSON の statusCode に設定されます。
throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
基本はこの引数で問題ありませんが、レスポンス JSON のプロパティを変更したい場合オーバーライドすることで、解決できます。
// /src/users/users.controller.ts
import { Controller, Get, HttpException, HttpStatus } from '@nestjs/common';
import { UsersService } from './users.service';
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get()
getUser(): string {
return this.usersService.getUser();
}
@Get('throw')
getException(): string {
throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
}
@Get('override')
getOverRideException(): string {
throw new HttpException({ status: HttpStatus.FORBIDDEN, error: 'override custom' }, HttpStatus.FORBIDDEN,
);
}
}
コードの作成が完了したら、
http://localhost:3000/users/override にアクセスしてください。
以下が表示されたら成功です。
カスタム例外をスローする
NestJs は組み込みの例外処理が存在しており、あまり自作する必要はないかと思いますが、自分でカスタム例外をレスポンスとして返すことができます。
カスタム例外は、HttpException を継承した例外クラスを作成する必要があります。
// /src/exception/forbidden.exception.ts
import { HttpException, HttpStatus } from '@nestjs/common';
export class ForbiddenException extends HttpException {
constructor() {
super('custom exception', HttpStatus.FORBIDDEN);
}
}
// /src/users/users.controller.ts
import { Controller, Get, HttpException, HttpStatus } from '@nestjs/common';
import { ForbiddenException } from '../exception/forbidden.exception';
import { UsersService } from './users.service';
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get()
getUser(): string {
return this.usersService.getUser();
}
@Get('throw')
getException(): string {
throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
}
@Get('override')
getOverRideException(): string {
throw new HttpException({ status: HttpStatus.FORBIDDEN, error: 'override custom' }, HttpStatus.FORBIDDEN,
);
}
@Get('custom')
getCustomException(): string {
throw new ForbiddenException();
}
}
コードの追記が完了したら、
http://localhost:3000/users/custom にアクセスしてください。
以下が表示されていたら完成です。
コード解説
export class ForbiddenException extends HttpException {
constructor() {
super('Forbidden', HttpStatus.FORBIDDEN);
}
}
例外クラスを作成するためには HttpException を継承し、拡張していきます。
getCustomException(): string{
throw new ForbiddenException();
}
カスタム例外クラスの使い方は簡単で、例外を発生させたいところで例外クラスを呼び出すだけです。
まとめ
NestJs はデフォルトで例外機能をあらかた準備しているので、それを利用するだけで簡単に例外時処理を簡単に作成できます。