UNPKG

twirp-rpc-client

Version:

Typescript twirp client built for use with ts-proto

114 lines (113 loc) 6.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.twirpErrorFromResponse = exports.TwirpErrorCode = exports.TwirpError = void 0; class TwirpError extends Error { constructor(code, msg, meta) { super(msg); this.code = code; this.msg = msg; this.meta = meta; } } exports.TwirpError = TwirpError; var TwirpErrorCode; (function (TwirpErrorCode) { // Canceled indicates the operation was cancelled (typically by the caller). TwirpErrorCode["Canceled"] = "canceled"; // Unknown error. For example when handling errors raised by APIs that do not // return enough error information. TwirpErrorCode["Unknown"] = "unknown"; // InvalidArgument indicates client specified an invalid argument. It // indicates arguments that are problematic regardless of the state of the // system (i.e. a malformed file name, required argument, number out of range, // etc.). TwirpErrorCode["InvalidArgument"] = "invalid_argument"; // DeadlineExceeded means operation expired before completion. For operations // that change the state of the system, this error may be returned even if the // operation has completed successfully (timeout). TwirpErrorCode["DeadlineExceeded"] = "deadline_exceeded"; // NotFound means some requested entity was not found. TwirpErrorCode["NotFound"] = "not_found"; // BadRoute means that the requested URL path wasn't routable to a Twirp // service and method. This is returned by the generated server, and usually // shouldn't be returned by applications. Instead, applications should use // NotFound or Unimplemented. TwirpErrorCode["BadRoute"] = "bad_route"; // AlreadyExists means an attempt to create an entity failed because one // already exists. TwirpErrorCode["AlreadyExists"] = "already_exists"; // PermissionDenied indicates the caller does not have permission to execute // the specified operation. It must not be used if the caller cannot be // identified (Unauthenticated). TwirpErrorCode["PermissionDenied"] = "permission_denied"; // Unauthenticated indicates the request does not have valid authentication // credentials for the operation. TwirpErrorCode["Unauthenticated"] = "unauthenticated"; // ResourceExhausted indicates some resource has been exhausted, perhaps a // per-user quota, or perhaps the entire file system is out of space. TwirpErrorCode["ResourceExhausted"] = "resource_exhausted"; // FailedPrecondition indicates operation was rejected because the system is // not in a state required for the operation's execution. For example, doing // an rmdir operation on a directory that is non-empty, or on a non-directory // object, or when having conflicting read-modify-write on the same resource. TwirpErrorCode["FailedPrecondition"] = "failed_precondition"; // Aborted indicates the operation was aborted, typically due to a concurrency // issue like sequencer check failures, transaction aborts, etc. TwirpErrorCode["Aborted"] = "aborted"; // OutOfRange means operation was attempted past the valid range. For example, // seeking or reading past end of a paginated collection. // // Unlike InvalidArgument, this error indicates a problem that may be fixed if // the system state changes (i.e. adding more items to the collection). // // There is a fair bit of overlap between FailedPrecondition and OutOfRange. // We recommend using OutOfRange (the more specific error) when it applies so // that callers who are iterating through a space can easily look for an // OutOfRange error to detect when they are done. TwirpErrorCode["OutOfRange"] = "out_of_range"; // Unimplemented indicates operation is not implemented or not // supported/enabled in this service. TwirpErrorCode["Unimplemented"] = "unimplemented"; // Internal errors. When some invariants expected by the underlying system // have been broken. In other words, something bad happened in the library or // backend service. Do not confuse with HTTP Internal Server Error; an // Internal error could also happen on the client code, i.e. when parsing a // server response. TwirpErrorCode["Internal"] = "internal"; // Unavailable indicates the service is currently unavailable. This is a most // likely a transient condition and may be corrected by retrying with a // backoff. TwirpErrorCode["Unavailable"] = "unavailable"; // DataLoss indicates unrecoverable data loss or corruption. TwirpErrorCode["DataLoss"] = "data_loss"; })(TwirpErrorCode = exports.TwirpErrorCode || (exports.TwirpErrorCode = {})); const STATUS_CODE_MAP = { 400: TwirpErrorCode.InvalidArgument, 403: TwirpErrorCode.PermissionDenied, 404: TwirpErrorCode.NotFound, 408: TwirpErrorCode.DeadlineExceeded, 409: TwirpErrorCode.Aborted, 412: TwirpErrorCode.FailedPrecondition, 429: TwirpErrorCode.ResourceExhausted, 500: TwirpErrorCode.Internal, 501: TwirpErrorCode.Unimplemented, 503: TwirpErrorCode.Unavailable, }; // twirpErrorFromIntermediary maps HTTP errors from non-twirp sources to twirp errors. // The mapping is similar to gRPC: https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md. function twirpErrorFromResponse(response) { var _a, _b, _c, _d; const status = response.status; const fallbackCode = (_a = STATUS_CODE_MAP[status]) !== null && _a !== void 0 ? _a : TwirpErrorCode.Internal; try { const parsedError = JSON.parse(response.data.toString()); const code = (_b = parsedError === null || parsedError === void 0 ? void 0 : parsedError.code) !== null && _b !== void 0 ? _b : fallbackCode; const msg = (_c = parsedError === null || parsedError === void 0 ? void 0 : parsedError.msg) !== null && _c !== void 0 ? _c : ''; const meta = (_d = parsedError === null || parsedError === void 0 ? void 0 : parsedError.meta) !== null && _d !== void 0 ? _d : {}; return new TwirpError(code, msg, meta); } catch (e) { return new TwirpError(fallbackCode, '', {}); } } exports.twirpErrorFromResponse = twirpErrorFromResponse;