import type { HttpResponseErrorMessage } from '../httpApi/internals/httpResponseMessage.js'; import type { MessageLocation } from '../common/messageLocation.js'; import type { SpecifyErrorKey } from './specifyErrors.js'; import { specifyErrors } from './specifyErrors.js'; import { getSpecifyErrorDefinition } from './getSpecifyErrorDefinition.js'; export class SpecifyError extends Error { public readonly errorKey: SpecifyErrorKey; public readonly originalError: unknown | undefined; public readonly httpStatusCode: number; public readonly location: MessageLocation | undefined; constructor({ publicMessage, errorKey, originalError, httpStatusCode, location, }: { publicMessage: string; errorKey?: SpecifyErrorKey; originalError?: Error; httpStatusCode?: number; location?: MessageLocation; }) { super(publicMessage); this.name = 'SpecifyError'; this.errorKey = errorKey || specifyErrors.UNEXPECTED_ERROR.errorKey; this.originalError = originalError; this.httpStatusCode = httpStatusCode || getSpecifyErrorDefinition(this.errorKey).statusCode; this.location = location; } getHttpStatusCode() { return this.httpStatusCode; } getHttpMessage(): HttpResponseErrorMessage { return { type: 'error', errorKey: this.errorKey, content: this.message, location: this.location, }; } }