export function connectErrorFromNodeReason()

in packages/dubbo-node/src/node-error.ts [22:66]


export function connectErrorFromNodeReason(reason: unknown): ConnectError {
  let code = Code.Internal;
  const chain = unwrapNodeErrorChain(reason).map(getNodeErrorProps);
  if (chain.some((p) => p.code == "ERR_STREAM_WRITE_AFTER_END")) {
    // We do not want intentional errors from the server to be shadowed
    // by client-side errors.
    // This can occur if the server has written a response with an error
    // and has ended the connection. This response may already sit in a
    // buffer on the client, while it is still writing to the request
    // body.
    // To avoid this problem, we wrap ERR_STREAM_WRITE_AFTER_END as a
    // ConnectError with Code.Aborted. The special meaning of this code
    // in this situation is documented in StreamingConn.send() and in
    // createServerStreamingFn().
    code = Code.Aborted;
  } else if (
    chain.some(
      (p) =>
        p.code == "ERR_STREAM_DESTROYED" ||
        p.code == "ERR_HTTP2_INVALID_STREAM" ||
        p.code == "ECONNRESET"
    )
  ) {
    // A handler whose stream is suddenly destroyed usually means the client
    // hung up. This behavior is triggered by the crosstest "cancel_after_begin".
    code = Code.Aborted;
  } else if (
    chain.some(
      (p) =>
        p.code == "ENOTFOUND" ||
        p.code == "EAI_AGAIN" ||
        p.code == "ECONNREFUSED"
    )
  ) {
    // Calling an unresolvable host should raise a ConnectError with
    // Code.Aborted.
    // This behavior is covered by the crosstest "unresolvable_host".
    code = Code.Unavailable;
  }
  const ce = ConnectError.from(reason, code);
  if (ce !== reason) {
    ce.cause = reason;
  }
  return ce;
}