0.2.7 • Published 3 months ago

nice-grpc-error-details v0.2.7

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

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;
  }
}
0.2.7

3 months ago

0.2.6

7 months ago

0.2.5

10 months ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.4

1 year ago

0.2.1

1 year ago

0.2.0

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.3

2 years ago

0.1.0

3 years ago

0.0.1

3 years ago