Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
chore!: replace errcode with CodeError
Browse files Browse the repository at this point in the history
  • Loading branch information
tabcat committed Feb 16, 2023
1 parent cb1489b commit c340711
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 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) {
return errCode(new ConnectionClosedError(state, msg), codes.ERR_CONNECTION_CLOSED)
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) {
return errCode(new DataChannelError(streamLabel, msg), codes.ERR_DATA_CHANNEL)
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) {
return errCode(new InappropriateMultiaddrError(msg), codes.ERR_INVALID_MULTIADDR)
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) {
return errCode(new InvalidArgumentError(msg), codes.ERR_INVALID_PARAMETERS)
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) {
return errCode(new InvalidFingerprintError(fingerprint, source), codes.ERR_INVALID_FINGERPRINT)
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) {
return errCode(new OperationAbortedError(context, reason), codes.ERR_ALREADY_ABORTED)
return new OperationAbortedError(context, reason)
}

export class OverStreamLimitError extends WebRTCTransportError {
constructor (msg: string) {
super(msg)
constructor (dir: Direction, proto: string) {
const code = dir === 'inbound' ? codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS : codes.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS
super(`${dir} stream limit reached for protocol - ${proto}`, code)
this.name = 'WebRTC/OverStreamLimitError'
}
}

export function overStreamLimit (dir: Direction, proto: string) {
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)
return new OverStreamLimitError(dir, 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) {
return errCode(new UnimplementedError(methodName), codes.ERR_NOT_IMPLEMENTED)
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) {
return errCode(new UnsupportedHashAlgorithmError(algorithm), codes.ERR_HASH_NOT_SUPPORTED)
return new UnsupportedHashAlgorithmError(algorithm)
}

0 comments on commit c340711

Please sign in to comment.