UNPKG

@instamenta/grpc-errors

Version:

grpc status codes builder for javascript and typescript grpc server responses

268 lines (267 loc) 9.57 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _Meta_metadata; Object.defineProperty(exports, "__esModule", { value: true }); exports.w = exports.Meta = void 0; const grpc_js_1 = require("@grpc/grpc-js"); //! GRPC DEFAULT ERRORS const invalidArgumentError = { name: 'InvalidArgument', code: 3, details: '', metadata: new grpc_js_1.Metadata(), message: 'Invalid argument provided.', }; const deadlineExceededError = { name: 'DeadlineExceeded', code: 4, details: '', metadata: new grpc_js_1.Metadata(), message: 'Deadline for the operation exceeded.', }; const notFoundError = { name: 'NotFound', code: 5, details: '', metadata: new grpc_js_1.Metadata(), message: 'Resource not found.', }; const alreadyExistsError = { name: 'AlreadyExists', code: 6, details: '', metadata: new grpc_js_1.Metadata(), message: 'Resource already exists.', }; const permissionDeniedError = { name: 'PermissionDenied', code: 7, details: '', metadata: new grpc_js_1.Metadata(), message: 'Permission denied for the operation.', }; const internalError = { name: 'Internal', code: 13, details: 'Server ran into unexpected internal error.', metadata: new grpc_js_1.Metadata(), message: 'Internal server error.', }; const unavailableError = { name: 'Unavailable', code: 14, details: '', metadata: new grpc_js_1.Metadata(), message: 'Service unavailable.', }; const dataLossError = { name: 'DataLoss', code: 15, details: '', metadata: new grpc_js_1.Metadata(), message: 'Data loss occurred.', }; const unauthenticatedError = { name: 'Unauthenticated', code: 16, details: '', metadata: new grpc_js_1.Metadata(), message: 'Request not authenticated.', }; const failedPreconditionError = { name: 'FailedPrecondition', code: 9, details: '', metadata: new grpc_js_1.Metadata(), message: 'Operation failed precondition check.', }; const validationError = { name: 'GrpcValidationError', code: 3, details: '', metadata: new grpc_js_1.Metadata(), message: 'gRPC validation error occurred.', }; const unauthorizedError = { name: 'GrpcUnauthorized', code: 16, details: '', metadata: new grpc_js_1.Metadata(), message: 'gRPC unauthorized access.', }; const resourceNotFoundError = { name: 'GrpcResourceNotFound', code: 5, details: '', metadata: new grpc_js_1.Metadata(), message: 'gRPC resource not found.', }; /** Tired of boring old meta whose methods cant be chained? Try this! Simple yet wonderful */ class Meta { constructor() { _Meta_metadata.set(this, new grpc_js_1.Metadata()); } set(key, value) { __classPrivateFieldGet(this, _Meta_metadata, "f").set(key, value); return this; } get() { return __classPrivateFieldGet(this, _Meta_metadata, "f"); } static build() { return new Meta(); } } exports.Meta = Meta; _Meta_metadata = new WeakMap(); /** * * 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 */ class GrpcErrors { constructor() { this.K = { 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', }; /** * A record of gRPC errors, where keys correspond to error codes * and values contain error details. */ this.ERRORS = { INVALID_ARGUMENT: invalidArgumentError, DEADLINE_EXCEEDED: deadlineExceededError, NOT_FOUND: notFoundError, ALREADY_EXISTS: alreadyExistsError, PERMISSION_DENIED: permissionDeniedError, INTERNAL: internalError, UNAVAILABLE: unavailableError, DATA_LOSS: dataLossError, UNAUTHENTICATED: unauthenticatedError, FAILED_PRECONDITION: failedPreconditionError, VALIDATION: validationError, UNAUTHORIZED: unauthorizedError, RESOURCE_NOT_FOUND: resourceNotFoundError, }; } /** * * 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() { return new GrpcErrors(); } /** * * 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 = null, _details = null, }) { this.ERRORS[_key] = _error; } /** * * 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 = null, _metadata = null, _details = null, }) { const key = String(_key); console.log(_source ? `[ Emitting GRPC ERROR: [ ${key} ] from "${_source}" ]` : `[ Emitting ERROR: [ ${key} ] ]`); const _error = this.ERRORS[_key]; if (_metadata) _error.metadata = _metadata; if (_details) _error.details = _details; return _error; } /** * * 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 = null, _metadata = null, _details = null, }) { const key = String(_key); const _error = this.ERRORS[_key]; if (_metadata) _error.metadata = _metadata; if (_details) _error.details = _details; call.emit('error', _error); return console.log(_source ? `[ Emitting GRPC ERROR: [ ${key} ] from "${_source}" ]` : `[ Emitting ERROR: [ ${key} ] ]`); } /** * * 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 = null, _metadata = null, _details = null, }) { const key = String(_key); const _error = this.ERRORS[_key]; if (_metadata) _error.metadata = _metadata; if (_details) _error.details = _details; callback(_error); return console.log(_source ? `[ Emitting GRPC ERROR: [ ${key} ] from "${_source}" ]` : `[ Emitting ERROR: [ ${key} ] ]`); } /** * * 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) { var _a, _b; const _error = this.ERRORS.VALIDATION; const _metadata = new grpc_js_1.Metadata(); (_a = error === null || error === void 0 ? void 0 : error.errors) === null || _a === void 0 ? void 0 : _a.forEach((e, i) => { _metadata.set(`error_${i}`, e.message); }); _error.metadata = _metadata; _error.details = (_b = error === null || error === void 0 ? void 0 : error.errors) === null || _b === void 0 ? void 0 : _b.map((e) => e.message).join(', '); return _error; } } exports.default = GrpcErrors; /** * * Get default Class instance. * * @type {GrpcErrors<IGrpcErrorKeys>} */ exports.w = GrpcErrors.getInstance();