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

feat!: create an error instance from code and required data #342

Merged
merged 11 commits into from
Sep 7, 2021
4 changes: 2 additions & 2 deletions lib/dataContract/DataContractFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const generateDataContractId = require('./generateDataContractId');
const DataContractCreateTransition = require('./stateTransition/DataContractCreateTransition/DataContractCreateTransition');

const generateEntropy = require('../util/generateEntropy');
const ConsensusError = require('../errors/consensus/ConsensusError');
const AbstractConsensusError = require('../errors/consensus/AbstractConsensusError');

class DataContractFactory {
/**
Expand Down Expand Up @@ -88,7 +88,7 @@ class DataContractFactory {

rawDataContract.protocolVersion = protocolVersion;
} catch (error) {
if (error instanceof ConsensusError) {
if (error instanceof AbstractConsensusError) {
throw new InvalidDataContractError([error]);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/dataContract/errors/InvalidDataContractError.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const DPPError = require('../../errors/DPPError');

class InvalidDataContractError extends DPPError {
/**
* @param {ConsensusError[]} errors
* @param {AbstractConsensusError[]} errors
* @param {RawDataContract} rawDataContract
*/
constructor(errors, rawDataContract) {
Expand All @@ -20,7 +20,7 @@ class InvalidDataContractError extends DPPError {
/**
* Get validation errors
*
* @return {ConsensusError[]}
* @return {AbstractConsensusError[]}
*/
getErrors() {
return this.errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function validateDataContractCreateTransitionStateFactory(stateRepository) {

if (existingDataContract) {
result.addError(
new DataContractAlreadyPresentError(dataContract),
new DataContractAlreadyPresentError(dataContractId.toBuffer()),
);
}

Expand Down
9 changes: 1 addition & 8 deletions lib/dataContract/validation/validateDataContractFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ module.exports = function validateDataContractFactory(
isUniqueIndexLimitReached = true;

result.addError(new UniqueIndicesLimitReachedError(
rawDataContract,
documentType,
));
}
Expand All @@ -133,7 +132,6 @@ module.exports = function validateDataContractFactory(

if (isSingleIndex) {
result.addError(new SystemPropertyIndexAlreadyPresentError(
rawDataContract,
documentType,
indexDefinition,
propertyName,
Expand All @@ -159,7 +157,6 @@ module.exports = function validateDataContractFactory(
.map(([propertyName]) => {
result.addError(
new UndefinedIndexPropertyError(
rawDataContract,
documentType,
indexDefinition,
propertyName,
Expand Down Expand Up @@ -205,7 +202,6 @@ module.exports = function validateDataContractFactory(

if (invalidPropertyType) {
result.addError(new InvalidIndexPropertyTypeError(
rawDataContract,
documentType,
indexDefinition,
propertyName,
Expand All @@ -219,7 +215,6 @@ module.exports = function validateDataContractFactory(
if (maxLength === undefined) {
result.addError(
new InvalidIndexedPropertyConstraintError(
rawDataContract,
documentType,
indexDefinition,
propertyName,
Expand All @@ -232,7 +227,6 @@ module.exports = function validateDataContractFactory(
if (maxLength !== undefined && maxLength > MAX_INDEXED_STRING_PROPERTY_LENGTH) {
result.addError(
new InvalidIndexedPropertyConstraintError(
rawDataContract,
documentType,
indexDefinition,
propertyName,
Expand All @@ -254,7 +248,7 @@ module.exports = function validateDataContractFactory(

if (!allAreRequired && !allAreNotRequired) {
result.addError(
new InvalidCompoundIndexError(rawDataContract, documentType, indexDefinition),
new InvalidCompoundIndexError(documentType, indexDefinition),
);
}
}
Expand All @@ -265,7 +259,6 @@ module.exports = function validateDataContractFactory(
if (indicesFingerprints.includes(indicesFingerprint)) {
result.addError(
new DuplicateIndexError(
rawDataContract,
documentType,
indexDefinition,
),
Expand Down
12 changes: 10 additions & 2 deletions lib/dataContract/validation/validateDataContractMaxDepthFactory.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const lodashCloneDeep = require('lodash.clonedeep');
const ValidationResult = require('../../validation/ValidationResult');
const JsonSchemaError = require('../../errors/consensus/basic/JsonSchemaError');
const DataContractMaxDepthExceedError = require('../../errors/consensus/basic/dataContract/DataContractMaxDepthExceedError');
const InvalidJsonSchemaRefError = require('../../errors/consensus/basic/dataContract/InvalidJsonSchemaRefError');

/**
* Check that JSON Schema max depth is less than max value
Expand Down Expand Up @@ -64,7 +64,11 @@ function validateDataContractMaxDepthFactory(refParser) {
},
});
} catch (error) {
result.addError(new JsonSchemaError(error));
const consensusError = new InvalidJsonSchemaRefError(error.message);

consensusError.setRefError(error);

result.addError(consensusError);

return result;
}
Expand All @@ -74,7 +78,11 @@ function validateDataContractMaxDepthFactory(refParser) {
} catch (error) {
if (error instanceof DataContractMaxDepthExceedError) {
result.addError(error);

return result;
}

throw error;
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const traverse = require('json-schema-traverse');
const ValidationResult = require('../../validation/ValidationResult');
const IncompatibleRe2PatternError = require('../../document/errors/IncompatibleRe2PatternError');
const IncompatibleRe2PatternError = require('../../errors/consensus/basic/dataContract/IncompatibleRe2PatternError');

/**
*
Expand All @@ -27,7 +27,11 @@ function validateDataContractPatternsFactory(
// eslint-disable-next-line no-new
new RE2(value, 'u');
} catch (e) {
result.addError(new IncompatibleRe2PatternError(value, path, e.message));
const consensusError = new IncompatibleRe2PatternError(value, path, e.message);

consensusError.setPatternError(e);

result.addError(consensusError);
}
}
});
Expand Down
26 changes: 15 additions & 11 deletions lib/dataTrigger/DataTrigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,32 @@ class DataTrigger {
* @returns {Promise<DataTriggerExecutionResult>}
*/
async execute(documentTransition, context) {
let result = new DataTriggerExecutionResult();
let result;

try {
result = await this.trigger(documentTransition, context, this.topLevelIdentity);
} catch (e) {
result.addError(
new DataTriggerExecutionError(
this,
context.getDataContract(),
context.getOwnerId(),
e,
),
result = new DataTriggerExecutionResult();

const consensusError = new DataTriggerExecutionError(
context.getDataContract().getId().toBuffer(),
documentTransition.getId().toBuffer(),
e.message,
);

consensusError.setExecutionError(e);

result.addError(consensusError);

return result;
}

if (!(result instanceof DataTriggerExecutionResult)) {
result = new DataTriggerExecutionResult();
result.addError(
new DataTriggerInvalidResultError(
this,
context.getDataContract(),
context.getOwnerId(),
context.getDataContract().getId().toBuffer(),
documentTransition.getId().toBuffer(),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ async function createContactRequestDataTrigger(documentTransition, context) {
const heightWindowEnd = coreChainLockedHeight + BLOCKS_WINDOW_SIZE;

if (coreHeightCreatedAt < heightWindowStart || coreHeightCreatedAt > heightWindowEnd) {
result.addError(
new DataTriggerConditionError(
documentTransition,
context.getDataContract(),
context.getOwnerId(),
'Core height is out of block height window',
),
const error = new DataTriggerConditionError(
context.getDataContract().getId().toBuffer(),
documentTransition.getId().toBuffer(),
`Core height ${coreHeightCreatedAt} is out of block height window from ${heightWindowStart} to ${heightWindowEnd}`,
);

error.setOwnerId(context.getOwnerId());
error.setDocumentTransition(documentTransition);

result.addError(error);
}

return result;
Expand Down
Loading