Skip to content

Commit

Permalink
refactor: Refactored exceptions (#263)
Browse files Browse the repository at this point in the history
* Refactored exceptions

* Synced exceptions in keymaster-app

* Added new test for verifyDb
  • Loading branch information
macterra authored Jul 31, 2024
1 parent f7516bc commit 4304dea
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 399 deletions.
4 changes: 2 additions & 2 deletions src/db-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function addEvent(did, event) {
const db = loadDb();

if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

const suffix = did.split(':').pop();
Expand Down Expand Up @@ -75,7 +75,7 @@ export async function getEvents(did) {

export async function setEvents(did, events) {
if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

const db = loadDb();
Expand Down
6 changes: 3 additions & 3 deletions src/db-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function resetDb() {

export async function addEvent(did, event) {
if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

const id = did.split(':').pop();
Expand All @@ -38,7 +38,7 @@ export async function addEvent(did, event) {

export async function getEvents(did) {
if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

try {
Expand All @@ -55,7 +55,7 @@ export async function getEvents(did) {

export async function deleteEvents(did) {
if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

const id = did.split(':').pop();
Expand Down
6 changes: 3 additions & 3 deletions src/db-sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function resetDb() {

export async function addEvent(did, event) {
if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

const events = await getEvents(did);
Expand All @@ -50,7 +50,7 @@ export async function addEvent(did, event) {

export async function getEvents(did) {
if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

try {
Expand All @@ -67,7 +67,7 @@ export async function getEvents(did) {

export async function deleteEvents(did) {
if (!did) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

const id = did.split(':').pop();
Expand Down
4 changes: 2 additions & 2 deletions src/db-wallet-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function loadWallet() {

export function newWallet(mnemonic, overwrite) {
if (fs.existsSync(walletName) && !overwrite) {
throw exceptions.UPDATE_FAILED;
throw new Error(exceptions.UPDATE_FAILED);
}

try {
Expand All @@ -50,6 +50,6 @@ export function newWallet(mnemonic, overwrite) {
return wallet;
}
catch (error) {
throw exceptions.INVALID_PARAMETER;
throw new Error(exceptions.INVALID_PARAMETER);
}
}
22 changes: 11 additions & 11 deletions src/exceptions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const INVALID_DID = new Error('Invalid DID');
export const UNKNOWN_DID = new Error('Unknown DID');
export const UNKNOWN_ID = new Error('Unknown ID');
export const INVALID_UPDATE = new Error('Invalid update');
export const INVALID_OPERATION = new Error('Invalid operation');
export const INVALID_VERSION = new Error('Invalid version');
export const INVALID_TYPE = new Error('Invalid type');
export const INVALID_REGISTRY = new Error('Invalid registry');
export const INVALID_PARAMETER = new Error('Invalid parameter');
export const UPDATE_FAILED = new Error('Update failed');
export const EXPECTED_EXCEPTION = new Error('Expected to throw an exception');
export const INVALID_DID = 'Invalid DID';
export const INVALID_OPERATION = 'Invalid operation';
export const INVALID_VERSION = 'Invalid version';
export const INVALID_TYPE = 'Invalid type';
export const INVALID_REGISTRY = 'Invalid registry';

export const UNKNOWN_ID = 'Unknown ID';
export const NO_CURRENT_ID = 'No current ID';
export const INVALID_PARAMETER = 'Invalid parameter';
export const UPDATE_FAILED = 'Update failed';
export const EXPECTED_EXCEPTION = 'Expected to throw an exception';
52 changes: 26 additions & 26 deletions src/gatekeeper-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export async function anchorSeed(seed) {

async function verifyCreateAgent(operation) {
if (!operation.signature) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

if (!operation.publicJwk) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

const operationCopy = JSON.parse(JSON.stringify(operation));
Expand All @@ -102,13 +102,13 @@ async function verifyCreateAgent(operation) {

async function verifyCreateAsset(operation) {
if (operation.controller !== operation.signature?.signer) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

const doc = await resolveDID(operation.signature.signer, { confirm: true, atTime: operation.signature.signed });

if (doc.mdip.registry === 'local' && operation.mdip.registry !== 'local') {
throw exceptions.INVALID_REGISTRY;
throw new Error(exceptions.INVALID_REGISTRY);
}

const operationCopy = JSON.parse(JSON.stringify(operation));
Expand All @@ -121,28 +121,28 @@ async function verifyCreateAsset(operation) {

async function verifyCreate(operation) {
if (operation?.type !== "create") {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

if (!operation.created) {
// TBD ensure valid timestamp format
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

if (!operation.mdip) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

if (!validVersions.includes(operation.mdip.version)) {
throw exceptions.INVALID_VERSION;
throw new Error(exceptions.INVALID_VERSION);
}

if (!validTypes.includes(operation.mdip.type)) {
throw exceptions.INVALID_TYPE;
throw new Error(exceptions.INVALID_TYPE);
}

if (!validRegistries.includes(operation.mdip.registry)) {
throw exceptions.INVALID_REGISTRY;
throw new Error(exceptions.INVALID_REGISTRY);
}

if (operation.mdip.type === 'agent') {
Expand All @@ -153,7 +153,7 @@ async function verifyCreate(operation) {
return verifyCreateAsset(operation);
}

throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

export async function createDID(operation) {
Expand Down Expand Up @@ -183,7 +183,7 @@ export async function createDID(operation) {
return did;
}
else {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}
}

Expand Down Expand Up @@ -289,7 +289,7 @@ async function verifyUpdate(operation, doc) {
}

export async function resolveDID(did, { atTime, atVersion, confirm, verify } = {}) {
const confirmedCacheable = confirm && !atTime && !atVersion;
const confirmedCacheable = !!confirm && !atTime && !atVersion;
const unconfirmedCacheable = !confirm && !atTime && !atVersion;

if (confirmedCacheable && !verify && confirmedCache[did]) {
Expand All @@ -303,15 +303,15 @@ export async function resolveDID(did, { atTime, atVersion, confirm, verify } = {
const events = await db.getEvents(did);

if (events.length === 0) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

const anchor = events[0];
let doc = await generateDoc(anchor.operation);
let mdip = doc?.mdip;

if (!mdip) {
throw exceptions.INVALID_DID;
throw new Error(exceptions.INVALID_DID);
}

if (atTime && new Date(mdip.created) > new Date(atTime)) {
Expand Down Expand Up @@ -343,7 +343,7 @@ export async function resolveDID(did, { atTime, atVersion, confirm, verify } = {
const valid = await verifyUpdate(operation, doc);

if (!valid) {
throw exceptions.INVALID_UPDATE;
throw new Error(exceptions.INVALID_OPERATION);
}
}

Expand Down Expand Up @@ -371,7 +371,7 @@ export async function resolveDID(did, { atTime, atVersion, confirm, verify } = {
}
else {
if (verify) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

console.error(`unknown type ${operation.type}`);
Expand Down Expand Up @@ -484,7 +484,7 @@ export async function exportDIDs(dids) {

export async function removeDIDs(dids) {
if (!Array.isArray(dids)) {
throw exceptions.INVALID_PARAMETER;
throw new Error(exceptions.INVALID_PARAMETER);
}

for (const did of dids) {
Expand Down Expand Up @@ -533,7 +533,7 @@ async function importUpdateEvent(event) {
export async function importEvent(event) {

if (!event.registry || !event.time || !event.operation) {
throw exceptions.INVALID_PARAMETER;
throw new Error(exceptions.INVALID_PARAMETER);
}

let did;
Expand All @@ -547,11 +547,11 @@ export async function importEvent(event) {
}

if (!did) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}
}
catch {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

const current = await exportDID(did);
Expand All @@ -560,7 +560,7 @@ export async function importEvent(event) {
const ok = await importCreateEvent(event);

if (!ok) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

return true;
Expand Down Expand Up @@ -593,7 +593,7 @@ export async function importEvent(event) {
const ok = await importUpdateEvent(event);

if (!ok) {
throw exceptions.INVALID_OPERATION;
throw new Error(exceptions.INVALID_OPERATION);
}

delete confirmedCache[did];
Expand All @@ -603,7 +603,7 @@ export async function importEvent(event) {

export async function importBatch(batch) {
if (!batch || !Array.isArray(batch) || batch.length < 1) {
throw exceptions.INVALID_PARAMETER;
throw new Error(exceptions.INVALID_PARAMETER);
}

let verified = 0;
Expand Down Expand Up @@ -638,15 +638,15 @@ export async function importBatch(batch) {

export async function getQueue(registry) {
if (!validRegistries.includes(registry)) {
throw exceptions.INVALID_REGISTRY;
throw new Error(exceptions.INVALID_REGISTRY);
}

return db.getQueue(registry);
}

export async function clearQueue(registry, events) {
if (!validRegistries.includes(registry)) {
throw exceptions.INVALID_REGISTRY;
throw new Error(exceptions.INVALID_REGISTRY);
}

return db.clearQueue(registry, events);
Expand Down
Loading

0 comments on commit 4304dea

Please sign in to comment.