Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
88 changes: 88 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: CI

on:
push:
branches: [master]
pull_request:

permissions:
contents: read

jobs:
quality:
name: Elixir ${{ matrix.elixir }} / OTP ${{ matrix.otp }}
runs-on: ubuntu-22.04
env:
MIX_ENV: test
strategy:
fail-fast: false
matrix:
include:
- elixir: "1.14.5"
otp: "25.3"
- elixir: "1.20.2"
otp: "29.0.3"
dialyzer: true

steps:
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
elixir-version: ${{ matrix.elixir }}
otp-version: ${{ matrix.otp }}

- name: Restore dependencies cache
uses: actions/cache@v4
with:
path: deps
key: deps-${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-${{ hashFiles('mix.exs', 'mix.lock') }}

- name: Restore build cache
uses: actions/cache@v4
with:
path: _build
key: build-${{ runner.os }}-${{ matrix.elixir }}-${{ matrix.otp }}-${{ hashFiles('mix.exs', 'mix.lock') }}

- run: mix deps.get

- name: Check formatting
run: mix format --check-formatted || (mix format && git diff --exit-code)

- name: Compile
run: |
set -o pipefail
mix compile --warnings-as-errors 2>&1 | tee compile.log

- name: Test
run: |
set -o pipefail
mix test 2>&1 | tee test.log

- run: mix credo --strict
- run: mix hex.build

- name: Dialyzer
if: matrix.dialyzer == true
run: |
set -o pipefail
{
dialyzer --build_plt --output_plt .dialyzer_plt \
--apps erts kernel stdlib compiler crypto parsetools
elixir_ebin="$(elixir -e 'IO.puts(:code.lib_dir(:elixir, :ebin))')"
dialyzer -Wno_unknown -pa "$elixir_ebin" \
--add_to_plt --plt .dialyzer_plt "$elixir_ebin"
dialyzer --no_check_plt -pa "$elixir_ebin" \
--plt .dialyzer_plt _build/test/lib/errx/ebin
} 2>&1 | tee dialyzer.log

- name: Upload failure diagnostics
if: failure()
uses: actions/upload-artifact@v4
with:
name: diagnostics-${{ matrix.elixir }}-${{ matrix.otp }}
path: |
compile.log
test.log
dialyzer.log
if-no-files-found: ignore
54 changes: 29 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
errx-*.tar

.elixir_ls/
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
errx-*.tar

.elixir_ls/

# Dialyzer PLTs generated by local quality checks.
*.plt
*.plt.hash
153 changes: 146 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,160 @@
# Errx

**TODO: Add description**
Errx enriches conventional Elixir `{:error, reason}` tuples with the caller
location, optional metadata, and a chain of causes. It keeps the tuple shape
used by `case`, `with`, and tagged-return APIs while preserving enough context
to understand where an error was created.

Errx is currently pre-1.0. Minor releases may refine the public API, but
changes are documented and existing tuple-based workflows are kept compatible
where practical.

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `errx` to your list of dependencies in `mix.exs`:
Add `errx` to the dependencies in `mix.exs`:

```elixir
def deps do
[
{:errx, "~> 0.1.0"}
{:errx, "~> 0.5"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/errx](https://hexdocs.pm/errx).
Errx 0.5 requires Elixir 1.14 or later.

## Wrap errors

```elixir
{:error, error} = Errx.wrap(:not_found)

error.reason
#=> :not_found

error.location
#=> %{
#=> module: MyApp.Users,
#=> function: :fetch,
#=> arity: 1,
#=> file: "lib/my_app/users.ex",
#=> line: 42
#=> }
```

`file` and `func` remain available as formatted compatibility fields. New code
should prefer the structured `location` map.

Both raw reasons and standard error tuples are accepted:

```elixir
Errx.wrap(:not_found)
Errx.wrap({:error, :not_found})
```

Wrapping an existing Errx value is idempotent and preserves its original
location.

## Match reasons

Use `Errx.match/1` in patterns:

```elixir
case Errx.wrap(:not_found) do
Errx.match(:not_found) -> :recover
end
```

Use `Errx.match/2` in assertions or ordinary expressions:

```elixir
Errx.match(Errx.wrap(:not_found), {:error, :not_found})
#=> true

Errx.match(:not_found, :not_found)
#=> false
```

At least one argument must be an Errx value. Unrelated values return `false`
rather than raising.

## Add metadata

`put_metadata/2` replaces metadata explicitly:

```elixir
error = Errx.put_metadata(:not_found, %{resource: "user", id: 123})
Errx.metadata(error)
#=> %{resource: "user", id: 123}
```

`merge_metadata/2` merges maps and overwrites duplicate keys with the new
values:

```elixir
:failure
|> Errx.put_metadata(%{request_id: "abc", retry: false})
|> Errx.merge_metadata(%{retry: true})
```

The older `metadata/2` function remains as a replacement alias for backwards
compatibility.

## Chain causes

The historical API accepts parent first and child second:

```elixir
{:error, error} = Errx.wrap(:database_unavailable, :lookup_failed)

error.reason
#=> :lookup_failed

error.parent.reason
#=> :database_unavailable
```

`with_parent/2` provides the explicit child-first form:

```elixir
Errx.with_parent(:lookup_failed, :database_unavailable)
```

When the child already has a parent chain, the new parent is appended rather
than replacing existing context. `Errx.cause/1` reads the immediate parent.
The struct field remains named `parent` in 0.x for compatibility; its semantic
meaning is the underlying cause.

## Raise Errx exceptions

```elixir
raise Errx, :not_found
raise Errx, "user was not found"
raise Errx, Errx.wrap(:not_found)
```

Binary attributes become the exception message. Other arbitrary attributes
are stored under `metadata.data`.

## Location limitations

Locations are collected from the current process stack trace. Errx scans for
the first frame outside the Errx and Process modules instead of relying on a
fixed stack index. Generated code, aggressive inlining, or unusual runtime
stack frames may still affect which caller is reported.

## Development

Run the complete local quality suite with:

```shell
mix deps.get
mix quality
```

CI verifies formatting, warnings, tests, Credo, package construction, and a
Dialyzer pass. The matrix covers the oldest supported Elixir release and the
latest stable Elixir/OTP combination.

## License

Errx is licensed under GPL-3.0-only. See `LICENSE`.
Loading
Loading