diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f417b1c..1870b05 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,7 @@ jobs: - uses: erlef/setup-beam@v1 with: otp-version: "27" - gleam-version: "1.9.0" + gleam-version: "1.13.0" rebar3-version: "3" # elixir-version: "1.14.2" - run: gleam test diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d49b3c..fc448e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ # Changelog -- Add documentation for 'mode' field in FileInfo. - ## Unreleased +## v2.3.2 - 26 December 2025 +- Fix bug where unknown errors were not properly converted to the Unknown variant in Erlang ffi. + ## v2.3.1 - 6 November 2025 - Fix bug with error string typo for `Enotdir` on JS. diff --git a/gleam.toml b/gleam.toml index 33a959a..b974558 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,5 +1,5 @@ name = "simplifile" -version = "2.3.1" +version = "2.3.2" description = "Basic file operations that work on all targets" licences = ["Apache-2.0"] diff --git a/src/simplifile_erl.erl b/src/simplifile_erl.erl index 09bef7a..a1d138f 100644 --- a/src/simplifile_erl.erl +++ b/src/simplifile_erl.erl @@ -91,7 +91,9 @@ posix_result(Result) -> {ok, Value} -> {ok, Value}; {error, Reason} when ?is_posix_error(Reason) -> - {error, Reason} + {error, Reason}; + {error, Reason} -> + {error, {unknown, string:uppercase(atom_to_binary(Reason))}} end. %% Read the binary contents of a file diff --git a/test/simplifile_test.gleam b/test/simplifile_test.gleam index 48701c6..0cbe3c2 100644 --- a/test/simplifile_test.gleam +++ b/test/simplifile_test.gleam @@ -1,21 +1,23 @@ import gleam/int import gleam/list import gleam/set +import gleam/string import gleeunit import gleeunit/should import simplifile.{ - 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, - 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, - 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, + 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, 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, 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, } pub fn main() { @@ -483,7 +485,9 @@ 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"]) + |> should.equal([ + "./test/simplifile_test.gleam", + ]) } // This test is only for local development @@ -761,3 +765,19 @@ 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("") + inner |> string.uppercase |> should.equal(inner) + // confirm the string has been uppercased +} + +// This is necessary to force unknown error generation uniformly across runtimes +@external(erlang, "simplifile_erl", "create_directory") +@external(javascript, "./simplifile_js.mjs", "createDirectory") +fn create_directory_with_bad_arg(arg: #(Nil, Nil)) -> Result(Nil, FileError)