Skip to content

Commit

Permalink
feat: added onepiece commanded ignore error (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
yordis committed May 15, 2023
1 parent 6b86a59 commit 4186850
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions apps/one_piece_commanded/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## v0.18.0 - 2023-05-15

- Added `OnePiece.Commanded.ignore_error/2`.

## v0.17.0 - 2023-04-28

- Added `OnePiece.Commanded.Aggregate.StatelessLifespan` module.
Expand Down
1 change: 1 addition & 0 deletions apps/one_piece_commanded/lib/one_piece/commanded.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ defmodule OnePiece.Commanded do
defdelegate tracing_from_metadata(opts, metadata), to: OnePiece.Commanded.Helpers
defdelegate skip_or_retry(tuple_response, delay, context), to: OnePiece.Commanded.Helpers
defdelegate increase_failure_counter(failure_context), to: OnePiece.Commanded.Helpers
defdelegate ignore_error(result, error), to: OnePiece.Commanded.Helpers
end
26 changes: 26 additions & 0 deletions apps/one_piece_commanded/lib/one_piece/commanded/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,30 @@ defmodule OnePiece.Commanded.Helpers do
def increase_failure_counter(%FailureContext{} = failure_context) do
Map.update(failure_context.context, :failures_count, 1, &(&1 + 1))
end

@doc """
Ignores a specific error from a command dispatch result.
This function takes a dispatch result and an error term. If the result represents an error that matches the provided
error term, the function returns `:ok`. Otherwise, it returns the original result.
This is useful when an error condition should be treated as a successful operation under specific circumstances.
## Examples
iex> OnePiece.Commanded.Helpers.ignore_error({:error, :idempotency_failure}, :idempotency_failure)
:ok
iex> OnePiece.Commanded.Helpers.ignore_error({:error, :something_went_wrong}, :idempotency_failure)
{:error, :something_went_wrong}
iex> OnePiece.Commanded.Helpers.ignore_error(:ok, :idempotency_failure)
:ok
iex> OnePiece.Commanded.Helpers.ignore_error({:ok, %{name: "Billy"}}, :idempotency_failure)
{:ok, %{name: "Billy"}}
"""
@spec ignore_error(result :: commanded_dispatch_response, error: any) :: commanded_dispatch_response
def ignore_error({:error, expected_error}, expected_error), do: :ok
def ignore_error(result, _expected_error), do: result
end
2 changes: 1 addition & 1 deletion apps/one_piece_commanded/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule OnePiece.Commanded.MixProject do
use Mix.Project

@app :one_piece_commanded
@version "0.17.0"
@version "0.18.0"
@elixir_version "~> 1.13"
@source_url "https://github.com/straw-hat-team/beam-monorepo"

Expand Down

0 comments on commit 4186850

Please sign in to comment.