Skip to content

fix: use named error when parsing multiaddrs #395

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

Merged
merged 1 commit into from
Feb 5, 2025
Merged
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
15 changes: 10 additions & 5 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

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

// if it's a path proto, take the rest
Expand Down Expand Up @@ -98,7 +98,7 @@
i += (size + n)

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

Check warning on line 101 in src/codec.ts

View check run for this annotation

Codecov / codecov/patch

src/codec.ts#L101

Added line #L101 was not covered by tests
}

// ok, tuple seems good.
Expand Down Expand Up @@ -193,7 +193,7 @@
i += (size + n)

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

// ok, tuple seems good.
Expand All @@ -207,6 +207,11 @@
return '/' + str.trim().split('/').filter((a) => a).join('/')
}

export function ParseError (str: string): Error {
return new Error('Error parsing address: ' + str)
export class ParseError extends Error {
static name = 'ParseError'
name = 'ParseError'

constructor (str: string) {
super(`Error parsing address: ${str}`)
}
}
Loading