Skip to content
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

Return exceptions for all filesystem errors #44

Merged
merged 1 commit into from
Nov 14, 2023
Merged

Conversation

mcrumm
Copy link
Member

@mcrumm mcrumm commented Nov 7, 2023

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:

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:

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

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
```
Copy link
Member

@benwilson512 benwilson512 left a comment

Choose a reason for hiding this comment

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

Definitely a nice improvement, thanks!

@mcrumm mcrumm merged commit 587de5e into master Nov 14, 2023
13 checks passed
@mcrumm mcrumm deleted the mc-exceptions branch November 14, 2023 23:21
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.

2 participants