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

create-grpc-error

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

create-grpc-error

Utility function that creates an Error suitable for gRPC responses

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
59
increased by145.83%
Maintainers
1
Weekly downloads
 
Created
Source

create-grpc-error

Utility function that creates an Error suitable for gRPC responses

npm version build status

grpc-error - GRPCError class that uses this module

API

createGRPCError(message, code, metadata) ⇒ Error

Utility function that creates an Error suitable for gRPC responses. See tests for all examples

Kind: global function
Returns: Error - The new Error

ParamTypeDescription
messageString | Number | Error | ObjectIf String the error message If Number the error code If instanceof Error, the error to source data from. We still create a new Error instance, copy data from the passed error and assign / merge the rest of arguments. This can be used to mege metadata of existing error with additional metadata. If Object, assumed to be metadata, either plain object representation or actual grpc.Metadata instance. We use grpc-create-metadata module to create metadata for the return value.
codeNumber | ObjectIf Number the error code If Object, assumed to be metadata, either plain object representation or actual grpc.Metadata instance. We use grpc-create-metadata module to create metadata for the return value.
metadataObjectThe error metadata. Either plain object representation or actual grpc.Metadata instance. We use grpc-create-metadata module to create metadata for the return value.

Example

const createGRPCError = require('create-grpc-error')

const err = createGRPCError('Boom', 2000, { ERROR_CODE: 'INVALID_TOKEN' })
console.log(err.message) // 'Boom'
console.log(err.code) // 2000
console.log(err.metadata instanceof grpc.Metadata) // true
console.log(err.metadata.getMap()) // { error_code: 'INVALID_TOKEN' }

Example (Source from error and merge metadatas)

const createGRPCError = require('create-grpc-error')

const existingError = new Error('Boom')
existingError.metadata = new grpc.Metadata()
existingError.metadata.add('foo', 'bar')

const err = createGRPCError(existingError, 2000, { ERROR_CODE: 'INVALID_TOKEN' })
console.log(err.message) // 'Boom'
console.log(err.code) // 2000
console.log(err.metadata instanceof grpc.Metadata) // true
console.log(err.metadata.getMap()) // { foo: 'bar', error_code: 'INVALID_TOKEN' }

applyCreate(err, message, code, metadata) ⇒ Error

Actual function that does all the work. Same as createGRPCError but applies cretion to the existing error.

Kind: global function
Returns: Error - See createGRPCError description

ParamTypeDescription
errErrorThe error to apply creation to
messageString | Number | Error | ObjectSee createGRPCError description
codeNumber | ObjectSee createGRPCError description
metadataObjectSee createGRPCError description

License

Apache-2.0

Keywords

FAQs

Package last updated on 30 Dec 2016

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