Skip to content

Commit

Permalink
fix: use named error when parsing multiaddrs (#395)
Browse files Browse the repository at this point in the history
Throw a `ParseError` instead of an `Error`
  • Loading branch information
achingbrain authored Feb 5, 2025
1 parent a379624 commit 5a9d33c
Showing 1 changed file with 10 additions and 5 deletions.
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 @@ export function stringToMultiaddrParts (str: string): MultiaddrParts {

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 @@ export function bytesToMultiaddrParts (bytes: Uint8Array): MultiaddrParts {
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'))
}

// ok, tuple seems good.
Expand Down Expand Up @@ -193,7 +193,7 @@ export function bytesToTuples (buf: Uint8Array): Tuple[] {
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 @@ export function cleanPath (str: string): string {
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}`)
}
}

0 comments on commit 5a9d33c

Please sign in to comment.