-
Notifications
You must be signed in to change notification settings - Fork 19
Stream write callback doesn't allow handling errors #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
See #41 (comment) |
@wclr Can you provide a Node.js example that demonstrates a time where the arg will be undefined? The docs don't really clarify this, so I assumed it was always @thomashoneyman I think we'll need to fix this before |
It is undefined in v16.13.2: const fs = require("fs")
var writeStream = fs.createWriteStream("./file.txt")
writeStream.write("some", null, (err) => {
console.log("err", err) // prints out `undefined`
writeStream.end((endErr) => {
console.log("endErr", endErr) // prints out `undefined`
})
}) So to use export function write(w) {
return chunk => done => () => w.write(chunk, null, done);
} Should be changed to: export function write(w) {
return chunk => done => () => w.write(chunk, null, (err) => done(err || null));
} The same for Other approach instread of Nullable use two callbacks (success and onError), though this kind of different api. |
Thanks. I can confirm that. Idiomatically, it would be better if the API exposed to the end-user was |
I think this is right, I just usually try to make FFI code as simple as possible. |
My thought was that if we used -- We want to handle both cases
\err -> do
case toMaybe err of
Nothing -> ...
Just e -> ...
-- We only care if it's an actual error:
\err -> for_ (toMaybe err) \actualErr -> ...
-- We only care if it's not an error:
\err -> when (null == err) $ ... Since 2/3 use foreign import writeStringImpl
:: forall r. Writable r -> String -> String -> (Nullable Error -> Effect Unit) -> Effect Boolean
writeString
:: forall r. Writable r -> String -> String -> (Maybe Error -> Effect Unit) -> Effect Boolean
writeString w s1 s2 cb = writeStringImpl w s1 s2 (cb <<< toMaybe) |
Not quite. The toMaybe :: forall a. Nullable a -> Maybe a
toMaybe n = runFn3 nullable n Nothing Just
export function nullable(a, r, f) {
return a == null ? r : f(a);
} and running If |
We do tend to use |
Yeah, I can confirm this too with this example: import fs from "fs";
var file = fs.createWriteStream("./file.txt")
file.on('error', () => {});
file.write("something", () => {
file.destroy(new Error("Muahahah!"));
file.end((err) => {
console.log("Example where 'err' is Error");
console.log(err)
});
var file2 = fs.createWriteStream("./file2.txt")
file2.end((err) => {
console.log("Example where 'err' is undefined");
console.log(err);
});
}); which outputs:
|
Uh oh!
There was an error while loading. Please reload this page.
The callback should allow error handling, for example:
(Nullable Error) -> Effect Unit
though the error value passed to the callback may beundefined
or jsError
.The text was updated successfully, but these errors were encountered: