-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document error handling behavior of JsonHigh.
Add a helper to check for errors (isError). Add test for error handling.
- Loading branch information
Showing
4 changed files
with
77 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {isError, JsonHigh} from '../mod.js' | ||
|
||
Deno.test('error handling', async () => { | ||
// based on a user-provided example from https://github.com/xtao-org/jsonhilo/issues/6 | ||
const handlers = { | ||
end: () => { | ||
throw Error('Expected end handler not to be called!') | ||
}, | ||
} | ||
const stream = JsonHigh(handlers) | ||
const writable = new WritableStream({ | ||
write: (chunk) => { | ||
const decoded = new TextDecoder().decode(chunk) | ||
stream.chunk(decoded) | ||
}, | ||
close: () => { | ||
const ret = stream.end() | ||
if (isError(ret) === false) { | ||
throw Error('Expected error to be returned by stream.end()!') | ||
} | ||
}, | ||
}) | ||
const readable = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(new TextEncoder().encode('{"a":2,"b":3')) | ||
controller.close() | ||
}, | ||
}) | ||
await readable.pipeTo(writable) | ||
}) |