From 4d75552ff180e0c10913225f22a1a839ab5776ab Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Sat, 1 Jun 2024 17:56:29 +0200 Subject: [PATCH] Return null instead of throwing on property errs --- lib/index.js | 15 +++++++++------ lib/msg-v4/tangle.js | 11 +++++++---- lib/msg-v4/validation.js | 12 +++++++----- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/index.js b/lib/index.js index b9439fc..73b8ffa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -131,7 +131,7 @@ class DBTangle extends MsgV4.Tangle { * *Erasables* are msgs that precede `msgsIDs` and can be erased without * losing a validation path toward the root. * @param {Array} msgIDs - * @returns {{ deletables: Set, erasables: Set }} + * @returns {{ deletables: Set, erasables: Set } | null} */ getDeletablesAndErasables(...msgIDs) { // Determine erasables @@ -139,6 +139,7 @@ class DBTangle extends MsgV4.Tangle { const minimum = this.getMinimumAmong(msgIDs) for (const msgID of minimum) { const trail = this.shortestPathToRoot(msgID) + if (!trail) return null for (const id of trail) { erasables.add(id) } @@ -174,6 +175,7 @@ class DBTangle extends MsgV4.Tangle { const trail = new Set() for (const msgID of minSetTight) { const path = this.shortestPathToRoot(msgID) + if (!path) return cb(Error("Couldn't get shortest path to root when slicing dbtangle")) for (const msgID of path) { trail.add(msgID) } @@ -529,16 +531,17 @@ function initDB(peer, config) { rec.msg.metadata.accountTips, (err, sigkeys) => { if (err) return cb(err) - + // TODO: how is sigkeys able to be undefined here? typechecker why are you weird? + if (!sigkeys) return cb(Error("Sigkeys missing somehow")) // Don't accept ghosts to come back, unless they are trail msgs if (!!rec.msg.data && ghosts.read(tangleID).has(rec.id)) { return cb(Error('Refusing a ghost msg to come back')) } - if ( - (err = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID)) - ) { - return cb(Error('Invalid msg', { cause: err })) + + const valErr = MsgV4.validate(rec.msg, tangle, sigkeys, rec.id, tangleID) + if (valErr) { + return cb(Error('Invalid msg', { cause: valErr })) } /** @param {CB} cb */ diff --git a/lib/msg-v4/tangle.js b/lib/msg-v4/tangle.js index af50e60..6faab4f 100644 --- a/lib/msg-v4/tangle.js +++ b/lib/msg-v4/tangle.js @@ -222,11 +222,12 @@ class Tangle { } /** - * @returns {'feed' | 'account' | 'weave'} + * @returns {'feed' | 'account' | 'weave' | null} */ get type() { if (!this.#rootMsg) { - throw new Error(`Tangle "${this.#rootID}" is missing root message`) + console.trace(new Error(`Tangle "${this.#rootID}" is missing root message`)) + return null } if (this.#isFeed()) return 'feed' if (this.#rootMsg.metadata.account === 'self') return 'account' @@ -235,7 +236,8 @@ class Tangle { get root() { if (!this.#rootMsg) { - throw new Error(`Tangle "${this.#rootID}" is missing root message`) + console.trace(new Error(`Tangle "${this.#rootID}" is missing root message`)) + return null } return this.#rootMsg } @@ -256,7 +258,8 @@ class Tangle { if (!prev) break if (prev === lastPrev) { // prettier-ignore - throw new Error(`Tangle "${this.#rootID}" has a cycle or lacking a trail to root`) + console.trace(new Error(`Tangle "${this.#rootID}" has a cycle or lacking a trail to root`)) + return null } else { lastPrev = prev } diff --git a/lib/msg-v4/validation.js b/lib/msg-v4/validation.js index 3187675..5398ba4 100644 --- a/lib/msg-v4/validation.js +++ b/lib/msg-v4/validation.js @@ -107,6 +107,8 @@ function validateSigkeyAndAccount(msg, tangle, sigkeys) { // prettier-ignore return `invalid msg: accountTips "${msg.metadata.accountTips}" should have been null in an account tangle\n` + JSON.stringify(msg) } + } else if (tangle.type === null) { + return "Cannot validate tangle of unknown type" } return undefined } @@ -199,6 +201,8 @@ function validateTangle(msg, tangle, tangleID) { // prettier-ignore return `invalid msg: account "${msg.metadata.account}" should have been feed account "${account}"\n` + JSON.stringify(msg) } + } else if (tangle.type === null) { + return "Unknown tangle type" } let lastPrev = null let minDiff = Infinity @@ -342,11 +346,8 @@ function validate(msg, tangle, sigkeys, msgID, rootID) { if ((err = validateSigkey(msg))) return err if ((err = validateData(msg))) return err - try { - if (tangle.type === 'feed' && isMoot(msg)) return // nothing else to check - } catch (/** @type {any} */ err) { - return err - } + if (tangle.type === 'feed' && isMoot(msg)) return // nothing else to check + if (tangle.type === null) return "Missing tangle type when validating msg" if ((err = validateDataSizeHash(msg))) return err if ((err = validateDomain(msg.metadata.domain))) return err @@ -357,6 +358,7 @@ function validate(msg, tangle, sigkeys, msgID, rootID) { if ((err = validateTangle(msg, tangle, rootID))) return err } if ((err = validateSignature(msg))) return err + return undefined } module.exports = {