Skip to content

Commit

Permalink
fix!: remove CodeError class
Browse files Browse the repository at this point in the history
The `CodeError` class has been replaced with specific error
subclasses with named constructors that use a `.name` property.

BREAKING CHANGE: instead of `CodeError`, use `TimeoutError`, `UnexpectedPeerError`, etc
  • Loading branch information
achingbrain committed Sep 9, 2024
1 parent 5214dec commit 3693f04
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 42 deletions.
37 changes: 0 additions & 37 deletions packages/interface/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,6 @@ export class AbortError extends Error {
}
}

/**
* @deprecated
*/
export class CodeError<T extends Record<string, any> = Record<string, never>> extends Error {
public readonly props: T

constructor (
message: string,
public readonly code: string,
props?: T
) {
super(message)

this.name = props?.name ?? 'CodeError'
this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions
}
}

/**
* @deprecated
*/
export class AggregateCodeError<T extends Record<string, any> = Record<string, never>> extends AggregateError {
public readonly props: T

constructor (
errors: Error[],
message: string,
public readonly code: string,
props?: T
) {
super(errors, message)

this.name = props?.name ?? 'AggregateCodeError'
this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions
}
}

export class UnexpectedPeerError extends Error {
constructor (message = 'Unexpected Peer') {
super(message)
Expand Down
6 changes: 3 additions & 3 deletions packages/libp2p/test/connection-monitor/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */

import { CodeError, start, stop } from '@libp2p/interface'
import { ConnectionClosedError, UnsupportedProtocolError, start, stop } from '@libp2p/interface'
import { defaultLogger } from '@libp2p/logger'
import { expect } from 'aegir/chai'
import delay from 'delay'
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('connection monitor', () => {
const connection = stubInterface<Connection>()
connection.newStream.withArgs('/ipfs/ping/1.0.0').callsFake(async () => {
await delay(10)
throw new CodeError('Unsupported protocol', 'ERR_UNSUPPORTED_PROTOCOL')
throw new UnsupportedProtocolError('Unsupported protocol')
})

components.connectionManager.getConnections.returns([connection])
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('connection monitor', () => {

const connection = stubInterface<Connection>()
connection.newStream.withArgs('/ipfs/ping/1.0.0').callsFake(async (protocols, opts) => {
throw new CodeError('Connection closed', 'ERR_CONNECTION_CLOSED')
throw new ConnectionClosedError('Connection closed')
})

components.connectionManager.getConnections.returns([connection])
Expand Down
4 changes: 2 additions & 2 deletions packages/transport-tcp/src/tcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/

import net from 'net'
import { AbortError, CodeError, serviceCapabilities, transportSymbol } from '@libp2p/interface'
import { AbortError, TimeoutError, serviceCapabilities, transportSymbol } from '@libp2p/interface'
import * as mafmt from '@multiformats/mafmt'
import { CustomProgressEvent } from 'progress-events'
import { CODE_CIRCUIT, CODE_P2P, CODE_UNIX } from './constants.js'
Expand Down Expand Up @@ -139,7 +139,7 @@ export class TCP implements Transport<TCPDialEvents> {
this.log('connection timeout %a', ma)
this.metrics?.dialerEvents.increment({ timeout: true })

const err = new CodeError(`connection timeout after ${Date.now() - start}ms`, 'ERR_CONNECT_TIMEOUT')
const err = new TimeoutError(`connection timeout after ${Date.now() - start}ms`)
// Note: this will result in onError() being called
rawSocket.emit('error', err)
}
Expand Down

0 comments on commit 3693f04

Please sign in to comment.