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

Commit 126c929

Browse files
author
Konstantin Shuplenkov
authored
fix: "not found" instead of "invalid argument" in gRPC endpoints
1 parent f8764e9 commit 126c929

7 files changed

+32
-6
lines changed

lib/grpcServer/handlers/core/getBlockHandlerFactory.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
server: {
77
error: {
88
InvalidArgumentGrpcError,
9+
NotFoundGrpcError,
910
},
1011
},
1112
} = require('@dashevo/grpc-common');
@@ -37,7 +38,7 @@ function getBlockHandlerFactory(insightAPI) {
3738
serializedBlock = await insightAPI.getRawBlockByHash(hash);
3839
} catch (e) {
3940
if (e.statusCode === 404) {
40-
throw new InvalidArgumentGrpcError('Invalid block hash');
41+
throw new NotFoundGrpcError('Block not found');
4142
}
4243

4344
throw e;

lib/grpcServer/handlers/core/getTransactionHandlerFactory.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
server: {
99
error: {
1010
InvalidArgumentGrpcError,
11+
NotFoundGrpcError,
1112
},
1213
},
1314
} = require('@dashevo/grpc-common');
@@ -36,7 +37,7 @@ function getTransactionHandlerFactory(insightAPI) {
3637
serializedTransaction = await insightAPI.getRawTransactionById(id);
3738
} catch (e) {
3839
if (e.statusCode === 404) {
39-
throw new InvalidArgumentGrpcError('Transaction not found');
40+
throw new NotFoundGrpcError('Transaction not found');
4041
}
4142

4243
throw e;

lib/grpcServer/handlers/platform/getDataContractHandlerFactory.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {
22
server: {
33
error: {
44
InvalidArgumentGrpcError,
5+
NotFoundGrpcError,
56
},
67
},
78
} = require('@dashevo/grpc-common');
@@ -38,7 +39,7 @@ function getDataContractHandlerFactory(driveAPI, dpp) {
3839
dataContractJSON = await driveAPI.fetchContract(id);
3940
} catch (e) {
4041
if (e instanceof RPCError && e.code === -32602) {
41-
throw new InvalidArgumentGrpcError(e.message, e.data);
42+
throw new NotFoundGrpcError(e.message, e.data);
4243
}
4344

4445
throw e;

lib/grpcServer/handlers/platform/getIdentityHandlerFactory.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {
22
server: {
33
error: {
44
InvalidArgumentGrpcError,
5+
NotFoundGrpcError,
56
},
67
},
78
} = require('@dashevo/grpc-common');
@@ -46,6 +47,9 @@ function getIdentityHandlerFactory(rpcClient, handleAbciResponse) {
4647
handleAbciResponse(result.response);
4748

4849
const { response: { value: identityBase64 } } = result;
50+
if (!identityBase64) {
51+
throw new NotFoundGrpcError('Identity not found');
52+
}
4953

5054
const response = new GetIdentityResponse();
5155

test/unit/grpcServer/handlers/core/getBlockHandlerFactory.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {
22
server: {
33
error: {
44
InvalidArgumentGrpcError,
5+
NotFoundGrpcError,
56
},
67
},
78
} = require('@dashevo/grpc-common');
@@ -135,8 +136,8 @@ describe('getBlockHandlerFactory', () => {
135136

136137
expect.fail('should thrown InvalidArgumentGrpcError error');
137138
} catch (e) {
138-
expect(e).to.be.instanceOf(InvalidArgumentGrpcError);
139-
expect(e.getMessage()).to.equal('Invalid block hash');
139+
expect(e).to.be.instanceOf(NotFoundGrpcError);
140+
expect(e.getMessage()).to.equal('Block not found');
140141
expect(insightAPIMock.getRawBlockByHeight).to.be.not.called();
141142
expect(insightAPIMock.getRawBlockByHash).to.be.calledOnceWith(hash);
142143
}

test/unit/grpcServer/handlers/core/getTransactionHandlerFactory.js

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {
22
server: {
33
error: {
44
InvalidArgumentGrpcError,
5+
NotFoundGrpcError,
56
},
67
},
78
} = require('@dashevo/grpc-common');
@@ -71,4 +72,20 @@ describe('getTransactionHandlerFactory', () => {
7172
expect(insightAPIMock.getRawTransactionById).to.be.not.called();
7273
}
7374
});
75+
76+
it('should throw NotFoundGrpcError if transaction is not found', async () => {
77+
const error = new Error();
78+
error.statusCode = 404;
79+
insightAPIMock.getRawTransactionById.throws(error);
80+
81+
try {
82+
await getTransactionHandler(call);
83+
84+
expect.fail('should thrown InvalidArgumentGrpcError error');
85+
} catch (e) {
86+
expect(e).to.be.instanceOf(NotFoundGrpcError);
87+
expect(e.getMessage()).to.equal('Transaction not found');
88+
expect(insightAPIMock.getRawTransactionById).to.be.calledOnceWith(id);
89+
}
90+
});
7491
});

test/unit/grpcServer/handlers/platform/getDataContractHandlerFactory.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {
22
server: {
33
error: {
44
InvalidArgumentGrpcError,
5+
NotFoundGrpcError,
56
},
67
},
78
} = require('@dashevo/grpc-common');
@@ -99,7 +100,7 @@ describe('getDataContractHandlerFactory', () => {
99100

100101
expect.fail('should throw InvalidArgumentGrpcError error');
101102
} catch (e) {
102-
expect(e).to.be.instanceOf(InvalidArgumentGrpcError);
103+
expect(e).to.be.instanceOf(NotFoundGrpcError);
103104
expect(e.getMessage()).to.equal(message);
104105
expect(e.getMetadata()).to.deep.equal(data);
105106
expect(driveApiMock.fetchContract).to.be.calledOnceWith(id);

0 commit comments

Comments
 (0)