UNPKG

@affinidi-tdk/iota-browser

Version:

Browser module to fetch data through Affinidi Iota Framework

70 lines 2.94 kB
import { errorEventSchema } from '../validators/events'; import { Logger } from '@affinidi-tdk/common/helpers'; export class IotaError extends Error { code; correlationId; issue; constructor(message, code, correlationId, issue) { super(message); this.code = code; this.correlationId = correlationId; this.issue = issue; } } export var IotaErrorCode; (function (IotaErrorCode) { IotaErrorCode["UNEXPECTED_ERROR"] = "UnexpectedError"; IotaErrorCode["DATA_REQUEST_ERROR"] = "DataRequestError"; IotaErrorCode["IOTA_CLIENT_NOT_STARTED"] = "IotaClientNotStarted"; IotaErrorCode["NOT_AUTHENTICATED"] = "NotAuthenticated"; IotaErrorCode["IOTA_SESSION_NOT_INITIALIZED"] = "IotaSessionNotInitialized"; IotaErrorCode["UNABLE_TO_CONNECT_WITH_PROVIDED_CREDENTIALS"] = "UnableToConnectWithProvidedCredentials"; })(IotaErrorCode || (IotaErrorCode = {})); export var InternalErrorCode; (function (InternalErrorCode) { InternalErrorCode["SIGNED_REQUEST_EVENT"] = "SignedRequestEvent"; InternalErrorCode["SIGNED_REQUEST_JWT"] = "SignedRequestJWT"; InternalErrorCode["RESPONSE_CALLBACK_EVENT"] = "ResponseCallbackEvent"; InternalErrorCode["PARSING_VERIFIABLE_PRESENTATION"] = "ParsingVerifiablePresentation"; InternalErrorCode["PARSING_PRESENTATION_SUBMISSION"] = "ParsingPresentationSubmission"; InternalErrorCode["PARSING_ERROR_EVENT"] = "ParsingErrorEvent"; })(InternalErrorCode || (InternalErrorCode = {})); function getIssue(errorEvent) { return errorEvent.error.details[0].issue && errorEvent.error.details[0].issue.length > 0 ? errorEvent.error.details[0].issue : errorEvent.error.message; } export function newUnexpectedError(internalErrorCode, correlationId) { const msg = `Unexpected error occured. Error Code: ${internalErrorCode}`; const error = new IotaError(msg, IotaErrorCode.UNEXPECTED_ERROR, correlationId, internalErrorCode); Logger.debug(msg, error); return error; } export function newIotaError(iotaErrorCode) { const msg = `Error setting up Iota channel. Error Code: ${iotaErrorCode}`; const error = new IotaError(msg, iotaErrorCode); Logger.debug(msg, error); return error; } export function newRequestError(event) { const issue = getIssue(event); const msg = `Error received for request ${event.correlationId}: ${issue}`; const error = new IotaError(msg, IotaErrorCode.DATA_REQUEST_ERROR, event.correlationId, issue); Logger.debug(msg, error); return error; } export function throwEventError(event) { let errorEvent; try { errorEvent = errorEventSchema.parse(event); } catch (e) { if (e instanceof Error) { Logger.debug(e.message); } throw newUnexpectedError(InternalErrorCode.PARSING_ERROR_EVENT, event.correlationId); } throw newRequestError(event); } //# sourceMappingURL=error.js.map