UNPKG

1.18 kBPlain TextView Raw
1import { AppError, ErrorData } from '@naturalcycles/js-lib'
2import { ValidationErrorItem } from 'joi'
3
4/**
5 * Example of ValidationErrorItem:
6 *
7 * {
8 * message: '"temperature" must be larger than or equal to 33',
9 * path: [ 'entries', 10, 'temperature' ],
10 * type: 'number.min',
11 * context: { limit: 33, value: 30, key: 'temperature', label: 'temperature' }
12 * }
13 */
14export interface JoiValidationErrorData extends ErrorData {
15 joiValidationErrorItems: ValidationErrorItem[]
16 joiValidationObjectName?: string
17 joiValidationObjectId?: string
18}
19
20export class JoiValidationError extends AppError<JoiValidationErrorData> {
21 constructor(message: string, data: JoiValidationErrorData) {
22 super(message, data)
23
24 this.constructor = JoiValidationError
25 ;(this as any).__proto__ = JoiValidationError.prototype
26 Object.defineProperty(this, 'name', {
27 value: this.constructor.name,
28 configurable: true,
29 })
30
31 if (Error.captureStackTrace) {
32 Error.captureStackTrace(this, this.constructor)
33 } else {
34 Object.defineProperty(this, 'stack', {
35 value: new Error().stack, // eslint-disable-line unicorn/error-message
36 })
37 }
38 }
39}