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

Commit

Permalink
fix: "not found" instead of "invalid argument" in gRPC endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shuplenkov authored Mar 10, 2020
1 parent f8764e9 commit 126c929
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/grpcServer/handlers/core/getBlockHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -37,7 +38,7 @@ function getBlockHandlerFactory(insightAPI) {
serializedBlock = await insightAPI.getRawBlockByHash(hash);
} catch (e) {
if (e.statusCode === 404) {
throw new InvalidArgumentGrpcError('Invalid block hash');
throw new NotFoundGrpcError('Block not found');
}

throw e;
Expand Down
3 changes: 2 additions & 1 deletion lib/grpcServer/handlers/core/getTransactionHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -36,7 +37,7 @@ function getTransactionHandlerFactory(insightAPI) {
serializedTransaction = await insightAPI.getRawTransactionById(id);
} catch (e) {
if (e.statusCode === 404) {
throw new InvalidArgumentGrpcError('Transaction not found');
throw new NotFoundGrpcError('Transaction not found');
}

throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -38,7 +39,7 @@ function getDataContractHandlerFactory(driveAPI, dpp) {
dataContractJSON = await driveAPI.fetchContract(id);
} catch (e) {
if (e instanceof RPCError && e.code === -32602) {
throw new InvalidArgumentGrpcError(e.message, e.data);
throw new NotFoundGrpcError(e.message, e.data);
}

throw e;
Expand Down
4 changes: 4 additions & 0 deletions lib/grpcServer/handlers/platform/getIdentityHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -46,6 +47,9 @@ function getIdentityHandlerFactory(rpcClient, handleAbciResponse) {
handleAbciResponse(result.response);

const { response: { value: identityBase64 } } = result;
if (!identityBase64) {
throw new NotFoundGrpcError('Identity not found');
}

const response = new GetIdentityResponse();

Expand Down
5 changes: 3 additions & 2 deletions test/unit/grpcServer/handlers/core/getBlockHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -135,8 +136,8 @@ describe('getBlockHandlerFactory', () => {

expect.fail('should thrown InvalidArgumentGrpcError error');
} catch (e) {
expect(e).to.be.instanceOf(InvalidArgumentGrpcError);
expect(e.getMessage()).to.equal('Invalid block hash');
expect(e).to.be.instanceOf(NotFoundGrpcError);
expect(e.getMessage()).to.equal('Block not found');
expect(insightAPIMock.getRawBlockByHeight).to.be.not.called();
expect(insightAPIMock.getRawBlockByHash).to.be.calledOnceWith(hash);
}
Expand Down
17 changes: 17 additions & 0 deletions test/unit/grpcServer/handlers/core/getTransactionHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -71,4 +72,20 @@ describe('getTransactionHandlerFactory', () => {
expect(insightAPIMock.getRawTransactionById).to.be.not.called();
}
});

it('should throw NotFoundGrpcError if transaction is not found', async () => {
const error = new Error();
error.statusCode = 404;
insightAPIMock.getRawTransactionById.throws(error);

try {
await getTransactionHandler(call);

expect.fail('should thrown InvalidArgumentGrpcError error');
} catch (e) {
expect(e).to.be.instanceOf(NotFoundGrpcError);
expect(e.getMessage()).to.equal('Transaction not found');
expect(insightAPIMock.getRawTransactionById).to.be.calledOnceWith(id);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
server: {
error: {
InvalidArgumentGrpcError,
NotFoundGrpcError,
},
},
} = require('@dashevo/grpc-common');
Expand Down Expand Up @@ -99,7 +100,7 @@ describe('getDataContractHandlerFactory', () => {

expect.fail('should throw InvalidArgumentGrpcError error');
} catch (e) {
expect(e).to.be.instanceOf(InvalidArgumentGrpcError);
expect(e).to.be.instanceOf(NotFoundGrpcError);
expect(e.getMessage()).to.equal(message);
expect(e.getMetadata()).to.deep.equal(data);
expect(driveApiMock.fetchContract).to.be.calledOnceWith(id);
Expand Down

0 comments on commit 126c929

Please sign in to comment.