Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 0dfb7d4

Browse files
author
Alan Shaw
committed
refactor: standard errors and remove first/last utils
1 parent b84467e commit 0dfb7d4

File tree

5 files changed

+47
-46
lines changed

5 files changed

+47
-46
lines changed

src/core/components-ipfsx/add/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const importer = require('ipfs-unixfs-importer')
44
const normaliseAddInput = require('ipfs-utils/src/files/normalise-input')
55
const { parseChunkerString } = require('./utils')
66
const pipe = require('it-pipe')
7-
const { withFirstAndLast } = require('../../utils')
87

98
module.exports = ({ ipld, dag, gcLock, preload, pin, constructorOptions }) => {
10-
return withFirstAndLast(async function * add (source, options) {
9+
return async function * add (source, options) {
1110
options = options || {}
1211

1312
const opts = {
@@ -53,7 +52,7 @@ module.exports = ({ ipld, dag, gcLock, preload, pin, constructorOptions }) => {
5352
} finally {
5453
releaseLock()
5554
}
56-
})
55+
}
5756
}
5857

5958
function transformFile (dag, opts) {

src/core/components-ipfsx/init.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const UnixFs = require('ipfs-unixfs')
1515
const multicodec = require('multicodec')
1616
const multiaddr = require('multiaddr')
1717
const {
18-
ERR_ALREADY_INITIALIZING,
19-
ERR_ALREADY_INITIALIZED,
20-
ERR_NOT_STARTED
18+
AlreadyInitializingError,
19+
AlreadyInitializedError,
20+
NotStartedError
2121
} = require('../../errors')
2222
const BlockService = require('ipfs-block-service')
2323
const Ipld = require('ipld')
@@ -35,7 +35,7 @@ module.exports = ({
3535
print,
3636
constructorOptions
3737
}) => async function init (options) {
38-
const { cancel } = apiManager.update({ init: ERR_ALREADY_INITIALIZING })
38+
const { cancel } = apiManager.update({ init: () => { throw new AlreadyInitializingError() } })
3939

4040
try {
4141
options = mergeOptions({}, options, constructorOptions.init)
@@ -137,7 +137,7 @@ module.exports = ({
137137
repo
138138
})
139139

140-
apiManager.update(api, ERR_NOT_STARTED)
140+
apiManager.update(api, () => { throw new NotStartedError() })
141141
} catch (err) {
142142
cancel()
143143
throw err
@@ -294,7 +294,7 @@ function createApi ({
294294

295295
const api = {
296296
add,
297-
init: ERR_ALREADY_INITIALIZED,
297+
init: () => { throw new AlreadyInitializedError() },
298298
start
299299
}
300300

src/core/components-ipfsx/stop.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const defer = require('p-defer')
44
const Components = require('.')
5-
const { ERR_NOT_STARTED, ERR_ALREADY_INITIALIZED } = require('../../errors')
5+
const { NotStartedError, AlreadyInitializedError } = require('../../errors')
66

77
module.exports = ({
88
apiManager,
@@ -51,7 +51,7 @@ module.exports = ({
5151
repo
5252
})
5353

54-
apiManager.update(api, ERR_NOT_STARTED)
54+
apiManager.update(api, () => { throw new NotStartedError() })
5555
} catch (err) {
5656
cancel()
5757
stopPromise.reject(err)
@@ -98,7 +98,7 @@ function createApi ({
9898

9999
const api = {
100100
add,
101-
init: ERR_ALREADY_INITIALIZED,
101+
init: () => { throw new AlreadyInitializedError() },
102102
start,
103103
stop: () => apiManager.api
104104
}

src/core/errors.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
1-
const errCode = require('err-code')
2-
3-
exports.ERR_NOT_INITIALIZED = () => {
4-
throw errCode(new Error('not initialized'), 'ERR_NOT_INITIALIZED')
1+
class NotInitializedError extends Error {
2+
constructor (message = 'not initialized') {
3+
super(message)
4+
this.name = 'NotInitializedError'
5+
this.code = NotInitializedError.code
6+
}
57
}
68

7-
exports.ERR_ALREADY_INITIALIZING = () => {
8-
const msg = 'cannot initialize an initializing node'
9-
throw errCode(new Error(msg), 'ERR_ALREADY_INITIALIZING')
9+
NotInitializedError.code = 'ERR_NOT_INITIALIZED'
10+
exports.NotInitializedError = NotInitializedError
11+
12+
class AlreadyInitializingError extends Error {
13+
constructor (message = 'cannot initialize an initializing node') {
14+
super(message)
15+
this.name = 'AlreadyInitializingError'
16+
this.code = AlreadyInitializedError.code
17+
}
1018
}
1119

12-
exports.ERR_ALREADY_INITIALIZED = () => {
13-
const msg = 'cannot re-initialize an initialized node'
14-
throw errCode(new Error(msg), 'ERR_ALREADY_INITIALIZED')
20+
AlreadyInitializingError.code = 'ERR_ALREADY_INITIALIZING'
21+
exports.AlreadyInitializingError = AlreadyInitializingError
22+
23+
class AlreadyInitializedError extends Error {
24+
constructor (message = 'cannot re-initialize an initialized node') {
25+
super(message)
26+
this.name = 'AlreadyInitializedError'
27+
this.code = AlreadyInitializedError.code
28+
}
1529
}
1630

17-
exports.ERR_NOT_STARTED = () => {
18-
throw errCode(new Error('not started'), 'ERR_NOT_STARTED')
31+
AlreadyInitializedError.code = 'ERR_ALREADY_INITIALIZED'
32+
exports.AlreadyInitializedError = AlreadyInitializedError
33+
34+
class NotStartedError extends Error {
35+
constructor (message = 'not started') {
36+
super(message)
37+
this.name = 'NotStartedError'
38+
this.code = NotStartedError.code
39+
}
1940
}
41+
42+
NotStartedError.code = 'ERR_NOT_STARTED'
43+
exports.NotStartedError = NotStartedError

src/core/utils.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,3 @@ const resolvePath = async function (objectAPI, ipfsPaths) {
127127
exports.normalizePath = normalizePath
128128
exports.parseIpfsPath = parseIpfsPath
129129
exports.resolvePath = resolvePath
130-
131-
exports.withFirstAndLast = fn => {
132-
return (...args) => {
133-
const it = fn(...args)
134-
return {
135-
[Symbol.asyncIterator] () {
136-
return it[Symbol.asyncIterator]()
137-
},
138-
async first () {
139-
const { value } = await it.next()
140-
return value
141-
},
142-
async last () {
143-
let last
144-
for await (const value of it) {
145-
last = value
146-
}
147-
return last
148-
}
149-
}
150-
}
151-
}

0 commit comments

Comments
 (0)