throw errors instead of console logs

This commit is contained in:
Henry Jameson 2025-03-23 17:23:47 +02:00
commit eaf7efdcff
2 changed files with 33 additions and 36 deletions

View file

@ -226,29 +226,24 @@ export const _mergePrefs = (recent, stale) => {
const totalJournal = _mergeJournal(staleJournal, recentJournal) const totalJournal = _mergeJournal(staleJournal, recentJournal)
totalJournal.forEach(({ path, operation, args }) => { totalJournal.forEach(({ path, operation, args }) => {
if (path.startsWith('_')) { if (path.startsWith('_')) {
console.error(`journal contains entry to edit internal (starts with _) field '${path}', something is incorrect here, ignoring.`) throw new Error(`journal contains entry to edit internal (starts with _) field '${path}', something is incorrect here, ignoring.`)
return
} }
switch (operation) { switch (operation) {
case 'set': case 'set':
if (path.startsWith('collections') || path.startsWith('objectCollections')) { if (path.startsWith('collections') || path.startsWith('objectCollections')) {
console.error('Illegal operation "set" on a collection') throw new Error('Illegal operation "set" on a collection')
return
} }
if (path.split(/\./g).length <= 1) { if (path.split(/\./g).length <= 1) {
console.error(`Calling set on depth <= 1 (path: ${path}) is not allowed`) throw new Error(`Calling set on depth <= 1 (path: ${path}) is not allowed`)
return
} }
set(resultOutput, path, args[0]) set(resultOutput, path, args[0])
break break
case 'unset': case 'unset':
if (path.startsWith('collections') || path.startsWith('objectCollections')) { if (path.startsWith('collections') || path.startsWith('objectCollections')) {
console.error('Illegal operation "unset" on a collection') throw new Error('Illegal operation "unset" on a collection')
return
} }
if (path.split(/\./g).length <= 2) { if (path.split(/\./g).length <= 2) {
console.error(`Calling unset on depth <= 2 (path: ${path}) is not allowed`) throw new Error(`Calling unset on depth <= 2 (path: ${path}) is not allowed`)
return
} }
unset(resultOutput, path) unset(resultOutput, path)
break break
@ -267,7 +262,7 @@ export const _mergePrefs = (recent, stale) => {
break break
} }
default: default:
console.error(`Unknown journal operation: '${operation}', did we forget to run reverse migrations beforehand?`) throw new Error(`Unknown journal operation: '${operation}', did we forget to run reverse migrations beforehand?`)
} }
}) })
return { ...resultOutput, _journal: totalJournal } return { ...resultOutput, _journal: totalJournal }
@ -406,16 +401,16 @@ export const mutations = {
}, },
setPreference (state, { path, value }) { setPreference (state, { path, value }) {
if (path.startsWith('_')) { if (path.startsWith('_')) {
console.error(`Tried to edit internal (starts with _) field '${path}', ignoring.`) throw new Error(`Tried to edit internal (starts with _) field '${path}', ignoring.`)
return
} }
if (path.startsWith('collections') || path.startsWith('objectCollections')) { if (path.startsWith('collections') || path.startsWith('objectCollections')) {
console.error(`Invalid operation 'set' for collection field '${path}', ignoring.`) throw new Error(`Invalid operation 'set' for collection field '${path}', ignoring.`)
return
} }
if (path.split(/\./g).length <= 1) { if (path.split(/\./g).length <= 1) {
console.error(`Calling set on depth <= 1 (path: ${path}) is not allowed`) throw new Error(`Calling set on depth <= 1 (path: ${path}) is not allowed`)
return }
if (path.split(/\./g).length > 3) {
throw new Error(`Calling set on depth > 3 (path: ${path}) is not allowed`)
} }
set(state.prefsStorage, path, value) set(state.prefsStorage, path, value)
state.prefsStorage._journal = [ state.prefsStorage._journal = [
@ -426,16 +421,16 @@ export const mutations = {
}, },
unsetPreference (state, { path, value }) { unsetPreference (state, { path, value }) {
if (path.startsWith('_')) { if (path.startsWith('_')) {
console.error(`Tried to edit internal (starts with _) field '${path}', ignoring.`) throw new Error(`Tried to edit internal (starts with _) field '${path}', ignoring.`)
return
} }
if (path.startsWith('collections') || path.startsWith('objectCollections')) { if (path.startsWith('collections') || path.startsWith('objectCollections')) {
console.error(`Invalid operation 'unset' for collection field '${path}', ignoring.`) throw new Error(`Invalid operation 'unset' for collection field '${path}', ignoring.`)
return
} }
if (path.split(/\./g).length <= 2) { if (path.split(/\./g).length <= 2) {
console.error(`Calling unset on depth <= 2 (path: ${path}) is not allowed`) throw new Error(`Calling unset on depth <= 2 (path: ${path}) is not allowed`)
return }
if (path.split(/\./g).length > 3) {
throw new Error(`Calling unset on depth > 3 (path: ${path}) is not allowed`)
} }
unset(state.prefsStorage, path, value) unset(state.prefsStorage, path, value)
state.prefsStorage._journal = [ state.prefsStorage._journal = [
@ -446,8 +441,7 @@ export const mutations = {
}, },
addCollectionPreference (state, { path, value }) { addCollectionPreference (state, { path, value }) {
if (path.startsWith('_')) { if (path.startsWith('_')) {
console.error(`tried to edit internal (starts with _) field '${path}', ignoring.`) throw new Error(`tried to edit internal (starts with _) field '${path}'`)
return
} }
if (path.startsWith('collections')) { if (path.startsWith('collections')) {
const collection = new Set(get(state.prefsStorage, path)) const collection = new Set(get(state.prefsStorage, path))
@ -456,8 +450,7 @@ export const mutations = {
} else if (path.startsWith('objectCollections')) { } else if (path.startsWith('objectCollections')) {
const { _key } = value const { _key } = value
if (!_key && typeof _key !== 'string') { if (!_key && typeof _key !== 'string') {
console.error('Object for storage is missing _key field! ignoring') throw new Error('Object for storage is missing _key field!')
return
} }
const collection = new Set(get(state.prefsStorage, path + '.index')) const collection = new Set(get(state.prefsStorage, path + '.index'))
collection.add(_key) collection.add(_key)
@ -472,8 +465,7 @@ export const mutations = {
}, },
removeCollectionPreference (state, { path, value }) { removeCollectionPreference (state, { path, value }) {
if (path.startsWith('_')) { if (path.startsWith('_')) {
console.error(`tried to edit internal (starts with _) field '${path}', ignoring.`) throw new Error(`tried to edit internal (starts with _) field '${path}', ignoring.`)
return
} }
const collection = new Set(get(state.prefsStorage, path)) const collection = new Set(get(state.prefsStorage, path))
collection.delete(value) collection.delete(value)
@ -486,8 +478,7 @@ export const mutations = {
}, },
reorderCollectionPreference (state, { path, value, movement }) { reorderCollectionPreference (state, { path, value, movement }) {
if (path.startsWith('_')) { if (path.startsWith('_')) {
console.error(`tried to edit internal (starts with _) field '${path}', ignoring.`) throw new Error(`tried to edit internal (starts with _) field '${path}', ignoring.`)
return
} }
const collection = get(state.prefsStorage, path) const collection = get(state.prefsStorage, path)
const newCollection = _moveItemInArray(collection, value, movement) const newCollection = _moveItemInArray(collection, value, movement)

View file

@ -173,11 +173,17 @@ describe('The serverSideStorage module', () => {
it('should not allow unsetting depth <= 2', () => { it('should not allow unsetting depth <= 2', () => {
const state = cloneDeep(defaultState) const state = cloneDeep(defaultState)
setPreference(state, { path: 'simple.object.foo', value: 1 }) setPreference(state, { path: 'simple.object.foo', value: 1 })
unsetPreference(state, { path: 'simple.object' }) expect(() => unsetPreference(state, { path: 'simple' })).to.throw()
unsetPreference(state, { path: 'simple' }) expect(() => unsetPreference(state, { path: 'simple.object' })).to.throw()
updateCache(state, { username: 'test' }) })
expect(state.prefsStorage.simple.object).to.have.property('foo')
expect(state.prefsStorage._journal.length).to.eql(1) it('should not allow (un)setting depth > 3', () => {
const state = cloneDeep(defaultState)
setPreference(state, { path: 'simple.object', value: {} })
expect(() => setPreference(state, { path: 'simple.object.lv3', value: 1 })).to.not.throw()
expect(() => setPreference(state, { path: 'simple.object.lv3.lv4', value: 1})).to.throw()
expect(() => unsetPreference(state, { path: 'simple.object.lv3', value: 1 })).to.not.throw()
expect(() => unsetPreference(state, { path: 'simple.object.lv3.lv4', value: 1})).to.throw()
}) })
}) })
}) })