Skip to content

Conversation

@felixakiragreen
Copy link
Contributor

Issue Described Here

How this happened:

  • I encountered an unmatched clause {error,ehostdown} when trying to write .append() to a file mounted via smb.

What I did:

  • Added a case for the POSIX error to is_posix_error in simplifile_erl.erl.
  • @lpil noticed that in JavaScript unknown errors are converted to Unknown, but on Erlang it crashes.
  • Added an unknown error case to posix_result, which converts the reason into a string.
  • Added a test to ensure that any unknown errors properly return the Unknown type.

Questions:

  • I discovered a number of lists of POSIX errors that had many more errors on them. This one by Apple seems to be one of the most comprehensive. If you want I can add support for the rest of them.
  • The first test I wrote worked in Erlang, Node, and Bun, but not Deno. So in order to force an unknown error uniformly across all runtimes, I created a shim that let me pass in a bad argument. I hope this is acceptable, if you know another way please let me know.

/// "sticky bit" on a regular file (not a directory).
Eftype
/// Host is down.
Ehostdown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. Would it be better to use Unknown?

end.

error_reason_to_binary(Reason) ->
unicode:characters_to_binary(io_lib:format("~tp", [Reason])).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think atom_to_binary would be better used here.

Would it also make sense to uppercase the string? To match how it is written in POSIX (and also also the JavaScript target)


export function createDirectoryWithBadArg(arg) {
return createDirectory(arg);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module could be removed and the @external reference ./simplifile_js.mjs directly.

@bcpeinhardt
Copy link
Owner

@felixakiragreen thank you for fixing this!

I think the main issue here is the crash/difference in error behavior. I think first, a PR fixing the erlang ffi to make use of the Unknown error variant to prevent the crash would be good that could be released as a patch version.

If we decide to change the list of recognized errors, we can do that in a follow up PR.

{error, Reason}
{error, Reason};
{error, Reason} ->
{error, {unknown, string:uppercase(atom_to_binary(Reason))}}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atom_to_binary function defaults to utf8, so it should be fine to pass the resulting binary to string:uppercase

@@ -1,9 +1,10 @@
# Changelog

- Add documentation for 'mode' field in FileInfo.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a docs update that doesn't affect the version number, so I think it shouldn't be in the changelog.

@bcpeinhardt bcpeinhardt merged commit c7c5714 into bcpeinhardt:main Dec 26, 2025
1 check passed
@bcpeinhardt
Copy link
Owner

closes #54

@bcpeinhardt bcpeinhardt changed the title Add support for error Ehostdown & handle Unknown errors in Erlang Handle Unknown errors in Erlang Dec 26, 2025
@felixakiragreen
Copy link
Contributor Author

Thank you so much! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants