From 4c55d79642d3c017ea04c782f223f9e98d721222 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Mon, 9 Mar 2020 22:54:00 +0300 Subject: [PATCH] feat: add additional gRPC errors - NotFoundGrpcError - DeadlineExceededGrpcError - ResourceExhaustedGrpcError --- index.js | 6 ++++++ lib/server/error/DeadlineExceededGrpcError.js | 13 ++++++++++++ lib/server/error/GrpcError.js | 5 ++++- lib/server/error/InvalidArgumentGrpcError.js | 2 +- lib/server/error/NotFoundGrpcError.js | 13 ++++++++++++ .../error/ResourceExhaustedGrpcError.js | 13 ++++++++++++ package-lock.json | 20 +++++++++---------- package.json | 2 +- .../error/InvalidArgumentGrpcError.spec.js | 2 +- 9 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 lib/server/error/DeadlineExceededGrpcError.js create mode 100644 lib/server/error/NotFoundGrpcError.js create mode 100644 lib/server/error/ResourceExhaustedGrpcError.js diff --git a/index.js b/index.js index 87a86ff..a7e50dd 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -45,6 +48,9 @@ module.exports = { InternalGrpcError, InvalidArgumentGrpcError, FailedPreconditionGrpcError, + ResourceExhaustedGrpcError, + DeadlineExceededGrpcError, + NotFoundGrpcError, }, }, utils: { diff --git a/lib/server/error/DeadlineExceededGrpcError.js b/lib/server/error/DeadlineExceededGrpcError.js new file mode 100644 index 0000000..51d915f --- /dev/null +++ b/lib/server/error/DeadlineExceededGrpcError.js @@ -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; diff --git a/lib/server/error/GrpcError.js b/lib/server/error/GrpcError.js index d1827e0..a18520a 100644 --- a/lib/server/error/GrpcError.js +++ b/lib/server/error/GrpcError.js @@ -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; diff --git a/lib/server/error/InvalidArgumentGrpcError.js b/lib/server/error/InvalidArgumentGrpcError.js index d10e89b..f2757d9 100644 --- a/lib/server/error/InvalidArgumentGrpcError.js +++ b/lib/server/error/InvalidArgumentGrpcError.js @@ -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); } } diff --git a/lib/server/error/NotFoundGrpcError.js b/lib/server/error/NotFoundGrpcError.js new file mode 100644 index 0000000..2bd6153 --- /dev/null +++ b/lib/server/error/NotFoundGrpcError.js @@ -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; diff --git a/lib/server/error/ResourceExhaustedGrpcError.js b/lib/server/error/ResourceExhaustedGrpcError.js new file mode 100644 index 0000000..9c4ff0e --- /dev/null +++ b/lib/server/error/ResourceExhaustedGrpcError.js @@ -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; diff --git a/package-lock.json b/package-lock.json index e1fc5d5..e314ba5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@dashevo/grpc-common", - "version": "0.2.0", + "version": "0.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -220,9 +220,9 @@ "integrity": "sha512-An+MXSV8CGXz/BO9C1KKsoJ/8WDrvlNUaRMsm2h+IHZuSyQkM8U5bJJkb8ItLKA73VePG/nUK+t+EuW2IWuhsQ==" }, "acorn": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.0.tgz", - "integrity": "sha512-kL5CuoXA/dgxlBbVrflsflzQ3PAas7RYZB52NOm/6839iVYJgKMJ3cQJD+t2i5+qFa8h3MDpEOJiS64E8JLnSQ==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.1.1.tgz", + "integrity": "sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==", "dev": true }, "acorn-jsx": { @@ -1639,9 +1639,9 @@ } }, "handlebars": { - "version": "4.4.5", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.4.5.tgz", - "integrity": "sha512-0Ce31oWVB7YidkaTq33ZxEbN+UDxMMgThvCe8ptgQViymL5DPis9uLdTA13MiRPhgvqyxIegugrP97iK3JeBHg==", + "version": "4.7.3", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.3.tgz", + "integrity": "sha512-SRGwSYuNfx8DwHD/6InAPzD6RgeruWLT+B8e8a7gGs8FWgHzlExpTFMEq2IA6QpAfOClpKHy6+8IqTjeBCu6Kg==", "dev": true, "requires": { "neo-async": "^2.6.0", @@ -3376,9 +3376,9 @@ "dev": true }, "uglify-js": { - "version": "3.6.3", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.3.tgz", - "integrity": "sha512-KfQUgOqTkLp2aZxrMbCuKCDGW9slFYu2A23A36Gs7sGzTLcRBDORdOi5E21KWHFIfkY8kzgi/Pr1cXCh0yIp5g==", + "version": "3.8.0", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.8.0.tgz", + "integrity": "sha512-ugNSTT8ierCsDHso2jkBHXYrU8Y5/fY2ZUprfrJUiD7YpuFvV4jODLFmb3h4btQjqr5Nh4TX4XtgDfCU1WdioQ==", "dev": true, "optional": true, "requires": { diff --git a/package.json b/package.json index 3531ec0..1e2df69 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dashevo/grpc-common", - "version": "0.2.0", + "version": "0.2.1", "description": "Common GRPC library", "main": "index.js", "scripts": { diff --git a/test/unit/server/error/InvalidArgumentGrpcError.spec.js b/test/unit/server/error/InvalidArgumentGrpcError.spec.js index 86d00f1..77f87f7 100644 --- a/test/unit/server/error/InvalidArgumentGrpcError.spec.js +++ b/test/unit/server/error/InvalidArgumentGrpcError.spec.js @@ -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); }); });