Absinthe Middleware not "catching" error

I’m raising an error inside a resolver with. I would like Absinthe Middleware to handle it, so I can display it to the frontend team. However when the error raises, the resolution variable doesn’t have any errors inside it.

What would I be doing wrong?

  def check(questions) do
    case questions do
      nil -> raise ArgumentError, message: "Invalid number of questions."
      _ -> some_extra_stuff
    end
  end
raise ArgumentError, message: "Invalid number of questions."
defmodule MyApp.Schema.Middleware.ArgumentErrors do
  @behaviour Absinthe.Middleware

  @impl true
  def call(resolution, _) do
    IO.inspect resolution
    resolution
  end

end
defmodule AdmissionWeb.Schema do
  use Absinthe.Schema
  alias AdmissionWeb.Schema.Middleware
  alias Admission.{Accounts, Exams}

  def dataloader() do
    Dataloader.new
    |> Dataloader.add_source(Accounts, Accounts.data())
    |> Dataloader.add_source(Exams, Exams.data())
  end

  def context(ctx) do
    Map.put(ctx, :loader, dataloader())
  end

  def plugins do
    [Absinthe.Middleware.Dataloader | Absinthe.Plugin.defaults]
  end

  def middleware(middleware, _field, %{identifier: :mutation}) do
    middleware ++ [Middleware.ArgumentErrors]
  end

  def middleware(middleware, _field, _object) do
    middleware
  end

end
1 Like

Absinthe does not catch errors for you automatically. If you want to catch errors, you need to wrap the standard resolution middleware in your own which catches exceptions.

2 Likes

You mean, that I should place the middleware inside a try/catch in the resolver?

I thought that the idea of the middleware was to be reusable.