import { CoercedError } from './factories.js'; import { ErrorCode } from './types.js'; declare const statuses: { /** Status representing other errors. */ readonly UNKNOWN: { readonly grpc: 2; readonly http: 500; }; /** * Generic internal failure status. This status is used by default for * standard errors which do not have one set explicitly. In general you * shouldn't need to set it except when masking an existing status. */ readonly INTERNAL: { readonly grpc: 13; readonly http: 500; }; /** * The operation is not implemented or is not supported/enabled in this * service. */ readonly UNIMPLEMENTED: { readonly grpc: 12; readonly http: 501; }; /** The service is currently unavailable. */ readonly UNAVAILABLE: { readonly grpc: 14; readonly http: 503; }; /** The deadline expired before the operation could complete. */ readonly DEADLINE_EXCEEDED: { readonly grpc: 4; readonly http: 504; }; /** * The operation was aborted, typically due to a concurrency issue such as a * sequencer check failure or transaction abort. */ readonly ABORTED: { readonly grpc: 10; readonly http: 504; }; /** The client specified an invalid argument. */ readonly INVALID_ARGUMENT: { readonly grpc: 3; readonly http: 400; }; /** * The request does not have valid authentication credentials for the * operation. */ readonly UNAUTHENTICATED: { readonly grpc: 16; readonly http: 401; }; /** The caller does not have permission to execute the specified operation. */ readonly PERMISSION_DENIED: { readonly grpc: 7; readonly http: 403; }; /** Some requested entity (e.g., file or directory) was not found. */ readonly NOT_FOUND: { readonly grpc: 5; readonly http: 404; }; /** * The entity that a client attempted to create (e.g., file or directory) * already exists. */ readonly ALREADY_EXISTS: { readonly grpc: 6; readonly http: 409; }; /** * The operation was rejected because the system is not in a state required * for the operation's execution. */ readonly FAILED_PRECONDITION: { readonly grpc: 9; readonly http: 422; }; /** Some resource has been exhausted. */ readonly RESOURCE_EXHAUSTED: { readonly grpc: 8; readonly http: 429; }; /** The operation was cancelled, typically by the caller. */ readonly CANCELLED: { readonly grpc: 1; readonly http: 499; }; }; export type StatusProtocol = 'grpc' | 'http'; export type ErrorStatus = keyof typeof statuses; /** * Non-error status, useful for example when including an ok case within * aggregations or metrics. */ export declare const OK_STATUS = "OK"; export type OkStatus = typeof OK_STATUS; export declare function isErrorStatus(arg: string): arg is ErrorStatus; /** A wrapping error which decorates another with a status. */ export interface StatusError extends Error { /** The error status. */ readonly status: ErrorStatus; /** The underlying error, to which the status is added. */ readonly contents: E; /** * Protocol code overrides. This can be useful when the generic status is not * as granular as the underlying protocol's error codes. */ readonly protocolCodes: StatusProtocolCodes; } export type StatusProtocolCodes = { readonly [P in StatusProtocol]?: number; }; export interface StatusErrorOptions { readonly protocolCodes?: StatusProtocolCodes; } /** * Returns true iff the status corresponds to an internal issue (500 code). This * should be used to avoid surfacing internal error details to clients. */ export declare function isInternalProblem(status: ErrorStatus): boolean; /** * Returns true iff the status corresponds to a server-side issue. This is all * statuses corresponding to 5XX codes, except UNIMPLEMENTED (technically 501). */ export declare function isServerProblem(status: ErrorStatus): boolean; export declare function statusError(status: ErrorStatus, err: E, opts?: StatusErrorOptions): StatusError; export type StatusErrorFactory = (err: E, opts?: StatusErrorOptions) => StatusError; export type StatusErrorFactories = { readonly [K in Exclude as ConstantToCamelCase]: StatusErrorFactory; }; /** Type-level case-change from `CONSTANT_CASE` to `camelCase`. */ type ConstantToCamelCase = S extends `${infer T}_${infer U}` ? `${Lowercase}${Capitalize>}` : Lowercase; export declare const statusErrors: StatusErrorFactories; export declare function isStatusError(err: unknown): err is StatusError; /** * Returns the default numeric gRPC code for a given status. See `protocolCodes` * for transmitting more granular values. */ export declare function statusToGrpcCode(status: ErrorStatus): number; export declare function statusFromGrpcCode(code: number): ErrorStatus | OkStatus; /** * Returns the default numeric HTTP code for a given status. See `protocolCodes` * for transmitting more granular values. */ export declare function statusToHttpCode(status: ErrorStatus): number; export declare function statusFromHttpCode(code: number): ErrorStatus | OkStatus; /** Returns the best numeric error code for a given error and protocol. */ export declare function statusProtocolCode(protocol: StatusProtocol, err: StatusError): number; export declare function inferErrorStatus(err: unknown): ErrorStatus; /** A failure is the public representation of an error. */ export interface Failure { /** Standard error status. */ readonly status: ErrorStatus; /** The error that caused the failure. */ readonly error: { /** Human-readable message about the error. */ readonly message: string; /** Application-specific error code for programmatic handling. */ readonly code?: string; /** Structured metadata. */ readonly tags?: { readonly [key: string]: unknown; }; }; } /** Generates a new failure from a status error. */ export declare function failure(err: StatusError, opts?: { /** Annotations to append to the failure's message */ readonly annotations?: ReadonlyArray; }): Failure; /** Mapping from status to error code(s). */ export type StatusMapping = { readonly [S in ErrorStatus]?: ErrorCode | Iterable; }; /** * Wraps and rethrows an (internal) error with the status specified in the input * mapping. If the error is not an internal one or doesn't match, this method * rethrows the original error. Note that this method does not walk the error's * causal chain to avoid swallowing downstream errors. */ export declare function rethrowWithStatus(err: unknown, mapping: StatusMapping): never; /** Map from error code to status. */ export interface ErrorStatuses { readonly [code: ErrorCode]: ErrorStatus; } export {};