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

feat!: use new error codes and data for ABCI responses #562

Merged
merged 14 commits into from
Sep 15, 2021
68 changes: 0 additions & 68 deletions lib/abci/errors/AbciError.js

This file was deleted.

13 changes: 0 additions & 13 deletions lib/abci/errors/InsufficientFundsError.js

This file was deleted.

29 changes: 0 additions & 29 deletions lib/abci/errors/InternalAbciError.js

This file was deleted.

18 changes: 0 additions & 18 deletions lib/abci/errors/InvalidArgumentAbciError.js

This file was deleted.

18 changes: 0 additions & 18 deletions lib/abci/errors/NotFoundAbciError.js

This file was deleted.

12 changes: 0 additions & 12 deletions lib/abci/errors/UnavailableAbciError.js

This file was deleted.

30 changes: 0 additions & 30 deletions lib/abci/errors/VerboseInternalAbciError.js

This file was deleted.

38 changes: 8 additions & 30 deletions lib/abci/errors/wrapInErrorHandlerFactory.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
const {
tendermint: {
abci: {
Event,
EventAttribute,
},
},
} = require('@dashevo/abci/types');

const AbciError = require('./AbciError');
const InternalAbciError = require('./InternalAbciError');
const VerboseInternalAbciError = require('./VerboseInternalAbciError');
const GrpcError = require('@dashevo/grpc-common/lib/server/error/GrpcError');
const InternalGrpcError = require('@dashevo/grpc-common/lib/server/error/InternalGrpcError');
const VerboseInternalGrpcError = require('@dashevo/grpc-common/lib/server/error/VerboseInternalGrpcError');

/**
* @param {BaseLogger} logger
Expand Down Expand Up @@ -46,12 +37,12 @@ function wrapInErrorHandlerFactory(logger, isProductionEnvironment) {
let error = e;

// Wrap all non ABCI errors to an internal ABCI error
if (!(e instanceof AbciError)) {
error = new InternalAbciError(e);
if (!(e instanceof GrpcError)) {
error = new InternalGrpcError(e);
}

// Log only internal ABCI errors
if (error instanceof InternalAbciError) {
if (error instanceof InternalGrpcError) {
// in consensus ABCI handlers (blockBegin, deliverTx, blockEnd, commit)
// we should propagate the error upwards
// to halt the Drive
Expand All @@ -69,31 +60,18 @@ function wrapInErrorHandlerFactory(logger, isProductionEnvironment) {
);

if (!isProductionEnvironment) {
error = new VerboseInternalAbciError(error);
error = new VerboseInternalGrpcError(error);
}
}

const events = [];

const attributes = Object.entries(error.getTags())
.map(([key, value]) => new EventAttribute({ key, value, index: true }));

if (attributes.length > 0) {
events.push(new Event({
type: 'error',
attributes,
}));
}

return {
code: error.getCode(),
log: JSON.stringify({
error: {
message: error.getMessage(),
data: error.getData(),
data: error.getRawMetadata(),
},
}),
events,
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/abci/handlers/deliverTxHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const AbstractDocumentTransition = require(
'@dashevo/dpp/lib/document/stateTransition/DocumentsBatchTransition/documentTransition/AbstractDocumentTransition',
);

const InvalidArgumentAbciError = require('../errors/InvalidArgumentAbciError');
const InvalidArgumentGrpcError = require('@dashevo/grpc-common/lib/server/error/InvalidArgumentGrpcError');

const DOCUMENT_ACTION_DESCRIPTONS = {
[AbstractDocumentTransition.ACTIONS.CREATE]: 'created',
Expand Down Expand Up @@ -87,7 +87,7 @@ function deliverTxHandlerFactory(

blockExecutionContext.incrementInvalidTxCount();

throw new InvalidArgumentAbciError('Invalid state transition', { errors: result.getErrors() });
throw new InvalidArgumentGrpcError('Invalid state transition', { errors: result.getErrors() });
}

// Apply state transition to the state
Expand Down
6 changes: 3 additions & 3 deletions lib/abci/handlers/query/dataContractQueryHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
},
} = require('@dashevo/dapi-grpc');

const NotFoundAbciError = require('../../errors/NotFoundAbciError');
const NotFoundGrpcError = require('@dashevo/grpc-common/lib/server/error/NotFoundGrpcError');

/**
*
Expand Down Expand Up @@ -44,7 +44,7 @@ function dataContractQueryHandlerFactory(
async function dataContractQueryHandler(params, { id }, request) {
// There is no signed state (current committed block height less then 2)
if (blockExecutionContext.isEmpty() || previousBlockExecutionContext.isEmpty()) {
throw new NotFoundAbciError('Data Contract not found');
throw new NotFoundGrpcError('Data Contract not found');
}

const response = createQueryResponse(GetDataContractResponse, request.prove);
Expand All @@ -53,7 +53,7 @@ function dataContractQueryHandlerFactory(

let dataContractBuffer;
if (!dataContract && !request.prove) {
throw new NotFoundAbciError('Data Contract not found');
throw new NotFoundGrpcError('Data Contract not found');
} else if (dataContract) {
dataContractBuffer = dataContract.toBuffer();
}
Expand Down
10 changes: 5 additions & 5 deletions lib/abci/handlers/query/documentQueryHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const {
},
} = require('@dashevo/dapi-grpc');

const InvalidArgumentGrpcError = require('@dashevo/grpc-common/lib/server/error/InvalidArgumentGrpcError');
const UnavailableGrpcError = require('@dashevo/grpc-common/lib/server/error/UnavailableGrpcError');
const InvalidQueryError = require('../../../document/errors/InvalidQueryError');
const InvalidArgumentAbciError = require('../../errors/InvalidArgumentAbciError');
const UnavailableAbciError = require('../../errors/UnavailableAbciError');

/**
*
Expand Down Expand Up @@ -77,13 +77,13 @@ function documentQueryHandlerFactory(
}

if (!container.has('previousBlockExecutionStoreTransactions')) {
throw new UnavailableAbciError();
throw new UnavailableGrpcError();
}

const previousBlockExecutionTransactions = container.resolve('previousBlockExecutionStoreTransactions');
const dataContractTransaction = previousBlockExecutionTransactions.getTransaction('dataContracts');
if (!dataContractTransaction.isStarted()) {
throw new UnavailableAbciError();
throw new UnavailableGrpcError();
}

const response = createQueryResponse(GetDocumentsResponse, request.prove);
Expand All @@ -100,7 +100,7 @@ function documentQueryHandlerFactory(
});
} catch (e) {
if (e instanceof InvalidQueryError) {
throw new InvalidArgumentAbciError(
throw new InvalidArgumentGrpcError(
`Invalid query: ${e.getErrors()[0].message}`,
{ errors: e.getErrors() },
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
},
} = require('@dashevo/dapi-grpc');

const InvalidArgumentAbciError = require('../../errors/InvalidArgumentAbciError');
const InvalidArgumentGrpcError = require('@dashevo/grpc-common/lib/server/error/InvalidArgumentGrpcError');

/**
*
Expand Down Expand Up @@ -50,7 +50,7 @@ function identitiesByPublicKeyHashesQueryHandlerFactory(
*/
async function identitiesByPublicKeyHashesQueryHandler(params, { publicKeyHashes }, request) {
if (publicKeyHashes && publicKeyHashes.length > maxIdentitiesPerRequest) {
throw new InvalidArgumentAbciError(
throw new InvalidArgumentGrpcError(
`Maximum number of ${maxIdentitiesPerRequest} requested items exceeded.`, {
maxIdentitiesPerRequest,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
},
} = require('@dashevo/dapi-grpc');

const InvalidArgumentAbciError = require('../../errors/InvalidArgumentAbciError');
const InvalidArgumentGrpcError = require('@dashevo/grpc-common/lib/server/error/InvalidArgumentGrpcError');

/**
*
Expand Down Expand Up @@ -46,7 +46,7 @@ function identityIdsByPublicKeyHashesQueryHandlerFactory(
*/
async function identityIdsByPublicKeyHashesQueryHandler(params, { publicKeyHashes }, request) {
if (publicKeyHashes && publicKeyHashes.length > maxIdentitiesPerRequest) {
throw new InvalidArgumentAbciError(
throw new InvalidArgumentGrpcError(
`Maximum number of ${maxIdentitiesPerRequest} requested items exceeded.`, {
maxIdentitiesPerRequest,
},
Expand Down
Loading