This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle unexpected end of stream (#46)
- we can't know if a specific async type is done or not as it's the responsibility of the type itself - we can know if a stream is interrupted prematurely, so I added a check for that - we treat the "stream interrupted" as a message that we enqueue - if the interruption happens when the iterator has already consumed what it needs, we're all good
- Loading branch information
Showing
13 changed files
with
478 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TsonError } from "../errors.js"; | ||
import { isPlainObject } from "../internals/isPlainObject.js"; | ||
|
||
function getErrorMessageFromUnknown(unknown: unknown): null | string { | ||
if (typeof unknown === "string") { | ||
return unknown; | ||
} | ||
|
||
if (unknown instanceof Error) { | ||
return unknown.message; | ||
} | ||
|
||
if (isPlainObject(unknown) && typeof unknown["message"] === "string") { | ||
return unknown["message"]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export class TsonPromiseRejectionError extends TsonError { | ||
constructor(cause: unknown) { | ||
// get error message from cause if possible | ||
const message = getErrorMessageFromUnknown(cause) ?? "Promise rejected"; | ||
super(message, { cause }); | ||
this.name = "TsonPromiseRejectionError"; | ||
} | ||
|
||
static from(cause: unknown) { | ||
return cause instanceof Error | ||
? cause | ||
: new TsonPromiseRejectionError(cause); | ||
} | ||
} | ||
|
||
export class TsonStreamInterruptedError extends TsonError { | ||
constructor(cause: unknown) { | ||
super( | ||
"Stream interrupted: " + | ||
(getErrorMessageFromUnknown(cause) ?? "unknown reason"), | ||
{ cause }, | ||
); | ||
this.name = "TsonStreamInterruptedError"; | ||
} | ||
} |
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
Oops, something went wrong.