Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nice-grpc-error-details

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nice-grpc-error-details

gRPC rich error model implementation for nice-grpc

  • 0.2.5
  • npm
  • Socket score

Version published
Weekly downloads
20K
increased by63.64%
Maintainers
1
Weekly downloads
 
Created
Source

nice-grpc-error-details npm version

Rich error model implementation for nice-grpc.

This package is experimental and its API should not be considered stable.

Provides extensions to ServerError and ClientError which carry extra error details, as well as server and client middleware to communicate these details via trailing metadata.

Installation

npm install nice-grpc-error-details

Usage

Server

Attach the middleware to a server:

import {errorDetailsServerMiddleware} from 'nice-grpc-error-details';

const server = createServer().use(errorDetailsServerMiddleware);

Throw RichServerError from a service implementation method:

import {ServerError, Status} from 'nice-grpc';
import {RichServerError, BadRequest} from 'nice-grpc-error-details';

const exampleServiceImpl: ServiceImplementation<
  typeof ExampleServiceDefinition
> = {
  async exampleUnaryMethod(
    request: ExampleRequest,
  ): Promise<DeepPartial<ExampleResponse>> {
    if (!request.someField)
      throw new RichServerError(
        Status.INVALID_ARGUMENT,
        "Missing required field 'some_field'",
        [
          BadRequest.fromPartial({
            fieldViolations: [
              {
                field: 'some_field',
                description: 'Field is required',
              },
            ],
          }),
        ],
      );

    // ... method logic
  },
};

Client

Attach the middleware to a client factory:

import {errorDetailsClientMiddleware} from 'nice-grpc-error-details';

const clientFactory = createClientFactory().use(errorDetailsClientMiddleware);

If an error with details is returned from a server, the client will receive RichClientError:

import {Status} from 'nice-grpc';
import {RichClientError, BadRequest} from 'nice-grpc-error-details';

const fieldViolations: BadRequest_FieldViolation[] = [];

try {
  await client.exampleUnaryMethod(request);
} catch (error: unknown) {
  if (
    error instanceof RichClientError &&
    error.code === Status.INVALID_ARGUMENT
  ) {
    // loop through error details to find `BadRequest`
    for (const detail of error.extra) {
      if (detail.$type === BadRequest.$type) {
        fieldViolations.push(...detail.fieldViolations);
      }
    }
  } else {
    throw error;
  }
}

FAQs

Package last updated on 11 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc