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

feat!: use .name property for errors instead of .code #315

Merged
merged 2 commits into from
Aug 2, 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
1 change: 0 additions & 1 deletion packages/blockstore-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
},
"dependencies": {
"@libp2p/logger": "^4.0.6",
"err-code": "^3.0.1",
"interface-blockstore": "^5.0.0",
"interface-store": "^5.0.0",
"it-drain": "^3.0.5",
Expand Down
4 changes: 2 additions & 2 deletions packages/blockstore-core/src/black-hole.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NotFoundError } from 'interface-store'
import { BaseBlockstore } from './base.js'
import * as Errors from './errors.js'
import type { Pair } from 'interface-blockstore'
import type { Await, AwaitIterable } from 'interface-store'
import type { CID } from 'multiformats/cid'
Expand All @@ -10,7 +10,7 @@ export class BlackHoleBlockstore extends BaseBlockstore {
}

get (): Await<Uint8Array> {
throw Errors.notFoundError()
throw new NotFoundError()
}

has (): Await<boolean> {
Expand Down
41 changes: 0 additions & 41 deletions packages/blockstore-core/src/errors.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/blockstore-core/src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NotFoundError } from 'interface-store'
import { BaseBlockstore } from './base.js'
import { Errors } from './index.js'
import type { Blockstore, Pair } from 'interface-blockstore'
import type { AbortOptions, Await, AwaitIterable } from 'interface-store'
import type { CID } from 'multiformats/cid'
Expand Down Expand Up @@ -34,7 +34,7 @@
}

if (this.child == null) {
throw Errors.notFoundError()
throw new NotFoundError()

Check warning on line 37 in packages/blockstore-core/src/identity.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/identity.ts#L37

Added line #L37 was not covered by tests
}

return this.child.get(key)
Expand Down
6 changes: 0 additions & 6 deletions packages/blockstore-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,7 @@
* ```
*/

import * as ErrorsImport from './errors.js'

export { BaseBlockstore } from './base.js'
export { MemoryBlockstore } from './memory.js'
export { BlackHoleBlockstore } from './black-hole.js'
export { TieredBlockstore } from './tiered.js'

export const Errors = {
...ErrorsImport
}
4 changes: 2 additions & 2 deletions packages/blockstore-core/src/memory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NotFoundError } from 'interface-store'
import { base32 } from 'multiformats/bases/base32'
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
import * as Digest from 'multiformats/hashes/digest'
import { BaseBlockstore } from './base.js'
import * as Errors from './errors.js'
import type { Pair } from 'interface-blockstore'
import type { Await, AwaitIterable } from 'interface-store'

Expand All @@ -26,7 +26,7 @@ export class MemoryBlockstore extends BaseBlockstore {
const buf = this.data.get(base32.encode(key.multihash.bytes))

if (buf == null) {
throw Errors.notFoundError()
throw new NotFoundError()
}

return buf
Expand Down
8 changes: 4 additions & 4 deletions packages/blockstore-core/src/tiered.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { logger } from '@libp2p/logger'
import { DeleteFailedError, NotFoundError, PutFailedError } from 'interface-store'
import drain from 'it-drain'
import filter from 'it-filter'
import merge from 'it-merge'
import { pushable } from 'it-pushable'
import { BaseBlockstore } from './base.js'
import * as Errors from './errors.js'
import type { Blockstore, Pair } from 'interface-blockstore'
import type { AbortOptions, AwaitIterable } from 'interface-store'
import type { CID } from 'multiformats/cid'
Expand All @@ -31,7 +31,7 @@
await Promise.all(this.stores.map(async store => { await store.put(key, value, options) }))
return key
} catch (err: any) {
throw Errors.putFailedError(err)
throw new PutFailedError(String(err))

Check warning on line 34 in packages/blockstore-core/src/tiered.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/tiered.ts#L34

Added line #L34 was not covered by tests
}
}

Expand All @@ -44,7 +44,7 @@
log.error(err)
}
}
throw Errors.notFoundError()
throw new NotFoundError()
}

async has (key: CID, options?: AbortOptions): Promise<boolean> {
Expand All @@ -61,7 +61,7 @@
try {
await Promise.all(this.stores.map(async store => { await store.delete(key, options) }))
} catch (err: any) {
throw Errors.deleteFailedError(err)
throw new DeleteFailedError(String(err))

Check warning on line 64 in packages/blockstore-core/src/tiered.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-core/src/tiered.ts#L64

Added line #L64 was not covered by tests
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/blockstore-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"dep-check": "aegir dep-check"
},
"dependencies": {
"blockstore-core": "^4.0.0",
"fast-write-atomic": "^0.2.1",
"interface-blockstore": "^5.0.0",
"interface-store": "^5.0.0",
Expand Down
18 changes: 8 additions & 10 deletions packages/blockstore-fs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { promisify } from 'node:util'
import {
Errors
} from 'blockstore-core'
// @ts-expect-error no types
import fwa from 'fast-write-atomic'
import { OpenFailedError, type AwaitIterable, PutFailedError, NotFoundError, DeleteFailedError } from 'interface-store'
import glob from 'it-glob'
import map from 'it-map'
import parallelBatch from 'it-parallel-batch'
import { NextToLast, type ShardingStrategy } from './sharding.js'
import { NextToLast } from './sharding.js'
import type { ShardingStrategy } from './sharding.js'
import type { Blockstore, Pair } from 'interface-blockstore'
import type { AwaitIterable } from 'interface-store'
import type { CID } from 'multiformats/cid'

const writeAtomic = promisify(fwa)
Expand Down Expand Up @@ -122,15 +120,15 @@ export class FsBlockstore implements Blockstore {
await fs.access(this.path, fs.constants.F_OK | fs.constants.W_OK)

if (this.errorIfExists) {
throw Errors.openFailedError(new Error(`Blockstore directory: ${this.path} already exists`))
throw new OpenFailedError(`Blockstore directory: ${this.path} already exists`)
}
} catch (err: any) {
if (err.code === 'ENOENT') {
if (this.createIfMissing) {
await fs.mkdir(this.path, { recursive: true })
return
} else {
throw Errors.openFailedError(new Error(`Blockstore directory: ${this.path} does not exist`))
throw new OpenFailedError(`Blockstore directory: ${this.path} does not exist`)
}
}

Expand All @@ -156,7 +154,7 @@ export class FsBlockstore implements Blockstore {

return key
} catch (err: any) {
throw Errors.putFailedError(err)
throw new PutFailedError(String(err))
}
}

Expand All @@ -179,7 +177,7 @@ export class FsBlockstore implements Blockstore {
try {
return await fs.readFile(path.join(this.path, dir, file))
} catch (err: any) {
throw Errors.notFoundError(err)
throw new NotFoundError(String(err))
}
}

Expand Down Expand Up @@ -207,7 +205,7 @@ export class FsBlockstore implements Blockstore {
return
}

throw Errors.deleteFailedError(err)
throw new DeleteFailedError(String(err))
}
}

Expand Down
9 changes: 4 additions & 5 deletions packages/blockstore-fs/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-env mocha */
import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
Expand Down Expand Up @@ -44,7 +43,7 @@ describe('FsBlockstore', () => {
const dir = path.join(os.tmpdir(), `test-${Math.random()}`)
const store = new FsBlockstore(dir, { createIfMissing: false })
await expect(store.open()).to.eventually.be.rejected
.with.property('code', 'ERR_OPEN_FAILED')
.with.property('name', 'OpenFailedError')
})

it('errorIfExists: true - folder exists', async () => {
Expand All @@ -54,7 +53,7 @@ describe('FsBlockstore', () => {
})
const store = new FsBlockstore(dir, { errorIfExists: true })
await expect(store.open()).to.eventually.be.rejected
.with.property('code', 'ERR_OPEN_FAILED')
.with.property('name', 'OpenFailedError')
})
})

Expand All @@ -68,7 +67,7 @@ describe('FsBlockstore', () => {
await fs.delete(key)

await expect(fs.get(key)).to.eventually.be.rejected
.with.property('code', 'ERR_NOT_FOUND')
.with.property('name', 'NotFoundError')
})

it('deleting non-existent files', async () => {
Expand All @@ -81,7 +80,7 @@ describe('FsBlockstore', () => {
await fs.delete(key)

await expect(fs.get(key)).to.eventually.be.rejected
.with.property('code', 'ERR_NOT_FOUND')
.with.property('name', 'NotFoundError')
})

describe('interface-blockstore (flat directory)', () => {
Expand Down
21 changes: 10 additions & 11 deletions packages/blockstore-idb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
* ```
*/

import {
BaseBlockstore,
Errors
} from 'blockstore-core'
import { openDB, type IDBPDatabase, deleteDB } from 'idb'
import { BaseBlockstore } from 'blockstore-core'
import { openDB, deleteDB } from 'idb'
import { OpenFailedError, PutFailedError, NotFoundError } from 'interface-store'
import { base32upper } from 'multiformats/bases/base32'
import { CID } from 'multiformats/cid'
import * as raw from 'multiformats/codecs/raw'
import * as Digest from 'multiformats/hashes/digest'
import type { IDBPDatabase } from 'idb'
import type { Pair } from 'interface-blockstore'
import type { AbortOptions, AwaitIterable } from 'interface-store'
import type { MultibaseCodec } from 'multiformats/bases/interface'
Expand Down Expand Up @@ -77,7 +76,7 @@ export class IDBBlockstore extends BaseBlockstore {
}
})
} catch (err: any) {
throw Errors.openFailedError(err)
throw new OpenFailedError(String(err))
}
}

Expand All @@ -95,7 +94,7 @@ export class IDBBlockstore extends BaseBlockstore {

return key
} catch (err: any) {
throw Errors.putFailedError(err)
throw new PutFailedError(String(err))
}
}

Expand All @@ -109,11 +108,11 @@ export class IDBBlockstore extends BaseBlockstore {
try {
val = await this.db.get(this.location, this.#encode(key))
} catch (err: any) {
throw Errors.putFailedError(err)
throw new PutFailedError(String(err))
}

if (val === undefined) {
throw Errors.notFoundError()
throw new NotFoundError()
}

return val
Expand All @@ -127,7 +126,7 @@ export class IDBBlockstore extends BaseBlockstore {
try {
await this.db.delete(this.location, this.#encode(key))
} catch (err: any) {
throw Errors.putFailedError(err)
throw new PutFailedError(String(err))
}
}

Expand All @@ -139,7 +138,7 @@ export class IDBBlockstore extends BaseBlockstore {
try {
return Boolean(await this.db.getKey(this.location, this.#encode(key)))
} catch (err: any) {
throw Errors.putFailedError(err)
throw new PutFailedError(String(err))
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/blockstore-level/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* ```
*/

import { BaseBlockstore, Errors } from 'blockstore-core'
import { BaseBlockstore } from 'blockstore-core'
import { DeleteFailedError, GetFailedError, NotFoundError, OpenFailedError, PutFailedError } from 'interface-store'
import { Level } from 'level'
import { base32upper } from 'multiformats/bases/base32'
import { CID } from 'multiformats/cid'
Expand Down Expand Up @@ -73,7 +74,7 @@
try {
await this.db.open(this.opts)
} catch (err: any) {
throw Errors.openFailedError(err)
throw new OpenFailedError(String(err))

Check warning on line 77 in packages/blockstore-level/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-level/src/index.ts#L77

Added line #L77 was not covered by tests
}
}

Expand All @@ -83,7 +84,7 @@

return key
} catch (err: any) {
throw Errors.putFailedError(err)
throw new PutFailedError(String(err))

Check warning on line 87 in packages/blockstore-level/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-level/src/index.ts#L87

Added line #L87 was not covered by tests
}
}

Expand All @@ -93,10 +94,10 @@
data = await this.db.get(this.#encode(key))
} catch (err: any) {
if (err.notFound != null) {
throw Errors.notFoundError(err)
throw new NotFoundError(String(err))
}

throw Errors.getFailedError(err)
throw new GetFailedError(String(err))

Check warning on line 100 in packages/blockstore-level/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-level/src/index.ts#L100

Added line #L100 was not covered by tests
}
return data
}
Expand All @@ -118,7 +119,7 @@
try {
await this.db.del(this.#encode(key))
} catch (err: any) {
throw Errors.deleteFailedError(err)
throw new DeleteFailedError(String(err))

Check warning on line 122 in packages/blockstore-level/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/blockstore-level/src/index.ts#L122

Added line #L122 was not covered by tests
}
}

Expand Down
Loading
Loading