npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grpc-web-error-details

v1.1.0

Published

Utility function for deserializing `grpc-status-details-bin` metadata on grpc-web

Downloads

4,954

Readme

grpc-web-error-details

Utility function for deserializing the grpc-web-details-bin metadata value when using grpc-web.

Motivation

gRPC provides two models of error handling: standard error model and richer error model.

Standard error model, which consists of an error status code and optional string error message, is the "official" gRPC error model.

If you're using protocol buffers, you can also use the richer error model developed and used by Google (click here for more about the error model). This model allows servers to return and clients to consume additional error details as one or more protobuf messages. It also defines a standard set of error message types to cover the most common error cases.

grpc-web, the official JavaScript implementation of gRPC for browser clients, only supports the standard error model. The error details are encoded in grpc-web-details-bin metadata field. However, deserializing the data is not trivial.

This library provides an utility function that takes an error and returns the deserialized Status message and the array of deserialized error details (comparable to Go's status.FromError and similar functions in other languages whose gRPC client supports the richer error model).

If you're looking to use the richer error model on Node.js, check out @stackpath/node-grpc-error-details.

Installation

yarn add grpc-web-error-details
# or if you're using npm
npm install grpc-web-error-details --save

This library also needs grpc-web and google-protobuf, which are required by gRPC Web clients.

Usage

Use statusFromError to deserialize error to Status and an array of error details. It returns a pair of status and an array of error details.

function statusFromError(err: any): [Status, ErrorDetails[]] | [null, null];

The function returns null if the passed error is not deserializable.

Call the function with an error object and if the return values are not null, call methods and use type assertions to handle errors.

import * as errorDetails from "./grpc-web-error-details";

// promise client
try {
  grpcWebPromiseClient.call();
} catch (e) {
  const [status, details] = errorDetails.statusFromError(e);
  if (status && details) {
    for (const d of details) {
      // use `instanceof` for type guard
      if (d instanceof errorDetails.BadRequest) {
        // use appropriate methods on details for further information
        for (const v of d.getFieldViolationsList()) {
          console.log(
            `Violation at field ${v.getField()}: ${v.getDescription}`
          );
        }
      }
    }
  }
}

// callback client
grpcWebClient.call(req, {}, (err, response) => {
  if (!err) {
    // RPC Success
    return;
  }
  const [status, details] = errorDetails.statusFromError(err);
  // handle richer error with status and details
});

Take a look at sample/client-js/index.ts for more examples. You can start the sample gRPC server and Envoy proxy by running docker-compose up inside ./sample, and use yarn ts-node sample/client-js/index.ts to run the client sample.