Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bojand/grpc-create-error
Utility to crete errors for gRPC responses
https://github.com/bojand/grpc-create-error
grpc hacktoberfest
Last synced: 10 days ago
JSON representation
Utility to crete errors for gRPC responses
- Host: GitHub
- URL: https://github.com/bojand/grpc-create-error
- Owner: bojand
- License: apache-2.0
- Created: 2016-12-30T03:01:57.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T02:32:49.000Z (over 1 year ago)
- Last Synced: 2024-10-13T07:54:17.765Z (26 days ago)
- Topics: grpc, hacktoberfest
- Language: JavaScript
- Homepage: https://bojand.github.io/grpc-create-error
- Size: 674 KB
- Stars: 8
- Watchers: 3
- Forks: 5
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-grpc - grpc-create-error - Utility function for creating `Errors` for gRPC responses (Language-Specific / Node.js)
README
# grpc-create-error
Utility function that creates an Error suitable for gRPC responses
[![npm version](https://img.shields.io/npm/v/grpc-create-error.svg?style=flat-square)](https://www.npmjs.com/package/grpc-create-error)
[![build status](https://img.shields.io/travis/bojand/grpc-create-error/master.svg?style=flat-square)](https://travis-ci.org/bojand/grpc-create-error)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](https://standardjs.com)
[![License](https://img.shields.io/github/license/bojand/grpc-create-error.svg?style=flat-square)](https://raw.githubusercontent.com/bojand/grpc-create-error/master/LICENSE)### Related
[grpc-error](https://github.com/bojand/grpc-error) - `GRPCError` class that uses this module
[grpc status codes](https://grpc.io/grpc/node/grpc.html) - The grpc status codes.
## 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| Param | Type | Description |
| --- | --- | --- |
| [message] |String
\|Number
\|Error
\|Object
| IfString
the error message IfNumber
the error code If instanceofError
, the error to source data from. We still create a newError
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. IfObject
, assumed to be metadata, either plain object representation or actualgrpc.Metadata
instance. We usegrpc-create-metadata
module to create metadata for the return value. |
| [code] |Number
\|Object
| IfNumber
the error code IfObject
, assumed to be metadata, either plain object representation or actualgrpc.Metadata
instance. We usegrpc-create-metadata
module to create metadata for the return value. |
| [metadata] |Object
| The error metadata. Either plain object representation or actualgrpc.Metadata
instance. We usegrpc-create-metadata
module to create metadata for the return value. |**Example** *(Using standard grpc status code)*
```js
const grpc = require('grpc')
const createGRPCError = require('create-grpc-error')
const err = createGRPCError('Ouch!', grpc.status.INVALID_ARGUMENT)
```
**Example** *(Custom error with metadata)*
```js
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)*
```js
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
- SeecreateGRPCError
description| Param | Type | Description |
| --- | --- | --- |
| err |Error
| The error to apply creation to |
| message |String
\|Number
\|Error
\|Object
| SeecreateGRPCError
description |
| code |Number
\|Object
| SeecreateGRPCError
description |
| metadata |Object
| SeecreateGRPCError
description |## License
Apache-2.0