import { TSDLErrorPackage } from "../../types/common"; import { Source } from "../../types/error"; import { errorCodes, errorCodesReversed } from "./errorCodes"; export class TSDLError { private $code: keyof typeof errorCodes; private $message?: string; private $validationError?: TValidationError; private $source: Source = "application"; constructor( code: | keyof typeof errorCodes | (typeof errorCodes)[keyof typeof errorCodes], message?: string ) { this.$message = message; if (typeof code === "number") { const match = errorCodesReversed[code]; if (match == null || !(match in errorCodes)) { throw InvalidCodeError(); } this.$code = match; } else { if (!(code in errorCodes)) { throw InvalidCodeError(); } this.$code = code; } } setSource(source: Source) { this.$source = source; return this; } setMessage(message?: string) { this.$message = message; return this; } setValidationError(error?: TValidationError) { this.$validationError = error; return this; } get code() { return this.$code; } get numberCode() { return errorCodes[this.$code]; } get message() { return this.$message; } get validationError() { return this.$validationError; } get semanticErrorMessage() { return `Error: ${this.$code}. Source: ${this.$source}`; } package(): TSDLErrorPackage { return { $schema: "TSDLError", code: this.$code, message: this.$message, validationError: this.$validationError, source: this.$source, } satisfies TSDLErrorPackage; } toString(): string { return JSON.stringify(this.package()); } static fromPackage( pkg: TSDLErrorPackage ) { return new TSDLError(pkg.code) .setMessage(pkg.message) .setValidationError(pkg.validationError); } } /** @internal */ function InvalidCodeError() { return new Error("Invalid code"); }