Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
feat!: throw NotFoundError if data is not found (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Shuplenkov authored Jul 2, 2021
1 parent 894cc56 commit 0b5d0ec
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const {
} = require('@dashevo/dapi-grpc');

const grpcErrorCodes = require('@dashevo/grpc-common/lib/server/error/GrpcErrorCodes');
const bs58 = require('bs58');

const GetDataContractResponse = require('./GetDataContractResponse');
const Metadata = require('../response/Metadata');
const NotFoundError = require('../../errors/NotFoundError');

/**
* @param {GrpcTransport} grpcTransport
Expand Down Expand Up @@ -46,10 +47,7 @@ function getDataContractFactory(grpcTransport) {
);
} catch (e) {
if (e.code === grpcErrorCodes.NOT_FOUND) {
return new GetDataContractResponse(
null,
new Metadata({ height: 0, coreChainLockedHeight: 0 }),
);
throw new NotFoundError(`DataContract ${bs58.encode(contractId)} is not found`);
}

throw e;
Expand Down
7 changes: 5 additions & 2 deletions lib/methods/platform/getIdentity/getIdentityFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const {
} = require('@dashevo/dapi-grpc');

const grpcErrorCodes = require('@dashevo/grpc-common/lib/server/error/GrpcErrorCodes');

const bs58 = require('bs58');

const GetIdentityResponse = require('./GetIdentityResponse');
const Metadata = require('../response/Metadata');
const NotFoundError = require('../../errors/NotFoundError');

/**
* @param {GrpcTransport} grpcTransport
Expand Down Expand Up @@ -44,7 +47,7 @@ function getIdentityFactory(grpcTransport) {
);
} catch (e) {
if (e.code === grpcErrorCodes.NOT_FOUND) {
return new GetIdentityResponse(null, new Metadata({ height: 0, coreChainLockedHeight: 0 }));
throw new NotFoundError(`Identity ${bs58.encode(id)} is not found`);
}

throw e;
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"@dashevo/dapi-grpc": "~0.20.0-dev.4",
"@dashevo/dashcore-lib": "~0.19.25",
"axios": "^0.21.1",
"bs58": "^4.0.1",
"cbor": "^7.0.5",
"node-inspect-extracted": "^1.0.7",
"lodash.sample": "^4.2.1"
"lodash.sample": "^4.2.1",
"node-inspect-extracted": "^1.0.7"
},
"devDependencies": {
"@babel/core": "^7.10.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const grpcErrorCodes = require('@dashevo/grpc-common/lib/server/error/GrpcErrorC

const getDataContractFactory = require('../../../../../lib/methods/platform/getDataContract/getDataContractFactory');
const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture');
const NotFoundError = require('../../../../../lib/methods/errors/NotFoundError');

describe('getDataContractFactory', () => {
let grpcTransportMock;
Expand Down Expand Up @@ -67,15 +68,21 @@ describe('getDataContractFactory', () => {
);
});

it('should return null if data contract not found', async () => {
it('should throw NotFoundError if data contract not found', async () => {
const error = new Error('Nothing found');
error.code = grpcErrorCodes.NOT_FOUND;

grpcTransportMock.request.throws(error);

const contractId = dataContractFixture.getId();

const result = await getDataContract(contractId, options);
try {
await getDataContract(contractId, options);

expect.fail('should throw NotFoundError');
} catch (e) {
expect(e).to.be.an.instanceOf(NotFoundError);
}

const request = new GetDataContractRequest();
request.setId(contractId);
Expand All @@ -86,8 +93,6 @@ describe('getDataContractFactory', () => {
request,
options,
]);
expect(result.getDataContract()).to.equal(null);
expect(result.getMetadata()).to.deep.equal({ height: 0, coreChainLockedHeight: 0 });
});

it('should throw unknown error', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const grpcErrorCodes = require('@dashevo/grpc-common/lib/server/error/GrpcErrorC

const getIdentityFactory = require('../../../../../lib/methods/platform/getIdentity/getIdentityFactory');
const getMetadataFixture = require('../../../../../lib/test/fixtures/getMetadataFixture');
const NotFoundError = require('../../../../../lib/methods/errors/NotFoundError');

describe('getIdentityFactory', () => {
let grpcTransportMock;
Expand Down Expand Up @@ -63,13 +64,18 @@ describe('getIdentityFactory', () => {
expect(result.getMetadata()).to.deep.equal(metadataFixture);
});

it('should return null if identity not found', async () => {
it('should throw NotFoundError if identity not found', async () => {
const error = new Error('Nothing found');
error.code = grpcErrorCodes.NOT_FOUND;

grpcTransportMock.request.throws(error);
try {
await getIdentity(identityId, options);

const result = await getIdentity(identityId, options);
expect.fail('should throw NotFoundError');
} catch (e) {
expect(e).to.be.an.instanceOf(NotFoundError);
}

const request = new GetIdentityRequest();
request.setId(identityId.toBuffer());
Expand All @@ -80,8 +86,6 @@ describe('getIdentityFactory', () => {
request,
options,
);
expect(result.getIdentity()).to.equal(null);
expect(result.getMetadata()).to.deep.equal({ height: 0, coreChainLockedHeight: 0 });
});

it('should throw unknown error', async () => {
Expand Down

0 comments on commit 0b5d0ec

Please sign in to comment.