From db28d7c9309f2eb5bf2fef35ec99f78a9891ef4c Mon Sep 17 00:00:00 2001 From: tabcat Date: Fri, 13 Jan 2023 16:11:22 -0600 Subject: [PATCH] chore: replace err-code with CodeError deps: remove err-code replace errcode with CodeError remove breaking changes fix linter errors --- package.json | 2 +- src/error.ts | 65 ++++++++++++++++++++++++++-------------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 944017a..e6a6f36 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/error.ts b/src/error.ts index d0b5285..360b4e3 100644 --- a/src/error.ts +++ b/src/error.ts @@ -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 { @@ -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) }