UNPKG

@bitblit/ratchet-warden-common

Version:

Typescript library to simplify using simplewebauthn and secondary auth methods over GraphQL

94 lines 2.89 kB
import { StringRatchet } from "@bitblit/ratchet-common/lang/string-ratchet"; import { WardenErrorCode } from "./warden-error-codes"; export class WardenError extends Error { static WARDEN_ERROR_FLAG_KEY = '__wardenErrorFlag'; _errorCode; _internalMessage; _publicMessage; _details; _wrappedError; constructor(code, internalMessage, publicMessage, wrapped, details) { super(StringRatchet.trimToNull(internalMessage) ?? 'Warden Security Error'); this._errorCode = code; this._internalMessage = StringRatchet.trimToNull(internalMessage) ?? 'Warden Security Error'; this._publicMessage = StringRatchet.trimToNull(publicMessage) ?? this._internalMessage; this._details = details; this._wrappedError = wrapped; this[WardenError.WARDEN_ERROR_FLAG_KEY] = true; } withFormattedInternalErrorMessage(format, ...input) { this.setFormattedInternalErrorMessage(format, ...input); return this; } setFormattedInternalErrorMessage(format, ...input) { const msg = StringRatchet.format(format, ...input); this._internalMessage = msg; } withFormattedPublicErrorMessage(format, ...input) { this.setFormattedPublicErrorMessage(format, ...input); return this; } setFormattedPublicErrorMessage(format, ...input) { const msg = StringRatchet.format(format, ...input); this._publicMessage = msg; } withErrorCode(code) { this.errorCode = code; return this; } withDetails(details) { this._details = details; return this; } withWrappedError(err) { this._wrappedError = err; return this; } isWrappedError() { return !!this._wrappedError; } static wrapError(err) { let rval = null; if (WardenError.objectIsWardenError(err)) { rval = err; } else { rval = new WardenError(WardenErrorCode.Unspecified).withWrappedError(err); } return rval; } static objectIsWardenError(obj) { return obj && obj[WardenError.WARDEN_ERROR_FLAG_KEY] === true; } get errorCode() { return this._errorCode; } set errorCode(value) { this._errorCode = value; } get publicMessage() { return this._publicMessage; } set publicMessage(value) { this._publicMessage = value; } get internalMessage() { return this._internalMessage; } set internalMessage(value) { this._internalMessage = value; } get details() { return this._details; } set details(value) { this._details = value; } get wrappedError() { return this._wrappedError; } set wrappedError(value) { this._wrappedError = value; } } //# sourceMappingURL=warden-error.js.map