Giter Site home page Giter Site logo

seth-schroeder / credo_check_error_handling_ecto_oban Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 33 KB

Custom credo check for edge cases in error handling with Oban and Ecto.Multi

License: GNU General Public License v3.0

Elixir 100.00%
credo ecto elixir oban

credo_check_error_handling_ecto_oban's Introduction

CredoCheckErrorHandlingEctoOban

This is a custom Credo check that looks for a very specific edge case:

  • Ecto.Repo.transaction/2 will return a 4 tuple when an error occurs inside a Multi.
  • An Oban worker that returns an error 4 tuple will be considered a success.

Many thanks to @andersonmcook for being the human version of this check!

Below is the warning that Oban gives when this happens in iex

  iex(14)> [warning] Expected Elixir.MyApp.MultiFailure.perform/1 to return:
  - `:ok`
  - `:discard`
  - `{:ok, value}`
  - `{:error, reason}`,
  - `{:cancel, reason}`
  - `{:discard, reason}`
  - `{:snooze, seconds}`
  Instead received:
  {:error, :alas, :poor_yorick, %{}}

  The job will be considered a success.

Here is an example of a potential situation:

def perform(%{}) do
  Multi.new()
  |> Multi.error(:alas, :poor_yorick)
  |> Repo.transaction()
end

Here is a possible resolution (mapping the 4 tuple to a 2 tuple):

def perform(%{}) do
  Multi.new()
  |> Multi.error(:alas, :poor_yorick)
  |> Repo.transaction()
  |> case do
       {:error, :alas, _, _} -> {:error, "we knew him well"}
       any -> any
     end
end

Please note that this custom credo check is known to have false positives. In order to address that it would have to grow closer to an interpreter. It has been lightly tested against public repositories, and has moderate unit test coverage.

My Org mode brain dump is progress.org.

Installation

If available in Hex, the package can be installed by adding credo_check_error_handling_ecto_oban to your list of dependencies in mix.exs:

def deps do
  [
    {:credo_check_error_handling_ecto_oban, "~> 0.9.0", only: [:dev, :test], runtime: false}
  ]
end

Add to your .credo.exs.

Recent versions of credo:

  checks: %{
    enabled: [
      # ...
      {CredoCheckErrorHandlingEctoOban.Check.TransactionErrorInObanJob, []}
    ]
  }

Older versions of credo:

  checks: [
    # ...
    {CredoCheckErrorHandlingEctoOban.Check.TransactionErrorInObanJob, []}
  ]

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/credo_check_error_handling_ecto_oban.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.