export function translate()

in sdk/core/core-amqp/src/errors.ts [673:758]


export function translate(err: unknown): MessagingError | Error {
  if (!isDefined(err)) {
    return new Error(`Unknown error encountered.`);
  } else if (typeof err !== "object") {
    // The error is a scalar type, make it the message of an actual error.
    return new Error(String(err));
  }

  const errObj = isErrorEvent(err) ? err.error : err;

  // Built-in errors like TypeError and RangeError should not be retryable as these indicate issues
  // with user input and not an issue with the Messaging process.
  if (errObj instanceof TypeError || errObj instanceof RangeError) {
    return errObj;
  }

  if (isAmqpError(errObj)) {
    // translate
    const condition = errObj.condition;
    const description = errObj.description!;
    const error = new MessagingError(description);
    if ((errObj as any).stack) error.stack = (errObj as any).stack;
    error.info = errObj.info;
    if (condition) {
      error.code = ConditionErrorNameMapper[condition as keyof typeof ConditionErrorNameMapper];
    }
    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) {
      // not found
      error.retryable = false;
    }
    return error;
  }

  if (errObj instanceof Error && errObj.name === "MessagingError") {
    // already translated
    return errObj;
  }

  if (isSystemError(errObj)) {
    // translate
    const condition = errObj.code;
    const description = errObj.message;
    const error = new MessagingError(description, errObj);
    let errorType = "SystemError";
    if (condition) {
      const amqpErrorCondition =
        SystemErrorConditionMapper[condition as keyof typeof SystemErrorConditionMapper];
      errorType =
        ConditionErrorNameMapper[amqpErrorCondition as keyof typeof ConditionErrorNameMapper];
    }
    if (retryableErrors.indexOf(errorType) === -1) {
      // not found
      error.retryable = false;
    }
    return error;
  }

  if (isBrowserWebsocketError(errObj)) {
    // Translate browser communication errors during opening handshake to generic ServiceCommunicationError
    const error = new MessagingError("Websocket connection failed.");
    error.code = ConditionErrorNameMapper[ErrorNameConditionMapper.ServiceCommunicationError];
    error.retryable = false;
    return error;
  }

  // Some errors come from rhea-promise and need to be converted to MessagingError.
  // A subset of these are also retryable.
  if (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) {
      // not found
      error.retryable = false;
    }
    return error;
  }

  return isError(errObj) ? errObj : new Error(String(errObj));
}