export function mapCodeFromHttpStatus()

in packages/firestore/src/remote/rpc_error.ts [234:306]


export function mapCodeFromHttpStatus(status?: number): Code {
  if (status === undefined) {
    logError('RPC_ERROR', 'HTTP error has no status');
    return Code.UNKNOWN;
  }

  // The canonical error codes for Google APIs [1] specify mapping onto HTTP
  // status codes but the mapping is not bijective. In each case of ambiguity
  // this function chooses a primary error.
  //
  // [1]
  // https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto
  switch (status) {
    case 200: // OK
      return Code.OK;

    case 400: // Bad Request
      return Code.FAILED_PRECONDITION;
    // Other possibilities based on the forward mapping
    // return Code.INVALID_ARGUMENT;
    // return Code.OUT_OF_RANGE;

    case 401: // Unauthorized
      return Code.UNAUTHENTICATED;

    case 403: // Forbidden
      return Code.PERMISSION_DENIED;

    case 404: // Not Found
      return Code.NOT_FOUND;

    case 409: // Conflict
      return Code.ABORTED;
    // Other possibilities:
    // return Code.ALREADY_EXISTS;

    case 416: // Range Not Satisfiable
      return Code.OUT_OF_RANGE;

    case 429: // Too Many Requests
      return Code.RESOURCE_EXHAUSTED;

    case 499: // Client Closed Request
      return Code.CANCELLED;

    case 500: // Internal Server Error
      return Code.UNKNOWN;
    // Other possibilities:
    // return Code.INTERNAL;
    // return Code.DATA_LOSS;

    case 501: // Unimplemented
      return Code.UNIMPLEMENTED;

    case 503: // Service Unavailable
      return Code.UNAVAILABLE;

    case 504: // Gateway Timeout
      return Code.DEADLINE_EXCEEDED;

    default:
      if (status >= 200 && status < 300) {
        return Code.OK;
      }
      if (status >= 400 && status < 500) {
        return Code.FAILED_PRECONDITION;
      }
      if (status >= 500 && status < 600) {
        return Code.INTERNAL;
      }
      return Code.UNKNOWN;
  }
}