Skip to content

Commit

Permalink
chore: replace err-code with CodeError (libp2p#82)
Browse files Browse the repository at this point in the history
deps: remove err-code

replace errcode with CodeError

remove breaking changes

fix linter errors
  • Loading branch information
tabcat authored Feb 28, 2023
1 parent 13e3927 commit cfa6494
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@
"@libp2p/interface-peer-id": "^2.0.0",
"@libp2p/interface-stream-muxer": "^3.0.0",
"@libp2p/interface-transport": "^2.0.0",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.0",
"@libp2p/peer-id": "^2.0.0",
"@multiformats/multiaddr": "^11.0.3",
"@protobuf-ts/runtime": "^2.8.0",
"err-code": "^3.0.1",
"it-length-prefixed": "^8.0.3",
"it-merge": "^2.0.0",
"it-pipe": "^2.0.4",
Expand Down
65 changes: 32 additions & 33 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import errCode from 'err-code'
import { CodeError } from '@libp2p/interfaces/errors'
import type { Direction } from '@libp2p/interface-connection'

export enum codes {
Expand All @@ -14,110 +14,109 @@ export enum codes {
ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS = 'ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS',
}

export class WebRTCTransportError extends Error {
constructor (msg: string) {
super(`WebRTC transport error: ${msg}`)
export class WebRTCTransportError extends CodeError {
constructor (msg: string, code?: string) {
super(`WebRTC transport error: ${msg}`, code ?? '')
this.name = 'WebRTCTransportError'
}
}

export class ConnectionClosedError extends WebRTCTransportError {
constructor (state: RTCPeerConnectionState, msg: string) {
super(`peerconnection moved to state: ${state}: ${msg}`)
super(`peerconnection moved to state: ${state}: ${msg}`, codes.ERR_CONNECTION_CLOSED)
this.name = 'WebRTC/ConnectionClosed'
}
}

export function connectionClosedError (state: RTCPeerConnectionState, msg: string): Error {
return errCode(new ConnectionClosedError(state, msg), codes.ERR_CONNECTION_CLOSED)
export function connectionClosedError (state: RTCPeerConnectionState, msg: string): ConnectionClosedError {
return new ConnectionClosedError(state, msg)
}

export class DataChannelError extends WebRTCTransportError {
constructor (streamLabel: string, msg: string) {
super(`[stream: ${streamLabel}] data channel error: ${msg}`)
super(`[stream: ${streamLabel}] data channel error: ${msg}`, codes.ERR_DATA_CHANNEL)
this.name = 'WebRTC/DataChannelError'
}
}

export function dataChannelError (streamLabel: string, msg: string): Error {
return errCode(new DataChannelError(streamLabel, msg), codes.ERR_DATA_CHANNEL)
export function dataChannelError (streamLabel: string, msg: string): DataChannelError {
return new DataChannelError(streamLabel, msg)
}

export class InappropriateMultiaddrError extends WebRTCTransportError {
constructor (msg: string) {
super(`There was a problem with the Multiaddr which was passed in: ${msg}`)
super(`There was a problem with the Multiaddr which was passed in: ${msg}`, codes.ERR_INVALID_MULTIADDR)
this.name = 'WebRTC/InappropriateMultiaddrError'
}
}

export function inappropriateMultiaddr (msg: string): Error {
return errCode(new InappropriateMultiaddrError(msg), codes.ERR_INVALID_MULTIADDR)
export function inappropriateMultiaddr (msg: string): InappropriateMultiaddrError {
return new InappropriateMultiaddrError(msg)
}

export class InvalidArgumentError extends WebRTCTransportError {
constructor (msg: string) {
super(`There was a problem with a provided argument: ${msg}`)
super(`There was a problem with a provided argument: ${msg}`, codes.ERR_INVALID_PARAMETERS)
this.name = 'WebRTC/InvalidArgumentError'
}
}

export function invalidArgument (msg: string): Error {
return errCode(new InvalidArgumentError(msg), codes.ERR_INVALID_PARAMETERS)
export function invalidArgument (msg: string): InvalidArgumentError {
return new InvalidArgumentError(msg)
}

export class InvalidFingerprintError extends WebRTCTransportError {
constructor (fingerprint: string, source: string) {
super(`Invalid fingerprint "${fingerprint}" within ${source}`)
super(`Invalid fingerprint "${fingerprint}" within ${source}`, codes.ERR_INVALID_FINGERPRINT)
this.name = 'WebRTC/InvalidFingerprintError'
}
}

export function invalidFingerprint (fingerprint: string, source: string): Error {
return errCode(new InvalidFingerprintError(fingerprint, source), codes.ERR_INVALID_FINGERPRINT)
export function invalidFingerprint (fingerprint: string, source: string): InvalidFingerprintError {
return new InvalidFingerprintError(fingerprint, source)
}

export class OperationAbortedError extends WebRTCTransportError {
constructor (context: string, abortReason: string) {
super(`Signalled to abort because (${abortReason}}) ${context}`)
super(`Signalled to abort because (${abortReason}}) ${context}`, codes.ERR_ALREADY_ABORTED)
this.name = 'WebRTC/OperationAbortedError'
}
}

export function operationAborted (context: string, reason: string): Error {
return errCode(new OperationAbortedError(context, reason), codes.ERR_ALREADY_ABORTED)
export function operationAborted (context: string, reason: string): OperationAbortedError {
return new OperationAbortedError(context, reason)
}

export class OverStreamLimitError extends WebRTCTransportError {
constructor (msg: string) {
super(msg)
const code = msg.startsWith('inbound') ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS
super(msg, code)
this.name = 'WebRTC/OverStreamLimitError'
}
}

export function overStreamLimit (dir: Direction, proto: string): Error {
const code = dir === 'inbound' ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS
return errCode(new OverStreamLimitError(`${dir} stream limit reached for protocol - ${proto}`), code)
export function overStreamLimit (dir: Direction, proto: string): OverStreamLimitError {
return new OverStreamLimitError(`${dir} stream limit reached for protocol - ${proto}`)
}

export class UnimplementedError extends WebRTCTransportError {
constructor (methodName: string) {
super(`A method (${methodName}) was called though it has been intentionally left unimplemented.`)
super(`A method (${methodName}) was called though it has been intentionally left unimplemented.`, codes.ERR_NOT_IMPLEMENTED)
this.name = 'WebRTC/UnimplementedError'
}
}

export function unimplemented (methodName: string): Error {
return errCode(new UnimplementedError(methodName), codes.ERR_NOT_IMPLEMENTED)
export function unimplemented (methodName: string): UnimplementedError {
return new UnimplementedError(methodName)
}

export class UnsupportedHashAlgorithmError extends WebRTCTransportError {
constructor (algo: string) {
const msg = `unsupported hash algorithm: ${algo}`
super(msg)
super(`unsupported hash algorithm: ${algo}`, codes.ERR_HASH_NOT_SUPPORTED)
this.name = 'WebRTC/UnsupportedHashAlgorithmError'
}
}

export function unsupportedHashAlgorithm (algorithm: string): Error {
return errCode(new UnsupportedHashAlgorithmError(algorithm), codes.ERR_HASH_NOT_SUPPORTED)
export function unsupportedHashAlgorithm (algorithm: string): UnsupportedHashAlgorithmError {
return new UnsupportedHashAlgorithmError(algorithm)
}

0 comments on commit cfa6494

Please sign in to comment.