Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- 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.


## Unreleased
- Add support for POSIX error `Ehostdown`.
- Fix bug where unknown errrs were not handled in Erlang ffi.

## v2.3.1 - 6 November 2025
- Fix bug with error string typo for `Enotdir` on JS.
Expand Down
3 changes: 3 additions & 0 deletions src/simplifile.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub type FileError {
/// Inappropriate file type or format. Usually caused by trying to set the
/// "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?

/// Interrupted system call.
Eintr
/// Invalid argument.
Expand Down Expand Up @@ -170,6 +172,7 @@ pub fn describe_error(error: FileError) -> String {
Enosr -> "No STREAM resources"
Enostr -> "Not a STREAM"
Eopnotsupp -> "Operation not supported on socket"
Ehostdown -> "Host is down"
NotUtf8 -> "File not UTF-8 encoded"
Unknown(inner) -> "Unknown error: " <> inner
}
Expand Down
8 changes: 7 additions & 1 deletion src/simplifile_erl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
orelse Error =:= efault
orelse Error =:= efbig
orelse Error =:= eftype
orelse Error =:= ehostdown
orelse Error =:= eintr
orelse Error =:= einval
orelse Error =:= eio
Expand Down Expand Up @@ -91,9 +92,14 @@ posix_result(Result) ->
{ok, Value} ->
{ok, Value};
{error, Reason} when ?is_posix_error(Reason) ->
{error, Reason}
{error, Reason};
{error, Reason} ->
{error, {unknown, error_reason_to_binary(Reason)}}
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)


%% Read the binary contents of a file
read_bits(Filename) ->
posix_result(file:read_file(Filename)).
Expand Down
2 changes: 2 additions & 0 deletions src/simplifile_js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ function cast_error(error_code) {
return new $simplifile.Eftype();
case "EINTR":
return new $simplifile.Eintr();
case "EHOSTDOWN":
return new $simplifile.Ehostdown();
case "EINVAL":
return new $simplifile.Einval();
case "EIO":
Expand Down
5 changes: 5 additions & 0 deletions test/create_directory_bad_arg.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createDirectory } from "./simplifile_js.mjs";

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.

39 changes: 30 additions & 9 deletions test/simplifile_test.gleam
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import gleam/int
import gleam/list
import gleam/set
import gleam/string
import gleeunit
import gleeunit/should
import simplifile.{
import simplifile.{type FileError,
Directory, Eacces, Eagain, Ebadf, Ebadmsg, Ebusy, Edeadlk, Edeadlock, Edquot,
Eexist, Efault, Efbig, Eftype, Einval, Eio, Eisdir, Eloop, Emfile, Emlink,
Emultihop, Enametoolong, Enfile, Enobufs, Enodev, Enoent, Enolck, Enolink,
Enomem, Enospc, Enosr, Enostr, Enosys, Enotblk, Enotdir, Enotsup, Enxio,
Eexist, Efault, Efbig, Eftype, Ehostdown, Einval, Eio, Eisdir, Eloop, Emfile,
Emlink, Emultihop, Enametoolong, Enfile, Enobufs, Enodev, Enoent, Enolck,
Enolink, Enomem, Enospc, Enosr, Enostr, Enosys, Enotblk, Enotdir, Enotsup, Enxio,
Eopnotsupp, Eoverflow, Eperm, Epipe, Erange, Erofs, Espipe, Esrch, Estale,
Etxtbsy, Exdev, Execute, File, FilePermissions, NotUtf8, Read, Unknown, Write,
append, append_bits, copy, copy_directory, copy_file, create_directory,
create_directory_all, create_file, create_link, create_symlink, delete,
delete_all, file_info, file_info_permissions, file_info_permissions_octal,
Etxtbsy, Exdev, Execute, File, FilePermissions, NotUtf8, Read,
Unknown, Write, append, append_bits, copy, copy_directory, copy_file,
create_directory, create_directory_all, create_file, create_link, create_symlink,
delete, delete_all, file_info, file_info_permissions, file_info_permissions_octal,
file_info_type, file_permissions_to_octal, get_files, is_directory, is_file,
is_symlink, link_info, read, read_bits, read_directory, rename,
set_permissions, set_permissions_octal, write, write_bits,
Expand Down Expand Up @@ -483,7 +484,11 @@ pub fn file_info_get_permissions_test() {
pub fn get_files_with_slash_test() {
let assert Ok(files) = get_files(in: "./test/")
files
|> should.equal(["./test/simplifile_test.gleam"])
|> list.sort(string.compare)
|> should.equal([
"./test/create_directory_bad_arg.mjs",
"./test/simplifile_test.gleam",
])
}

// This test is only for local development
Expand Down Expand Up @@ -692,6 +697,8 @@ pub fn describe_error_test() {
let assert "Operation not supported on socket" =
simplifile.describe_error(Eopnotsupp)

let assert "Host is down" = simplifile.describe_error(Ehostdown)

let assert "File not UTF-8 encoded" = simplifile.describe_error(NotUtf8)

let assert "Unknown error: Something went wrong" =
Expand Down Expand Up @@ -761,3 +768,17 @@ pub fn parse_errors_test() {
simplifile.create_directory_all("./tmp/wumbo/wombo") |> should.be_error
err |> should.equal(Enotdir)
}

pub fn unknown_errors_return_unknown_test() {
let err = create_directory_with_bad_arg(#(Nil, Nil))
err |> should.be_error

let assert Error(unknown) = err
let assert Unknown(inner) = unknown
inner |> should.not_equal("")
}

// This is necessary to force unknown error generation uniformly across runtimes
@external(erlang, "simplifile_erl", "create_directory")
@external(javascript, "./create_directory_bad_arg.mjs", "createDirectoryWithBadArg")
fn create_directory_with_bad_arg(arg: #(Nil, Nil)) -> Result(Nil, FileError)