Skip to content

Commit 6638f5c

Browse files
committed
feat: add custom error type SseError
1 parent 9725e80 commit 6638f5c

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

docs/api.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
### Exports
66

7-
* [Session](#session)
8-
* [createSession](#createsession%3A-(constructorparameters<typeof-session>)-%3D>-promise<session>)
9-
* [Channel](#channel)
10-
* [createChannel](#createchannel%3A-(...args%3A-constructorparameters<typeof-channel>)-%3D>-channel)
11-
* [EventBuffer](#eventbuffer)
12-
* [createEventBuffer](#createeventbuffer-args-constructorparameterstypeof-eventbuffer--eventbuffer)
7+
* [`Session`](#session)
8+
* [`createSession`](#createsession%3A-(constructorparameters<typeof-session>)-%3D>-promise<session>)
9+
* [`Channel`](#channel)
10+
* [`createChannel`](#createchannel%3A-(...args%3A-constructorparameters<typeof-channel>)-%3D>-channel)
11+
* [`EventBuffer`](#eventbuffer)
12+
* [`createEventBuffer`](#createeventbuffer-args-constructorparameterstypeof-eventbuffer--eventbuffer)
13+
* [`SseError`](#sseerror)
1314

1415
## Documentation
1516

@@ -69,6 +70,8 @@ If no event name is given, the event name is set to `"message"`.
6970

7071
If no event ID is given, the event ID (and thus the [`lastId` property](#session%23lastid%3A-string)) is set to a unique string generated using a [cryptographic pseudorandom number generator](https://nodejs.org/api/crypto.html#cryptorandomuuidoptions).
7172

73+
If the session has disconnected, an [`SseError`](#sseerror) will be thrown.
74+
7275
Emits the `push` event with the given data, event name and event ID in that order.
7376

7477
#### `Session#stream`: `(stream: Readable[, options: object]) => Promise<boolean>`
@@ -321,3 +324,7 @@ Get a copy of the buffer contents.
321324
Creates and returns an instance of an [EventBuffer](#eventbuffer).
322325

323326
Takes the [same arguments as the EventBuffer class constructor](#new-eventbufferoptions--).
327+
328+
### `SseError`
329+
330+
Represents an SSE-related error thrown from within Better SSE.

src/Channel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import {Session, DefaultSessionState} from "./Session";
12
import {TypedEmitter, EventMap} from "./lib/TypedEmitter";
23
import {generateId} from "./lib/generateId";
3-
import {Session, DefaultSessionState} from "./Session";
4+
import {SseError} from "./lib/SseError";
45

56
interface BroadcastOptions<
67
SessionState extends Record<string, unknown> = DefaultSessionState
@@ -77,7 +78,7 @@ class Channel<
7778
}
7879

7980
if (!session.isConnected) {
80-
throw new Error("Cannot register a non-active session.");
81+
throw new SseError("Cannot register a non-active session.");
8182
}
8283

8384
session.once("disconnected", () => {

src/Session.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {createPushFromStream} from "./lib/createPushFromStream";
1111
import {createPushFromIterable} from "./lib/createPushFromIterable";
1212
import {serialize, SerializerFunction} from "./lib/serialize";
1313
import {sanitize, SanitizerFunction} from "./lib/sanitize";
14+
import {SseError} from "./lib/SseError";
1415

1516
interface SessionOptions
1617
extends Pick<EventBufferOptions, "serializer" | "sanitizer"> {
@@ -353,7 +354,7 @@ class Session<
353354
eventId = generateId()
354355
): this => {
355356
if (!this.isConnected) {
356-
throw new Error("Cannot push data to a non-active session.");
357+
throw new SseError("Cannot push data to a non-active session.");
357358
}
358359

359360
this.buffer.push(data, eventName, eventId);

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export * from "./createEventBuffer";
99

1010
export type {StreamOptions} from "./lib/createPushFromStream";
1111
export type {IterateOptions} from "./lib/createPushFromIterable";
12+
13+
export * from "./lib/SseError";

src/lib/SseError.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class SseError extends Error {
2+
constructor(message: string) {
3+
super(message);
4+
this.message = `better-sse: ${message}`;
5+
}
6+
}
7+
8+
export {SseError};

0 commit comments

Comments
 (0)