UNPKG

@instamenta/grpc-errors

Version:

grpc status codes builder for javascript and typescript grpc server responses

131 lines 4.94 kB
import { ServerErrorResponse, Metadata, ServerWritableStream, sendUnaryData, MetadataValue } from '@grpc/grpc-js'; import { ZodError } from 'zod'; /** * Defines interface with default set of keys for gRPC error * types to be provided for generic * or extended in case new errors are added. */ export interface IGrpcErrorKeys { INVALID_ARGUMENT: 'INVALID_ARGUMENT'; DEADLINE_EXCEEDED: 'DEADLINE_EXCEEDED'; NOT_FOUND: 'NOT_FOUND'; ALREADY_EXISTS: 'ALREADY_EXISTS'; PERMISSION_DENIED: 'PERMISSION_DENIED'; INTERNAL: 'INTERNAL'; UNAVAILABLE: 'UNAVAILABLE'; DATA_LOSS: 'DATA_LOSS'; UNAUTHENTICATED: 'UNAUTHENTICATED'; FAILED_PRECONDITION: 'FAILED_PRECONDITION'; VALIDATION: 'VALIDATION'; UNAUTHORIZED: 'UNAUTHORIZED'; RESOURCE_NOT_FOUND: 'RESOURCE_NOT_FOUND'; } /** Tired of boring old meta whose methods cant be chained? Try this! Simple yet wonderful */ export declare class Meta { #private; set(key: string, value: MetadataValue): this; get(): Metadata; static build(): Meta; } /** * * Class that provides utility functions for managing gRPC errors. Logging meaningful errors to the console. * * ! With unmatched type safety and extendability! * * @template T - string literal type representing gRPC error keys. * @class GrpcErrors */ export default class GrpcErrors<T extends IGrpcErrorKeys> { K: T; /** * A record of gRPC errors, where keys correspond to error codes * and values contain error details. */ ERRORS: Record<keyof T, ServerErrorResponse>; /** * * Creates and returns a new instance of the GrpcErrors class. * * @template T - string literal type representing gRPC error keys. * @returns - new instance of the GrpcErrors class. */ static getInstance<T extends IGrpcErrorKeys>(): GrpcErrors<T>; /** * * Extends the ERRORS record with a custom gRPC error object. * * @param _key - The key representing the custom gRPC error type. * @param _error - The custom gRPC error object to add to the ERRORS record. * @param _metadata - Additional metadata to attach to the custom gRPC error (if available). * @param _details - Additional details about the custom gRPC error (if available). */ EXTEND({ _key, _error, _metadata, _details, }: I_EXTEND<T>): void; /** * * Throws a gRPC error and logs it. * * @param _key - The key representing the gRPC error type. * @param _source - The source of the error (if available). * @param _metadata - Additional metadata to attach to the gRPC error. * @param _details - Additional details about the error (if available). * @returns - The thrown gRPC error response. */ T({ _key, _source, _metadata, _details, }: I_THROW<T>): ServerErrorResponse; /** * * Emits a gRPC error on a writable stream and logs it. * * @param call - The writable stream on which to emit the gRPC error. * @param _key - The key representing the gRPC error type. * @param _source - The source of the error (if available). * @param _metadata - Additional metadata to attach to the gRPC error. * @param _details - Additional details about the error (if available). */ E({ call, _key, _source, _metadata, _details, }: I_EMIT<T>): void; /** * * Calls a callback function with a gRPC error and logs it. * * @param callback - The callback function to call with the gRPC error. * @param _key - The key representing the gRPC error type. * @param _source - The source of the error (if available). * @param _metadata - Additional metadata to attach to the gRPC error. (if available). * @param _details - Additional details about the error (if available). */ CB({ callback, _key, _source, _metadata, _details, }: I_CALLBACK<T>): void; /** * * Converts a Zod error to a gRPC error response with metadata and details. * * @param error - The Zod error to convert. * @returns - The gRPC error response. */ handleZodError(error: ZodError): ServerErrorResponse; } /** * * Get default Class instance. * * @type {GrpcErrors<IGrpcErrorKeys>} */ export declare const w: GrpcErrors<IGrpcErrorKeys>; export interface I_EXTEND<T> { _key: keyof T; _error: ServerErrorResponse; _metadata?: null | Metadata; _details?: string | null; } export interface I_THROW<T> { _key: keyof T; _source?: string | null; _metadata?: null | Metadata; _details?: string | null; } export interface I_EMIT<T> { call: ServerWritableStream<any, any>; _key: keyof T; _source?: string | null; _metadata?: null | Metadata; _details?: string | null; } export interface I_CALLBACK<T> { callback: sendUnaryData<any>; _key: keyof T; _source?: string | null; _metadata?: null | Metadata; _details?: string | null; } //# sourceMappingURL=index.d.ts.map