Skip to content

Errors

This package provides primitives for implementing AIP errors as described by AIP-193.

StatusError is an Error subclass that wraps a google.rpc.Status message. It includes a gRPC Code, an HTTP status code, and structured error details.

import { StatusError, NotFoundError } from "@protoutil/aip/errors";
// Use a specific error subclass
throw new NotFoundError({
message: "Book not found",
errorInfo: { reason: "BOOK_NOT_FOUND", domain: "library.api" },
});

Each subclass maps to a gRPC status code and HTTP code:

ClassgRPC CodeHTTP Code
CancelledErrorCANCELLED499
UnknownErrorUNKNOWN500
InvalidArgumentErrorINVALID_ARGUMENT400
DeadlineExceededErrorDEADLINE_EXCEEDED504
NotFoundErrorNOT_FOUND404
AlreadyExistsErrorALREADY_EXISTS409
PermissionDeniedErrorPERMISSION_DENIED403
UnauthenticatedErrorUNAUTHENTICATED401
ResourceExhaustedErrorRESOURCE_EXHAUSTED429
FailedPreconditionErrorFAILED_PRECONDITION400
AbortedErrorABORTED409
OutOfRangeErrorOUT_OF_RANGE400
UnimplementedErrorUNIMPLEMENTED501
InternalErrorINTERNAL500
UnavailableErrorUNAVAILABLE503
DataLossErrorDATA_LOSS500
// Convert a StatusError to a google.rpc.Status message
const statusMsg = error.toStatus();
// Build a StatusError from a google.rpc.Status message
const error = StatusError.fromStatus(statusMsg);

parseStatus parses a google.rpc.Status message and returns the appropriate StatusError subclass based on the status code:

import { parseStatus } from "@protoutil/aip/errors";
const error = parseStatus(statusMsg);
// Returns e.g. NotFoundError for Code.NOT_FOUND

status() creates a google.rpc.Status protobuf message from a StatusInit object:

import { status } from "@protoutil/aip/errors";
import { Code } from "@protoutil/aip/errors";
const statusMsg = status({
code: Code.NOT_FOUND,
message: "Resource not found",
errorInfo: { reason: "NOT_FOUND", domain: "example.com" },
});

A pre-built google.rpc.Status message with Code.OK:

import { OK_STATUS } from "@protoutil/aip/errors";

Per AIP-193, each error detail field may only be set once. The errorInfo field is optional. The following detail types are supported:

Detail TypeDescription
errorInfoDescribes the cause of the error with a reason and domain.
retryInfoDescribes when the client can retry a failed request.
debugInfoDescribes debugging info (stack traces, etc.).
quotaFailureDescribes how a quota check failed.
preconditionFailureDescribes what preconditions have failed.
badRequestDescribes violations in a client request.
requestInfoContains metadata about the request.
resourceInfoDescribes the resource being accessed.
helpProvides links to documentation or resources.
localizedMessageProvides a localized error message.
import { packErrorDetails, unpackErrorDetails, errorDetails } from "@protoutil/aip/errors";
// Create structured error details
const details = errorDetails({
errorInfo: { reason: "QUOTA_EXCEEDED", domain: "example.com" },
retryInfo: { retryDelay: { seconds: 30n } },
});
// Pack details into google.protobuf.Any messages
const packed = packErrorDetails({
errorInfo: { reason: "QUOTA_EXCEEDED", domain: "example.com" },
});
// Unpack details from google.protobuf.Any messages
const unpacked = unpackErrorDetails(anyMessages);
ExportDescription
StatusErrorBase error class wrapping google.rpc.Status
CancelledErrorDataLossErrorSpecific error subclasses (see table above)
parseStatus(status)Parse a Status into the appropriate StatusError subclass
status(init)Create a google.rpc.Status message
OK_STATUSPre-built OK status message
errorDetails(init)Create an ErrorDetails object
packErrorDetails(init)Pack error details into google.protobuf.Any[]
unpackErrorDetails(details)Unpack error details from google.protobuf.Any[]