UNPKG

@azure/core-amqp

Version:

Common library for amqp based azure sdks like @azure/event-hubs.

378 lines (377 loc) • 21.1 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var errors_exports = {}; __export(errors_exports, { ConditionErrorNameMapper: () => ConditionErrorNameMapper, ConditionStatusMapper: () => ConditionStatusMapper, ErrorNameConditionMapper: () => ErrorNameConditionMapper, MessagingError: () => MessagingError, SystemErrorConditionMapper: () => SystemErrorConditionMapper, isMessagingError: () => isMessagingError, isSystemError: () => isSystemError, retryableErrors: () => retryableErrors, translate: () => translate }); module.exports = __toCommonJS(errors_exports); var import_rhea_promise = require("rhea-promise"); var import_core_util = require("@azure/core-util"); var import_utils = require("./util/utils.js"); var ConditionStatusMapper = ((ConditionStatusMapper2) => { ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:timeout"] = import_rhea_promise.AmqpResponseStatusCode.RequestTimeout] = "com.microsoft:timeout"; ConditionStatusMapper2[ConditionStatusMapper2["amqp:not-found"] = import_rhea_promise.AmqpResponseStatusCode.NotFound] = "amqp:not-found"; ConditionStatusMapper2[ConditionStatusMapper2["amqp:not-implemented"] = import_rhea_promise.AmqpResponseStatusCode.NotImplemented] = "amqp:not-implemented"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:entity-already-exists"] = import_rhea_promise.AmqpResponseStatusCode.Conflict] = "com.microsoft:entity-already-exists"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:message-lock-lost"] = import_rhea_promise.AmqpResponseStatusCode.Gone] = "com.microsoft:message-lock-lost"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:session-lock-lost"] = import_rhea_promise.AmqpResponseStatusCode.Gone] = "com.microsoft:session-lock-lost"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:no-matching-subscription"] = import_rhea_promise.AmqpResponseStatusCode.InternalServerError] = "com.microsoft:no-matching-subscription"; ConditionStatusMapper2[ConditionStatusMapper2["amqp:link:message-size-exceeded"] = import_rhea_promise.AmqpResponseStatusCode.Forbidden] = "amqp:link:message-size-exceeded"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:server-busy"] = import_rhea_promise.AmqpResponseStatusCode.ServiceUnavailable] = "com.microsoft:server-busy"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:argument-error"] = import_rhea_promise.AmqpResponseStatusCode.BadRequest] = "com.microsoft:argument-error"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:argument-out-of-range"] = import_rhea_promise.AmqpResponseStatusCode.BadRequest] = "com.microsoft:argument-out-of-range"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:store-lock-lost"] = import_rhea_promise.AmqpResponseStatusCode.Gone] = "com.microsoft:store-lock-lost"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:session-cannot-be-locked"] = import_rhea_promise.AmqpResponseStatusCode.Gone] = "com.microsoft:session-cannot-be-locked"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:partition-not-owned"] = import_rhea_promise.AmqpResponseStatusCode.Gone] = "com.microsoft:partition-not-owned"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:entity-disabled"] = import_rhea_promise.AmqpResponseStatusCode.BadRequest] = "com.microsoft:entity-disabled"; ConditionStatusMapper2[ConditionStatusMapper2["com.microsoft:publisher-revoked"] = import_rhea_promise.AmqpResponseStatusCode.Unauthorized] = "com.microsoft:publisher-revoked"; ConditionStatusMapper2[ConditionStatusMapper2["amqp:link:stolen"] = import_rhea_promise.AmqpResponseStatusCode.Gone] = "amqp:link:stolen"; ConditionStatusMapper2[ConditionStatusMapper2["amqp:not-allowed"] = import_rhea_promise.AmqpResponseStatusCode.BadRequest] = "amqp:not-allowed"; ConditionStatusMapper2[ConditionStatusMapper2["amqp:unauthorized-access"] = import_rhea_promise.AmqpResponseStatusCode.Unauthorized] = "amqp:unauthorized-access"; ConditionStatusMapper2[ConditionStatusMapper2["amqp:resource-limit-exceeded"] = import_rhea_promise.AmqpResponseStatusCode.Forbidden] = "amqp:resource-limit-exceeded"; return ConditionStatusMapper2; })(ConditionStatusMapper || {}); var ConditionErrorNameMapper = /* @__PURE__ */ ((ConditionErrorNameMapper2) => { ConditionErrorNameMapper2["com.microsoft:address-already-in-use"] = "AddressAlreadyInUseError"; ConditionErrorNameMapper2["com.microsoft:store-lock-lost"] = "StoreLockLostError"; ConditionErrorNameMapper2["com.microsoft:no-matching-subscription"] = "NoMatchingSubscriptionError"; ConditionErrorNameMapper2["com.microsoft:partition-not-owned"] = "PartitionNotOwnedError"; ConditionErrorNameMapper2["com.microsoft:publisher-revoked"] = "PublisherRevokedError"; ConditionErrorNameMapper2["com.microsoft:entity-already-exists"] = "MessagingEntityAlreadyExistsError"; ConditionErrorNameMapper2["com.microsoft:entity-disabled"] = "MessagingEntityDisabledError"; ConditionErrorNameMapper2["com.microsoft:message-lock-lost"] = "MessageLockLostError"; ConditionErrorNameMapper2["com.microsoft:session-lock-lost"] = "SessionLockLostError"; ConditionErrorNameMapper2["com.microsoft:session-cannot-be-locked"] = "SessionCannotBeLockedError"; ConditionErrorNameMapper2["amqp:internal-error"] = "InternalServerError"; ConditionErrorNameMapper2["amqp:not-found"] = "ServiceCommunicationError"; ConditionErrorNameMapper2["com.microsoft:message-not-found"] = "MessageNotFoundError"; ConditionErrorNameMapper2["com.microsoft:relay-not-found"] = "RelayNotFoundError"; ConditionErrorNameMapper2["amqp:not-implemented"] = "NotImplementedError"; ConditionErrorNameMapper2["amqp:not-allowed"] = "InvalidOperationError"; ConditionErrorNameMapper2["amqp:resource-limit-exceeded"] = "QuotaExceededError"; ConditionErrorNameMapper2["amqp:unauthorized-access"] = "UnauthorizedError"; ConditionErrorNameMapper2["com.microsoft:auth-failed"] = "UnauthorizedError"; ConditionErrorNameMapper2["com.microsoft:timeout"] = "ServiceUnavailableError"; ConditionErrorNameMapper2["com.microsoft:message-wait-timeout"] = "MessageWaitTimeout"; ConditionErrorNameMapper2["com.microsoft:argument-out-of-range"] = "ArgumentOutOfRangeError"; ConditionErrorNameMapper2["amqp:precondition-failed"] = "PreconditionFailedError"; ConditionErrorNameMapper2["com.microsoft:precondition-failed"] = "PreconditionFailedError"; ConditionErrorNameMapper2["amqp:decode-error"] = "DecodeError"; ConditionErrorNameMapper2["amqp:invalid-field"] = "InvalidFieldError"; ConditionErrorNameMapper2["amqp:resource-locked"] = "ResourceLockedError"; ConditionErrorNameMapper2["amqp:resource-deleted"] = "ResourceDeletedError"; ConditionErrorNameMapper2["amqp:illegal-state"] = "IllegalStateError"; ConditionErrorNameMapper2["amqp:frame-size-too-small"] = "FrameSizeTooSmallError"; ConditionErrorNameMapper2["amqp:link:detach-forced"] = "DetachForcedError"; ConditionErrorNameMapper2["amqp:link:transfer-limit-exceeded"] = "TransferLimitExceededError"; ConditionErrorNameMapper2["amqp:link:message-size-exceeded"] = "MessageTooLargeError"; ConditionErrorNameMapper2["amqp:link:redirect"] = "LinkRedirectError"; ConditionErrorNameMapper2["amqp:link:stolen"] = "ReceiverDisconnectedError"; ConditionErrorNameMapper2["amqp:session:window-violation"] = "SessionWindowViolationError"; ConditionErrorNameMapper2["amqp:session:errant-link"] = "ErrantLinkError"; ConditionErrorNameMapper2["amqp:session:handle-in-use"] = "HandleInUseError"; ConditionErrorNameMapper2["amqp:session:unattached-handle"] = "UnattachedHandleError"; ConditionErrorNameMapper2["amqp:connection:forced"] = "ConnectionForcedError"; ConditionErrorNameMapper2["amqp:connection:framing-error"] = "FramingError"; ConditionErrorNameMapper2["amqp:connection:redirect"] = "ConnectionRedirectError"; ConditionErrorNameMapper2["com.microsoft:server-busy"] = "ServerBusyError"; ConditionErrorNameMapper2["com.microsoft:argument-error"] = "ArgumentError"; ConditionErrorNameMapper2["com.microsoft:operation-cancelled"] = "OperationCancelledError"; ConditionErrorNameMapper2["client.sender:not-enough-link-credit"] = "SenderBusyError"; ConditionErrorNameMapper2["client.sender:link-not-ready"] = "SenderNotReadyError"; ConditionErrorNameMapper2["system:error"] = "SystemError"; return ConditionErrorNameMapper2; })(ConditionErrorNameMapper || {}); var ErrorNameConditionMapper = /* @__PURE__ */ ((ErrorNameConditionMapper2) => { ErrorNameConditionMapper2["AddressAlreadyInUseError"] = "com.microsoft:address-already-in-use"; ErrorNameConditionMapper2["StoreLockLostError"] = "com.microsoft:store-lock-lost"; ErrorNameConditionMapper2["NoMatchingSubscriptionError"] = "com.microsoft:no-matching-subscription"; ErrorNameConditionMapper2["PartitionNotOwnedError"] = "com.microsoft:partition-not-owned"; ErrorNameConditionMapper2["PublisherRevokedError"] = "com.microsoft:publisher-revoked"; ErrorNameConditionMapper2["MessagingEntityAlreadyExistsError"] = "com.microsoft:entity-already-exists"; ErrorNameConditionMapper2["MessagingEntityDisabledError"] = "com.microsoft:entity-disabled"; ErrorNameConditionMapper2["MessageLockLostError"] = "com.microsoft:message-lock-lost"; ErrorNameConditionMapper2["SessionLockLostError"] = "com.microsoft:session-lock-lost"; ErrorNameConditionMapper2["SessionCannotBeLockedError"] = "com.microsoft:session-cannot-be-locked"; ErrorNameConditionMapper2["InternalServerError"] = "amqp:internal-error"; ErrorNameConditionMapper2["ServiceCommunicationError"] = "amqp:not-found"; ErrorNameConditionMapper2["MessageNotFoundError"] = "com.microsoft:message-not-found"; ErrorNameConditionMapper2["RelayNotFoundError"] = "com.microsoft:relay-not-found"; ErrorNameConditionMapper2["NotImplementedError"] = "amqp:not-implemented"; ErrorNameConditionMapper2["InvalidOperationError"] = "amqp:not-allowed"; ErrorNameConditionMapper2["QuotaExceededError"] = "amqp:resource-limit-exceeded"; ErrorNameConditionMapper2["UnauthorizedError"] = "amqp:unauthorized-access"; ErrorNameConditionMapper2["ServiceUnavailableError"] = "com.microsoft:timeout"; ErrorNameConditionMapper2["MessageWaitTimeout"] = "com.microsoft:message-wait-timeout"; ErrorNameConditionMapper2["ArgumentOutOfRangeError"] = "com.microsoft:argument-out-of-range"; ErrorNameConditionMapper2["PreconditionFailedError"] = "amqp:precondition-failed"; ErrorNameConditionMapper2["DecodeError"] = "amqp:decode-error"; ErrorNameConditionMapper2["InvalidFieldError"] = "amqp:invalid-field"; ErrorNameConditionMapper2["ResourceLockedError"] = "amqp:resource-locked"; ErrorNameConditionMapper2["ResourceDeletedError"] = "amqp:resource-deleted"; ErrorNameConditionMapper2["IllegalStateError"] = "amqp:illegal-state"; ErrorNameConditionMapper2["FrameSizeTooSmallError"] = "amqp:frame-size-too-small"; ErrorNameConditionMapper2["DetachForcedError"] = "amqp:link:detach-forced"; ErrorNameConditionMapper2["TransferLimitExceededError"] = "amqp:link:transfer-limit-exceeded"; ErrorNameConditionMapper2["MessageTooLargeError"] = "amqp:link:message-size-exceeded"; ErrorNameConditionMapper2["LinkRedirectError"] = "amqp:link:redirect"; ErrorNameConditionMapper2["ReceiverDisconnectedError"] = "amqp:link:stolen"; ErrorNameConditionMapper2["SessionWindowViolationError"] = "amqp:session:window-violation"; ErrorNameConditionMapper2["ErrantLinkError"] = "amqp:session:errant-link"; ErrorNameConditionMapper2["HandleInUseError"] = "amqp:session:handle-in-use"; ErrorNameConditionMapper2["UnattachedHandleError"] = "amqp:session:unattached-handle"; ErrorNameConditionMapper2["ConnectionForcedError"] = "amqp:connection:forced"; ErrorNameConditionMapper2["FramingError"] = "amqp:connection:framing-error"; ErrorNameConditionMapper2["ConnectionRedirectError"] = "amqp:connection:redirect"; ErrorNameConditionMapper2["ServerBusyError"] = "com.microsoft:server-busy"; ErrorNameConditionMapper2["ArgumentError"] = "com.microsoft:argument-error"; ErrorNameConditionMapper2["OperationCancelledError"] = "com.microsoft:operation-cancelled"; ErrorNameConditionMapper2["SenderBusyError"] = "client.sender:not-enough-link-credit"; ErrorNameConditionMapper2["SenderNotReadyError"] = "client.sender:link-not-ready"; ErrorNameConditionMapper2["SystemError"] = "system:error"; return ErrorNameConditionMapper2; })(ErrorNameConditionMapper || {}); const systemErrorFieldsToCopy = [ "address", "code", "errno", "info", "port", "stack", "syscall" ]; function isMessagingError(error) { return error.name === "MessagingError"; } class MessagingError extends Error { /** * Address to which the network connection failed. * Only present if the `MessagingError` was instantiated with a Node.js `SystemError`. */ address; /** * A string label that identifies the error. */ code; /** * System-provided error number. * Only present if the `MessagingError` was instantiated with a Node.js `SystemError`. */ errno; /** * The error name. Default value: "MessagingError". */ name = "MessagingError"; /** * The unavailable network connection port. * Only present if the `MessagingError` was instantiated with a Node.js `SystemError`. */ port; /** * Name of the system call that triggered the error. * Only present if the `MessagingError` was instantiated with a Node.js `SystemError`. */ syscall; /** * * Describes whether the error is retryable. Default: true. */ retryable = true; /** * Extra details about the error. */ info; /** * @param message - The error message that provides more information about the error. * @param originalError - An error whose properties will be copied to the MessagingError if the * property matches one found on the Node.js `SystemError`. */ constructor(message, originalError) { super(message); if (!originalError) { return; } for (const propName of systemErrorFieldsToCopy) { if (originalError[propName] != void 0) { this[propName] = originalError[propName]; } } } } const retryableErrors = [ "InternalServerError", "ServerBusyError", "ServiceUnavailableError", "OperationCancelledError", // The service may throw UnauthorizedError if credentials have been rotated. // Attempt to retry in case the user has also rotated their credentials. "UnauthorizedError", // OperationTimeoutError occurs when the service fails to respond within a given timeframe. // Since reasons for such failures can be transient, this is treated as a retryable error. "OperationTimeoutError", "SenderBusyError", "SenderNotReadyError", "MessagingError", "DetachForcedError", "ConnectionForcedError", "TransferLimitExceededError", // InsufficientCreditError occurs when the number of credits available on Rhea link is insufficient. // Since reasons for such shortage can be transient such as for pending delivery of messages, this is treated as a retryable error. "InsufficientCreditError" ]; var SystemErrorConditionMapper = /* @__PURE__ */ ((SystemErrorConditionMapper2) => { SystemErrorConditionMapper2["ENOTFOUND"] = "amqp:not-found"; SystemErrorConditionMapper2["EBUSY"] = "com.microsoft:server-busy"; SystemErrorConditionMapper2["ECONNREFUSED"] = "amqp:connection:forced"; SystemErrorConditionMapper2["ETIMEDOUT"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["ECONNRESET"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["ENETDOWN"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["EHOSTDOWN"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["ENETRESET"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["ENETUNREACH"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["ENONET"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["EADDRNOTAVAIL"] = "com.microsoft:timeout"; SystemErrorConditionMapper2["EAI_AGAIN"] = "com.microsoft:timeout"; return SystemErrorConditionMapper2; })(SystemErrorConditionMapper || {}); function isSystemError(err) { if (!(0, import_core_util.isObjectWithProperties)(err, ["code", "syscall", "errno"])) { return false; } if (!(0, import_utils.isString)(err.code) || !(0, import_utils.isString)(err.syscall)) { return false; } if (!(0, import_utils.isString)(err.errno) && !(0, import_utils.isNumber)(err.errno)) { return false; } return true; } function isBrowserWebsocketError(err) { let result = false; if (!import_core_util.isNodeLike && globalThis && err.type === "error") { const wsKey = "WebSocket"; const WS = (0, import_utils.getGlobalProperty)(wsKey); if (WS && err.target instanceof WS) { result = true; } } return result; } function isErrorEvent(err) { return typeof err.error === "object" && typeof err.message === "string"; } const rheaPromiseErrors = [ // OperationTimeoutError occurs when the service fails to respond within a given timeframe. "OperationTimeoutError", // InsufficientCreditError occurs when the number of credits available on Rhea link is insufficient. "InsufficientCreditError", // Defines the error that occurs when the Sender fails to send a message. "SendOperationFailedError" ]; function translate(err) { if (!(0, import_core_util.isDefined)(err)) { return new Error(`Unknown error encountered.`); } else if (typeof err !== "object") { return new Error(String(err)); } const errObj = isErrorEvent(err) ? err.error : err; if (errObj instanceof TypeError || errObj instanceof RangeError) { return errObj; } if (errObj instanceof AggregateError && errObj.errors.length > 0) { const translatedErrors = errObj.errors.map((e) => translate(e)); const retryable = translatedErrors.some((e) => e.retryable === true); const result = new AggregateError(translatedErrors, errObj.message, { cause: errObj }); result.stack = errObj.stack; result.retryable = retryable; return result; } if (isAmqpError(errObj)) { const condition = errObj.condition; const description = errObj.description; const error = new MessagingError(description); if (errObj.stack) error.stack = errObj.stack; error.info = errObj.info; if (condition) { error.code = ConditionErrorNameMapper[condition]; } if (description && (description.includes("status-code: 404") || description.match(/The messaging entity .* could not be found.*/i) !== null)) { error.code = "MessagingEntityNotFoundError"; } if (error.code && retryableErrors.indexOf(error.code) === -1) { error.retryable = false; } return error; } if (errObj instanceof Error && errObj.name === "MessagingError") { return errObj; } if (isSystemError(errObj)) { const condition = errObj.code; const description = errObj.message; const error = new MessagingError(description, errObj); let errorType = "SystemError"; if (condition) { const amqpErrorCondition = SystemErrorConditionMapper[condition]; errorType = ConditionErrorNameMapper[amqpErrorCondition]; } if (retryableErrors.indexOf(errorType) === -1) { error.retryable = false; } return error; } if (isBrowserWebsocketError(errObj)) { const error = new MessagingError("Websocket connection failed."); error.code = ConditionErrorNameMapper["amqp:not-found" /* ServiceCommunicationError */]; error.retryable = false; return error; } if ((0, import_core_util.isError)(errObj) && rheaPromiseErrors.indexOf(errObj.name) !== -1) { const error = new MessagingError(errObj.message, errObj); error.code = errObj.name; if (error.code && retryableErrors.indexOf(error.code) === -1) { error.retryable = false; } return error; } return (0, import_core_util.isError)(errObj) ? errObj : new Error(String(errObj)); } function isAmqpError(error) { return (0, import_rhea_promise.isAmqpError)(error); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ConditionErrorNameMapper, ConditionStatusMapper, ErrorNameConditionMapper, MessagingError, SystemErrorConditionMapper, isMessagingError, isSystemError, retryableErrors, translate }); //# sourceMappingURL=errors.js.map