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

chore: use NotFoundError utility in more places #965

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
5 changes: 2 additions & 3 deletions src/core-ownership.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import pDefer from 'p-defer'
import { NAMESPACES } from './constants.js'
import { TypedEmitter } from 'tiny-typed-emitter'
import { omit } from './lib/omit.js'
import { NotFoundError } from './errors.js'
/**
* @import {
* CoreOwnershipWithSignatures,
Expand Down Expand Up @@ -90,9 +91,7 @@ export class CoreOwnership extends TypedEmitter {
const result = (await this.#dataType[kSelect]())
.where(or.apply(null, expressions))
.get()
if (!result) {
throw new Error('NotFound')
}
if (!result) throw new NotFoundError()
return result.docId
}

Expand Down
7 changes: 4 additions & 3 deletions src/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import pDefer from 'p-defer'
import { discoveryKey } from 'hypercore-crypto'
import { NAMESPACE_SCHEMAS } from '../constants.js'
import { createMap } from '../utils.js'
import { NotFoundError } from '../errors.js'
/** @import { MapeoDoc } from '@comapeo/schema' */

/**
Expand Down Expand Up @@ -182,7 +183,7 @@ export class DataStore extends TypedEmitter {
const coreRecord = this.#coreManager.getCoreByDiscoveryKey(coreDiscoveryKey)
if (!coreRecord) throw new Error('Invalid versionId')
const block = await coreRecord.core.get(index, { wait: false })
if (!block) throw new Error('Not Found')
if (!block) throw new NotFoundError('Not Found')
return decode(block, { coreDiscoveryKey, index })
}

Expand All @@ -202,9 +203,9 @@ export class DataStore extends TypedEmitter {
async readRaw(versionId) {
const { coreDiscoveryKey, index } = parseVersionId(versionId)
const coreRecord = this.#coreManager.getCoreByDiscoveryKey(coreDiscoveryKey)
if (!coreRecord) throw new Error('core not found')
if (!coreRecord) throw new NotFoundError('core not found')
const block = await coreRecord.core.get(index, { wait: false })
if (!block) throw new Error('Not Found')
if (!block) throw new NotFoundError()
return block
}

Expand Down
4 changes: 2 additions & 2 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class NotFoundError extends Error {
constructor() {
super('Not found')
constructor(message = 'Not found') {
super(message)
}
}
5 changes: 3 additions & 2 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
kRequestFullStop,
kRescindFullStopRequest,
} from './sync/sync-api.js'
import { NotFoundError } from './errors.js'
/** @import { ProjectSettingsValue as ProjectValue } from '@comapeo/schema' */
/** @import NoiseSecretStream from '@hyperswarm/secret-stream' */
/** @import { SetNonNullable } from 'type-fest' */
Expand Down Expand Up @@ -456,7 +457,7 @@ export class MapeoManager extends TypedEmitter {
.get()

if (!projectKeysTableResult) {
throw new Error(`NotFound: project ID ${projectPublicId} not found`)
throw new NotFoundError(`Project ID ${projectPublicId} not found`)
}

const { projectId } = projectKeysTableResult
Expand Down Expand Up @@ -896,7 +897,7 @@ export class MapeoManager extends TypedEmitter {
.get()

if (!row) {
throw new Error(`NotFound: project ID ${projectPublicId} not found`)
throw new NotFoundError(`Project ID ${projectPublicId} not found`)
}

const { keysCipher, projectId, projectInfo } = row
Expand Down
9 changes: 5 additions & 4 deletions src/mapeo-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { Logger } from './logger.js'
import { IconApi } from './icon-api.js'
import { readConfig } from './config-import.js'
import TranslationApi from './translation-api.js'
import { NotFoundError } from './errors.js'
/** @import { ProjectSettingsValue } from '@comapeo/schema' */
/** @import { CoreStorage, KeyPair, Namespace, ReplicationStream } from './types.js' */

Expand Down Expand Up @@ -659,7 +660,7 @@ export class MapeoProject extends TypedEmitter {
const coreId = this.#coreManager
.getCoreByDiscoveryKey(coreDiscoveryKey)
?.key.toString('hex')
if (!coreId) throw new Error('NotFound')
if (!coreId) throw new NotFoundError()
return this.#coreOwnership.getOwner(coreId)
}

Expand Down Expand Up @@ -879,7 +880,7 @@ export class MapeoProject extends TypedEmitter {
const fieldRefs = fieldNames.map((fieldName) => {
const fieldRef = fieldNameToRef.get(fieldName)
if (!fieldRef) {
throw new Error(
throw new NotFoundError(
`field ${fieldName} not found (referenced by preset ${value.name})})`
)
}
Expand All @@ -891,7 +892,7 @@ export class MapeoProject extends TypedEmitter {
}
const iconRef = iconNameToRef.get(iconName)
if (!iconRef) {
throw new Error(
throw new NotFoundError(
`icon ${iconName} not found (referenced by preset ${value.name})`
)
}
Expand Down Expand Up @@ -938,7 +939,7 @@ export class MapeoProject extends TypedEmitter {
})
)
} else {
throw new Error(
throw new NotFoundError(
`docRef for ${value.docRefType} with name ${name} not found`
)
}
Expand Down
17 changes: 17 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test, { describe } from 'node:test'
import assert from 'node:assert/strict'
import { NotFoundError } from '../src/errors.js'

describe('NotFoundError', () => {
test('subclasses Error', () => {
assert(new NotFoundError() instanceof Error)
})

test('with no error message', () => {
assert.equal(new NotFoundError().message, 'Not found')
})

test('with custom error message', () => {
assert.equal(new NotFoundError('foo').message, 'foo')
})
})
Loading