-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d43efa7
commit ad3a0cb
Showing
9 changed files
with
126 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { EventEmitter } from "eventemitter3"; | ||
import logger from "./logger"; | ||
|
||
export interface EventsWithError<ErrorType extends string> { | ||
error: (error: PeerError<`${ErrorType}`>) => void; | ||
} | ||
|
||
export class EventEmitterWithError< | ||
ErrorType extends string, | ||
Events extends EventsWithError<ErrorType>, | ||
> extends EventEmitter<Events, never> { | ||
/** | ||
* Emits a typed error message. | ||
* | ||
* @internal | ||
*/ | ||
emitError(type: ErrorType, err: string | Error): void { | ||
logger.error("Error:", err); | ||
|
||
// @ts-ignore | ||
this.emit("error", new PeerError<`${ErrorType}`>(`${type}`, err)); | ||
} | ||
} | ||
/** | ||
* A PeerError is emitted whenever an error occurs. | ||
* It always has a `.type`, which can be used to identify the error. | ||
*/ | ||
export class PeerError<T extends string> extends Error { | ||
/** | ||
* @internal | ||
*/ | ||
constructor(type: T, err: Error | string) { | ||
if (typeof err === "string") { | ||
super(err); | ||
} else { | ||
super(); | ||
Object.assign(this, err); | ||
} | ||
|
||
this.type = type; | ||
} | ||
|
||
public type: T; | ||
} |