Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more detailed error messages for jdonld safeValidation errors #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 181 additions & 42 deletions managers/asset-operations-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { assertionMetadata, formatAssertion, calculateRoot } = require('assertion-tools');
const JsonLdError = require('jsonld/lib/JsonLdError.js');
const {
isEmptyObject,
deriveUAL,
Expand Down Expand Up @@ -77,10 +78,32 @@ class AssetOperationsManager {
authToken,
);

let operationResult = {};

let privateAssertion;
let privateAssertionId;
if (jsonContent.private && !isEmptyObject(jsonContent.private)) {
privateAssertion = await formatAssertion(jsonContent.private);
try {
privateAssertion = await formatAssertion(jsonContent.private);
} catch (error) {
let errorMessage;
if (error instanceof JsonLdError && Object.hasOwn(error, 'details')) {
errorMessage = error.details.message;
} else {
errorMessage = error.message;
}

operationResult.status = undefined;
operationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage,
};

return {
operation: getOperationStatusObject(operationResult, undefined),
};
}

privateAssertionId = calculateRoot(privateAssertion);
}
const publicGraph = {
Expand All @@ -95,7 +118,29 @@ class AssetOperationsManager {
: null,
],
};
const publicAssertion = await formatAssertion(publicGraph);

let publicAssertion;
try {
publicAssertion = await formatAssertion(publicGraph);
} catch (error) {
let errorMessage;
if (error instanceof JsonLdError && Object.hasOwn(error, 'details')) {
errorMessage = error.details.message;
} else {
errorMessage = error.message;
}

operationResult.status = undefined;
operationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage,
};

return {
operation: getOperationStatusObject(operationResult, undefined),
};
}

const publicAssertionId = calculateRoot(publicAssertion);

const contentAssetStorageAddress = await this.blockchainService.getContractAddress(
Expand Down Expand Up @@ -159,7 +204,7 @@ class AssetOperationsManager {
authToken,
assertions,
);
let operationResult = await this.nodeApiService.getOperationResult(
operationResult = await this.nodeApiService.getOperationResult(
endpoint,
port,
authToken,
Expand Down Expand Up @@ -298,27 +343,44 @@ class AssetOperationsManager {
const publicAssertion = getPublicOperationResult.data.assertion;

if (validate === true && calculateRoot(publicAssertion) !== publicAssertionId) {
getPublicOperationResult.status = undefined;
getPublicOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage: "Calculated root hashes don't match!",
};

return {
operation: getOperationStatusObject(getPublicOperationResult, undefined),
};
}

let result = { operation: {} };
if (contentType !== CONTENT_TYPES.PRIVATE) {
let formattedPublicAssertion = publicAssertion;
try {
if (outputFormat !== GET_OUTPUT_FORMATS.N_QUADS) {
formattedPublicAssertion = await toJSONLD(publicAssertion.join('\n'));
} else {
formattedPublicAssertion = publicAssertion.join('\n');
}
} catch (error) {
getPublicOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage: error.message,
};
}
if (outputFormat !== GET_OUTPUT_FORMATS.N_QUADS) {
try {
formattedPublicAssertion = await toJSONLD(publicAssertion.join('\n'));
} catch (error) {
let errorMessage;
if (error instanceof JsonLdError && Object.hasOwn(error, 'details')) {
errorMessage = error.details.message;
} else {
errorMessage = error.message;
}

getPublicOperationResult.status = undefined;
getPublicOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage,
};

return {
operation: getOperationStatusObject(getPublicOperationResult, undefined),
};
}
} else {
formattedPublicAssertion = publicAssertion.join('\n');
}

if (contentType === CONTENT_TYPES.PUBLIC) {
result = {
Expand All @@ -333,7 +395,7 @@ class AssetOperationsManager {
};
}

result.operation.publicGet = getOperationStatusObject(
result.operation = getOperationStatusObject(
getPublicOperationResult,
getPublicOperationId,
);
Expand Down Expand Up @@ -377,35 +439,72 @@ class AssetOperationsManager {
);

const privateAssertionNQuads = queryPrivateOperationResult.data;

const privateAssertion = await toNQuads(
privateAssertionNQuads,
'application/n-quads',
);

let privateAssertion;
try {
privateAssertion = await toNQuads(
privateAssertionNQuads,
'application/n-quads',
);
} catch (error) {
let errorMessage;
if (error instanceof JsonLdError && Object.hasOwn(error, 'details')) {
errorMessage = error.details.message;
} else {
errorMessage = error.message;
}

queryPrivateOperationResult.status = undefined;
queryPrivateOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage,
};

return {
operation: getOperationStatusObject(queryPrivateOperationResult, undefined),
};
}

let formattedPrivateAssertion;
if (
privateAssertion.length &&
validate === true &&
calculateRoot(privateAssertion) !== privateAssertionId
) {
queryPrivateOperationResult.status = undefined;
queryPrivateOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage: "Calculated root hashes don't match!",
};

return {
operation: getOperationStatusObject(queryPrivateOperationResult, undefined),
};
}

try {
if (outputFormat !== GET_OUTPUT_FORMATS.N_QUADS) {
if (outputFormat !== GET_OUTPUT_FORMATS.N_QUADS) {
try {
formattedPrivateAssertion = await toJSONLD(privateAssertion.join('\n'));
} else {
formattedPrivateAssertion = privateAssertion.join('\n');
} catch (error) {
let errorMessage;
if (error instanceof JsonLdError && Object.hasOwn(error, 'details')) {
errorMessage = error.details.message;
} else {
errorMessage = error.message;
}

queryPrivateOperationResult.status = undefined;
queryPrivateOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage,
};

return {
operation: getOperationStatusObject(queryPrivateOperationResult, undefined),
};
}
} catch (error) {
queryPrivateOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage: error.message,
};
} else {
formattedPrivateAssertion = privateAssertion.join('\n');
}

if (contentType === CONTENT_TYPES.PRIVATE) {
Expand All @@ -420,7 +519,7 @@ class AssetOperationsManager {
assertionId: privateAssertionId,
};
}
result.operation.queryPrivate = getOperationStatusObject(
result.operation = getOperationStatusObject(
queryPrivateOperationResult,
queryPrivateOperationId,
);
Expand Down Expand Up @@ -468,10 +567,32 @@ class AssetOperationsManager {
);

const { tokenId } = resolveUAL(UAL);

let operationResult = {};

let privateAssertion;
let privateAssertionId;
if (jsonContent.private && !isEmptyObject(jsonContent.private)) {
privateAssertion = await formatAssertion(jsonContent.private);
try {
privateAssertion = await formatAssertion(jsonContent.private);
} catch (error) {
let errorMessage;
if (error instanceof JsonLdError && Object.hasOwn(error, 'details')) {
errorMessage = error.details.message;
} else {
errorMessage = error.message;
}

operationResult.status = undefined;
operationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage,
};

return {
operation: getOperationStatusObject(operationResult, undefined),
};
}
privateAssertionId = calculateRoot(privateAssertion);
}

Expand Down Expand Up @@ -504,16 +625,16 @@ class AssetOperationsManager {
publicAssertion = getPublicOperationResult.data.assertion;

if (calculateRoot(publicAssertion) !== publicAssertionId) {
getPublicOperationResult.status = undefined;
getPublicOperationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage: "Calculated root hashes don't match!",
};

// TODO: Check returned response
return {
UAL,
getPublicOperationResult
}
operation: getOperationStatusObject(getPublicOperationResult, undefined),
};
}

// Transform public assertion to include updated private assertion Id
Expand All @@ -537,7 +658,27 @@ class AssetOperationsManager {
: null,
],
};
publicAssertion = await formatAssertion(publicGraph);

try {
publicAssertion = await formatAssertion(publicGraph);
} catch (error) {
let errorMessage;
if (error instanceof JsonLdError && Object.hasOwn(error, 'details')) {
errorMessage = error.details.message;
} else {
errorMessage = error.message;
}

operationResult.status = undefined;
operationResult.data = {
errorType: 'DKG_CLIENT_ERROR',
errorMessage,
};

return {
operation: getOperationStatusObject(operationResult, undefined),
};
}
}

const publicAssertionId = calculateRoot(publicAssertion);
Expand Down Expand Up @@ -602,7 +743,7 @@ class AssetOperationsManager {
assertions,
);

let operationResult = await this.nodeApiService.getOperationResult(
operationResult = await this.nodeApiService.getOperationResult(
endpoint,
port,
authToken,
Expand Down Expand Up @@ -640,13 +781,11 @@ class AssetOperationsManager {
frequency,
operationId,
);

return {
UAL,
operation: getOperationStatusObject(
operationResult,
operationId
),
};
operation: getOperationStatusObject(operationResult, operationId),
}
}

async waitFinalization(UAL, options = {}) {
Expand Down
Loading