Skip to content

Commit 5a9d33c

Browse files
authored
fix: use named error when parsing multiaddrs (#395)
Throw a `ParseError` instead of an `Error`
1 parent a379624 commit 5a9d33c

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

Diff for: src/codec.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function stringToMultiaddrParts (str: string): MultiaddrParts {
4343

4444
p++ // advance addr part
4545
if (p >= parts.length) {
46-
throw ParseError('invalid address: ' + str)
46+
throw new ParseError('invalid address: ' + str)
4747
}
4848

4949
// if it's a path proto, take the rest
@@ -98,7 +98,7 @@ export function bytesToMultiaddrParts (bytes: Uint8Array): MultiaddrParts {
9898
i += (size + n)
9999

100100
if (i > bytes.length) { // did not end _exactly_ at buffer.length
101-
throw ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(bytes, 'base16'))
101+
throw new ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(bytes, 'base16'))
102102
}
103103

104104
// ok, tuple seems good.
@@ -193,7 +193,7 @@ export function bytesToTuples (buf: Uint8Array): Tuple[] {
193193
i += (size + n)
194194

195195
if (i > buf.length) { // did not end _exactly_ at buffer.length
196-
throw ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(buf, 'base16'))
196+
throw new ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(buf, 'base16'))
197197
}
198198

199199
// ok, tuple seems good.
@@ -207,6 +207,11 @@ export function cleanPath (str: string): string {
207207
return '/' + str.trim().split('/').filter((a) => a).join('/')
208208
}
209209

210-
export function ParseError (str: string): Error {
211-
return new Error('Error parsing address: ' + str)
210+
export class ParseError extends Error {
211+
static name = 'ParseError'
212+
name = 'ParseError'
213+
214+
constructor (str: string) {
215+
super(`Error parsing address: ${str}`)
216+
}
212217
}

0 commit comments

Comments
 (0)