Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
feat: add additional gRPC errors
Browse files Browse the repository at this point in the history
- NotFoundGrpcError
- DeadlineExceededGrpcError
- ResourceExhaustedGrpcError
  • Loading branch information
shumkov authored Mar 9, 2020
1 parent c27662e commit 4c55d79
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 14 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const wrapInErrorHandlerFactory = require('./lib/server/error/wrapInErrorHandler
const FailedPreconditionGrpcError = require('./lib/server/error/FailedPreconditionGrpcError');
const InvalidArgumentGrpcError = require('./lib/server/error/InvalidArgumentGrpcError');
const InternalGrpcError = require('./lib/server/error/InternalGrpcError');
const ResourceExhaustedGrpcError = require('./lib/server/error/ResourceExhaustedGrpcError');
const DeadlineExceededGrpcError = require('./lib/server/error/DeadlineExceededGrpcError');
const NotFoundGrpcError = require('./lib/server/error/NotFoundGrpcError');
const GrpcError = require('./lib/server/error/GrpcError');

const isObject = require('./lib/utils/isObject');
Expand Down Expand Up @@ -45,6 +48,9 @@ module.exports = {
InternalGrpcError,
InvalidArgumentGrpcError,
FailedPreconditionGrpcError,
ResourceExhaustedGrpcError,
DeadlineExceededGrpcError,
NotFoundGrpcError,
},
},
utils: {
Expand Down
13 changes: 13 additions & 0 deletions lib/server/error/DeadlineExceededGrpcError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const GrpcError = require('./GrpcError');

class DeadlineExceededGrpcError extends GrpcError {
/**
* @param {string} message
* @param {Object} [metadata]
*/
constructor(message, metadata = undefined) {
super(GrpcError.CODES.DEADLINE_EXCEEDED, message, metadata);
}
}

module.exports = DeadlineExceededGrpcError;
5 changes: 4 additions & 1 deletion lib/server/error/GrpcError.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ class GrpcError extends Error {
}

GrpcError.CODES = {
INTERNAL: 13,
INVALID_ARGUMENT: 3,
DEADLINE_EXCEEDED: 4,
NOT_FOUND: 5,
RESOURCE_EXHAUSTED: 8,
FAILED_PRECONDITION: 9,
INTERNAL: 13,
};

module.exports = GrpcError;
2 changes: 1 addition & 1 deletion lib/server/error/InvalidArgumentGrpcError.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class InvalidArgumentGrpcError extends GrpcError {
* @param {Object} [metadata]
*/
constructor(message, metadata = undefined) {
super(GrpcError.CODES.INVALID_ARGUMENT, `Invalid argument: ${message}`, metadata);
super(GrpcError.CODES.INVALID_ARGUMENT, message, metadata);
}
}

Expand Down
13 changes: 13 additions & 0 deletions lib/server/error/NotFoundGrpcError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const GrpcError = require('./GrpcError');

class NotFoundGrpcError extends GrpcError {
/**
* @param {string} message
* @param {Object} [metadata]
*/
constructor(message, metadata = undefined) {
super(GrpcError.CODES.NOT_FOUND, message, metadata);
}
}

module.exports = NotFoundGrpcError;
13 changes: 13 additions & 0 deletions lib/server/error/ResourceExhaustedGrpcError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const GrpcError = require('./GrpcError');

class ResourceExhaustedGrpcError extends GrpcError {
/**
* @param {string} message
* @param {Object} [metadata]
*/
constructor(message, metadata = undefined) {
super(GrpcError.CODES.RESOURCE_EXHAUSTED, message, metadata);
}
}

module.exports = ResourceExhaustedGrpcError;
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dashevo/grpc-common",
"version": "0.2.0",
"version": "0.2.1",
"description": "Common GRPC library",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/server/error/InvalidArgumentGrpcError.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('InvalidArgumentGrpcError', () => {
it('should return message', () => {
const result = error.getMessage();

expect(result).to.equal(`Invalid argument: ${message}`);
expect(result).to.equal(message);
});
});

Expand Down

0 comments on commit 4c55d79

Please sign in to comment.