From 94175af0baacc19af65aa48346d08b5680167678 Mon Sep 17 00:00:00 2001 From: Nik Shevchenko Date: Sat, 23 Nov 2024 08:30:21 +0100 Subject: [PATCH] feat: added unprocessable entity (422) http error (#1459) --- README.md | 1 + docs/lang/chinese/README.md | 1 + lang/chinese/README.md | 1 + src/http-error/UnprocessableEntityError.ts | 15 +++++++++++++++ src/index.ts | 1 + 5 files changed, 19 insertions(+) create mode 100644 src/http-error/UnprocessableEntityError.ts diff --git a/README.md b/README.md index bd0364de..6248f191 100644 --- a/README.md +++ b/README.md @@ -766,6 +766,7 @@ There are set of prepared errors you can use: - NotAcceptableError - NotFoundError - UnauthorizedError +- UnprocessableEntityError You can also create and use your own errors by extending `HttpError` class. To define the data returned to the client, you could define a toJSON method in your error. diff --git a/docs/lang/chinese/README.md b/docs/lang/chinese/README.md index 22e851f1..807f629f 100644 --- a/docs/lang/chinese/README.md +++ b/docs/lang/chinese/README.md @@ -724,6 +724,7 @@ getOne(@Param("id") id: number) { - NotAcceptableError - NotFoundError - UnauthorizedError +- UnprocessableEntityError 可以继承 `HttpError` 类自行创建使用 error。 也可实现一个 toJson 函数定义返回给客户端的数据。 diff --git a/lang/chinese/README.md b/lang/chinese/README.md index 5f54f7c0..13a63e55 100644 --- a/lang/chinese/README.md +++ b/lang/chinese/README.md @@ -727,6 +727,7 @@ getOne(@Param("id") id: number) { - NotAcceptableError - NotFoundError - UnauthorizedError +- UnprocessableEntityError 可以继承 `HttpError` 类自行创建使用 error。 也可实现一个 toJson 函数定义返回给客户端的数据。 diff --git a/src/http-error/UnprocessableEntityError.ts b/src/http-error/UnprocessableEntityError.ts new file mode 100644 index 00000000..0db36e43 --- /dev/null +++ b/src/http-error/UnprocessableEntityError.ts @@ -0,0 +1,15 @@ +import { HttpError } from './HttpError'; + +/** + * Exception for 422 HTTP error. + */ +export class UnprocessableEntityError extends HttpError { + name = 'UnprocessableEntityError'; + + constructor(message?: string) { + super(422); + Object.setPrototypeOf(this, UnprocessableEntityError.prototype); + + if (message) this.message = message; + } +} diff --git a/src/index.ts b/src/index.ts index 06549c2b..2f9bc20e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -71,6 +71,7 @@ export * from './http-error/NotAcceptableError'; export * from './http-error/MethodNotAllowedError'; export * from './http-error/NotFoundError'; export * from './http-error/UnauthorizedError'; +export * from './http-error/UnprocessableEntityError'; export * from './driver/express/ExpressMiddlewareInterface'; export * from './driver/express/ExpressErrorMiddlewareInterface';