{"version":3,"sources":["../../src/error/status.ts"],"sourcesContent":["import type { Detail, Details } from './detail';\n\nexport const Code = {\n  // Not an error; returned on success\n  //\n  // HTTP Mapping: 200 OK\n  OK: 200,\n\n  // The operation was cancelled, typically by the caller.\n  //\n  // HTTP Mapping: 499 Client Closed Request\n  CANCELLED: 499,\n\n  // Unknown error.  For example, this error may be returned when\n  // a `Status` value received from another address space belongs to\n  // an error space that is not known in this address space.  Also,\n  // errors raised by APIs that do not return enough error information\n  // may be converted to this error.\n  //\n  // HTTP Mapping: 500 Internal Server Error\n  UNKNOWN: 500,\n\n  // The client specified an invalid argument.  Note that this differs\n  // from `FAILED_PRECONDITION`.  `INVALID_ARGUMENT` indicates arguments\n  // that are problematic regardless of the state of the system\n  // (e.g., a malformed file name).\n  //\n  // HTTP Mapping: 400 Bad Request\n  INVALID_ARGUMENT: 400,\n\n  // The deadline expired before the operation could complete. For operations\n  // that change the state of the system, this error may be returned\n  // even if the operation has completed successfully.  For example, a\n  // successful response from a server could have been delayed long\n  // enough for the deadline to expire.\n  //\n  // HTTP Mapping: 504 Gateway Timeout\n  DEADLINE_EXCEEDED: 504,\n\n  // Some requested entity (e.g., file or directory) was not found.\n  //\n  // Note to server developers: if a request is denied for an entire class\n  // of users, such as gradual feature rollout or undocumented whitelist,\n  // `NOT_FOUND` may be used. If a request is denied for some users within\n  // a class of users, such as user-based access control, `PERMISSION_DENIED`\n  // must be used.\n  //\n  // HTTP Mapping: 404 Not Found\n  NOT_FOUND: 404,\n\n  // The entity that a client attempted to create (e.g., file or directory)\n  // already exists.\n  //\n  // HTTP Mapping: 409 Conflict\n  ALREADY_EXISTS: 409,\n\n  // The caller does not have permission to execute the specified\n  // operation. `PERMISSION_DENIED` must not be used for rejections\n  // caused by exhausting some resource (use `RESOURCE_EXHAUSTED`\n  // instead for those errors). `PERMISSION_DENIED` must not be\n  // used if the caller can not be identified (use `UNAUTHENTICATED`\n  // instead for those errors). This error code does not imply the\n  // request is valid or the requested entity exists or satisfies\n  // other pre-conditions.\n  //\n  // HTTP Mapping: 403 Forbidden\n  PERMISSION_DENIED: 403,\n\n  // Some resource has been exhausted, perhaps a per-user quota, or\n  // perhaps the entire file system is out of space.\n  //\n  // HTTP Mapping: 429 Too Many Requests\n  RESOURCE_EXHAUSTED: 429,\n\n  // The operation was rejected because the system is not in a state\n  // required for the operation's execution.  For example, the directory\n  // to be deleted is non-empty, a rmdir operation is applied to\n  // a non-directory, etc.\n  //\n  // Service implementors can use the following guidelines to decide\n  // between `FAILED_PRECONDITION`, `ABORTED`, and `UNAVAILABLE`:\n  //  (a) Use `UNAVAILABLE` if the client can retry just the failing call.\n  //  (b) Use `ABORTED` if the client should retry at a higher level\n  //      (e.g., when a client-specified test-and-set fails, indicating the\n  //      client should restart a read-modify-write sequence).\n  //  (c) Use `FAILED_PRECONDITION` if the client should not retry until\n  //      the system state has been explicitly fixed.  E.g., if a \"rmdir\"\n  //      fails because the directory is non-empty, `FAILED_PRECONDITION`\n  //      should be returned since the client should not retry unless\n  //      the files are deleted from the directory.\n  //\n  // HTTP Mapping: 400 Bad Request\n  FAILED_PRECONDITION: 400,\n\n  // The operation was aborted, typically due to a concurrency issue such as\n  // a sequencer check failure or transaction abort.\n  //\n  // See the guidelines above for deciding between `FAILED_PRECONDITION`,\n  // `ABORTED`, and `UNAVAILABLE`.\n  //\n  // HTTP Mapping: 409 Conflict\n  ABORTED: 409,\n\n  // The operation was attempted past the valid range.  E.g., seeking or\n  // reading past end-of-file.\n  //\n  // Unlike `INVALID_ARGUMENT`, this error indicates a problem that may\n  // be fixed if the system state changes. For example, a 32-bit file\n  // system will generate `INVALID_ARGUMENT` if asked to read at an\n  // offset that is not in the range [0,2^32-1], but it will generate\n  // `OUT_OF_RANGE` if asked to read from an offset past the current\n  // file size.\n  //\n  // There is a fair bit of overlap between `FAILED_PRECONDITION` and\n  // `OUT_OF_RANGE`.  We recommend using `OUT_OF_RANGE` (the more specific\n  // error) when it applies so that callers who are iterating through\n  // a space can easily look for an `OUT_OF_RANGE` error to detect when\n  // they are done.\n  //\n  // HTTP Mapping: 400 Bad Request\n  OUT_OF_RANGE: 400,\n\n  // The operation is not implemented or is not supported/enabled in this\n  // service.\n  //\n  // HTTP Mapping: 501 Not Implemented\n  UNIMPLEMENTED: 501,\n\n  // Internal errors.  This means that some invariants expected by the\n  // underlying system have been broken.  This error code is reserved\n  // for serious errors.\n  //\n  // HTTP Mapping: 500 Internal Server Error\n  INTERNAL: 500,\n\n  // The service is currently unavailable.  This is most likely a\n  // transient condition, which can be corrected by retrying with\n  // a backoff. Note that it is not always safe to retry\n  // non-idempotent operations.\n  //\n  // See the guidelines above for deciding between `FAILED_PRECONDITION`,\n  // `ABORTED`, and `UNAVAILABLE`.\n  //\n  // HTTP Mapping: 503 Service Unavailable\n  UNAVAILABLE: 503,\n\n  // Unrecoverable data loss or corruption.\n  //\n  // HTTP Mapping: 500 Internal Server Error\n  DATA_LOSS: 500,\n\n  // The request does not have valid authentication credentials for the\n  // operation.\n  //\n  // HTTP Mapping: 401 Unauthorized\n  UNAUTHENTICATED: 401,\n\n  // The request method is not supported by the server and cannot be handled.\n  //\n  // HTTP Mapping: 405 Method Not Allowed\n  METHOD_NOT_ALLOWED: 405,\n} as const;\n\nexport const DEFAULT_MESSAGES: Record<keyof typeof Code, string> = {\n  OK: 'OK',\n  CANCELLED: 'The operation was cancelled',\n  UNKNOWN: 'Unknown error',\n  INVALID_ARGUMENT: 'The client specified an invalid argument',\n  DEADLINE_EXCEEDED: 'The deadline expired before the operation could complete',\n  NOT_FOUND: 'Some requested entity was not found',\n  ALREADY_EXISTS: 'The entity that a client attempted to create already exists',\n  PERMISSION_DENIED: 'The caller does not have permission to execute the specified operation',\n  RESOURCE_EXHAUSTED: 'Some resource has been exhausted',\n  FAILED_PRECONDITION:\n    \"The operation was rejected because the system is not in a state required for the operation's execution\",\n  ABORTED: 'The operation was aborted',\n  OUT_OF_RANGE: 'The operation was attempted past the valid range',\n  UNIMPLEMENTED: 'The operation is not implemented or is not supported/enabled in this service',\n  INTERNAL: 'Internal errors',\n  UNAVAILABLE: 'The service is currently unavailable',\n  DATA_LOSS: 'Unrecoverable data loss or corruption',\n  UNAUTHENTICATED: 'The request does not have valid authentication credentials for the operation',\n  METHOD_NOT_ALLOWED: 'The request method is not supported by the server and cannot be handled',\n};\n\nexport interface ErrorBody {\n  error: {\n    code: number;\n    status: keyof typeof Code;\n    message: string;\n    details: Detail[];\n  };\n}\n\nexport class StatusError extends Error {\n  readonly status: number;\n  readonly body?: ErrorBody;\n\n  constructor(status: number, body?: ErrorBody) {\n    super(body?.error?.message ?? `Status Error: ${status}`);\n    this.name = 'StatusError';\n    this.status = status;\n    this.body = body;\n    Object.setPrototypeOf(this, StatusError.prototype);\n  }\n}\n\nexport class StatusCode {\n  code: keyof typeof Code;\n  message?: string;\n  private constructor(code: keyof typeof Code, message?: string) {\n    this.code = code;\n    this.message = message;\n  }\n\n  static of(code: keyof typeof Code, message?: string) {\n    return new StatusCode(code, message ?? DEFAULT_MESSAGES[code]);\n  }\n\n  body(details?: Details): ErrorBody {\n    return {\n      error: {\n        code: Code[this.code],\n        status: this.code,\n        message: this.message ?? '',\n        details: details?.list ?? [],\n      },\n    };\n  }\n\n  error(details?: Details): Error {\n    const body = this.body(details);\n    if (Status.adapter) return Status.adapter(Code[this.code], body);\n    return new StatusError(Code[this.code], body);\n  }\n\n  response(details?: Details): Response {\n    const body = this.body(details);\n    return Response.json(body, { status: body.error.code });\n  }\n}\n\nexport class Status {\n  static adapter?: (status: number, response: ErrorBody) => Error;\n\n  static ok = (message?: string) => StatusCode.of('OK', message);\n  static cancelled = (message?: string) => StatusCode.of('CANCELLED', message);\n  static unknown = (message?: string) => StatusCode.of('UNKNOWN', message);\n  static invalidArgument = (message?: string) => StatusCode.of('INVALID_ARGUMENT', message);\n  static deadlineExceeded = (message?: string) => StatusCode.of('DEADLINE_EXCEEDED', message);\n  static notFound = (message?: string) => StatusCode.of('NOT_FOUND', message);\n  static alreadyExists = (message?: string) => StatusCode.of('ALREADY_EXISTS', message);\n  static permissionDenied = (message?: string) => StatusCode.of('PERMISSION_DENIED', message);\n  static unauthorized = (message?: string) => StatusCode.of('UNAUTHENTICATED', message);\n  static resourceExhausted = (message?: string) => StatusCode.of('RESOURCE_EXHAUSTED', message);\n  static failedPrecondition = (message?: string) => StatusCode.of('FAILED_PRECONDITION', message);\n  static aborted = (message?: string) => StatusCode.of('ABORTED', message);\n  static outOfRange = (message?: string) => StatusCode.of('OUT_OF_RANGE', message);\n  static unimplemented = (message?: string) => StatusCode.of('UNIMPLEMENTED', message);\n  static internal = (message?: string) => StatusCode.of('INTERNAL', message);\n  static unavailable = (message?: string) => StatusCode.of('UNAVAILABLE', message);\n  static dataLoss = (message?: string) => StatusCode.of('DATA_LOSS', message);\n  static methodNotAllowed = (message?: string) => StatusCode.of('METHOD_NOT_ALLOWED', message);\n}\n"],"mappings":";AAEO,IAAM,OAAO;AAAA;AAAA;AAAA;AAAA,EAIlB,IAAI;AAAA;AAAA;AAAA;AAAA,EAKJ,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASX,SAAS;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQT,kBAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASlB,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWnB,WAAW;AAAA;AAAA;AAAA;AAAA;AAAA,EAMX,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYhB,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMnB,oBAAoB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBpB,qBAAqB;AAAA;AAAA;AAAA;AAAA;AA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