-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return exceptions for all filesystem errors
The following changes are aimed at simplifying the Briefly API: `Briefly.create()` and `Briefly.create(opts)` now return either `{:ok, path}` or `{:error, Exception.t()}`. The following exceptions may be returned: - `%Briefly.NoRootDirectoryError{}` - Returned if Briefly is unable to create a root temporary directory. - `%Briefly.WriteError{}` - Returned when a temporary entry cannot be created. The exception contains the POSIX error code the caused the failure. The `:too_many_attempts` error is not directly actionable so it is no longer surfaced. Since Briefly still performs several write attempts when faced with `:eexist` and `:eaccess` error codes, too many attempts can be inferred when either of those codes are returned. The error messages raised by `Briefly.create!()` have been moved to the Exception modules and aside from no longer printing the number of attempts they provide the same information. For example, if you have this: ```elixir case Briefly.create() do {:ok, path} -> path {:no_space, _} -> raise "no space" {:too_many_attempts, _} -> raise "too many attempts" {:no_tmp, _} -> raise "no tmp dirs" end ``` ...then change it to this: ```elixir case Briefly.create() do {:ok, path} -> path {:error, %Briefly.WriteError{code: :enospc}} -> raise "no space" {:error, %Briefly.WriteError{code: c}} when c in [:eexist, :eacces] -> raise "too many attempts" {:error, %Briefly.NoRootDirectoryError{}} -> raise "no tmp dirs" {:error, exception} -> raise exception end ```
- Loading branch information
Showing
4 changed files
with
65 additions
and
37 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,38 @@ | ||
defmodule Briefly.NoRootDirectoryError do | ||
@moduledoc """ | ||
Returned when none of the root temporary directories could be accessed. | ||
""" | ||
@type t :: %__MODULE__{ | ||
:tmp_dirs => [String.t()] | ||
} | ||
defexception [:tmp_dirs] | ||
|
||
@impl true | ||
def message(_) do | ||
"could not create a directory to store temporary files." <> | ||
" Set the :briefly :directory application setting to a directory with write permission" | ||
end | ||
end | ||
|
||
defmodule Briefly.WriteError do | ||
@moduledoc """ | ||
Returned when a temporary file cannot be written. | ||
""" | ||
@type t :: %__MODULE__{ | ||
:code => :file.posix() | :badarg | :terminated | :system_limit, | ||
:entry_type => :directory | :file, | ||
:tmp_dir => String.t() | ||
} | ||
defexception [:code, :entry_type, :tmp_dir] | ||
|
||
@impl true | ||
def message(%{code: code} = e) when code in [:eexist, :eacces] do | ||
"tried to create a temporary #{e.entry_type} in #{e.tmp_dir} but failed." <> | ||
" Set the :briefly :directory application setting to a directory with write permission" | ||
end | ||
|
||
@impl true | ||
def message(e) do | ||
"could not write #{e.entry_type} in #{e.tmp_dir}, got: #{inspect(e.code)}" | ||
end | ||
end |