{"version":3,"file":"status.mjs","names":[],"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 canceled, 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":";AAEA,MAAa,OAAO;CAIlB,IAAI;CAKJ,WAAW;CASX,SAAS;CAQT,kBAAkB;CASlB,mBAAmB;CAWnB,WAAW;CAMX,gBAAgB;CAYhB,mBAAmB;CAMnB,oBAAoB;CAoBpB,qBAAqB;CASrB,SAAS;CAmBT,cAAc;CAMd,eAAe;CAOf,UAAU;CAWV,aAAa;CAKb,WAAW;CAMX,iBAAiB;CAKjB,oBAAoB;AACtB;AAEA,MAAa,mBAAsD;CACjE,IAAI;CACJ,WAAW;CACX,SAAS;CACT,kBAAkB;CAClB,mBAAmB;CACnB,WAAW;CACX,gBAAgB;CAChB,mBAAmB;CACnB,oBAAoB;CACpB,qBACE;CACF,SAAS;CACT,cAAc;CACd,eAAe;CACf,UAAU;CACV,aAAa;CACb,WAAW;CACX,iBAAiB;CACjB,oBAAoB;AACtB;AAWA,IAAa,cAAb,MAAa,oBAAoB,MAAM;CACrC;CACA;CAEA,YAAY,QAAgB,MAAkB;EAC5C,MAAM,MAAM,MAAM,WAAW,iBAAiB,QAAQ;EACtD,KAAK,OAAO;E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