francois1

francois1

Absinthe and handling not found errors

In Phoenix Controllers, we use the bang-versions of the Ecto Repo functions, such as Repo.get!/3. In production, these errors will be intercepted and a 404 page is presented to the user when working with Phoenix templates.

In Absinthe, is it possible to achieve something similar? I.e. when an Ecto.NoResultsError error is raised, have the following data returned with a 200 http code:

{
  "data": {
    "user": null
  },
  "errors": [
    {
      "message": "Not found",
      "locations": [{"line": 2, "column": 3}],
      "path": ["user"]
    }
  ]
}

This allows to avoid having to write a set of case/with control structures and use the bang-versions of the Ecto functions, the same way we work with Phoenix Controllers.

Wouldn’t that be the best way to work in resolvers? If not, why? Any alternatives?

First Post!

francois1

francois1

I went for a hybrid approach where

  1. resources’ IDs passed directly as arguments are processed by a middleware which attempts to fetch the resource and puts it in the context, or adds an error (in errors). Example:
field :create_foo, :create_or_update_foo_result do
  arg :input, non_null(:foo_input)
  arg :organization_id, non_null(:id)

  middleware Middleware.HandleNotFoundError,
    arg: :organization_id,
    context_key: :organization,
    fetcher: &MyContext.get_organization(&2.current_user, &1)

  resolve(&Resolvers.MyContext.create_foo/3)
end

I can then access the organization in this example from the context.

  1. IDs passed in the attrs map for the context (:input object above) are handled by foreign_key_constraint/3 when building the changeset, and I return to the client either changeset errors (in data) or a specialized object like BarNotFound { id }.

And this is where it’s really, really nice to work with soft deletes where I do not have to return nice errors to the client in case there is a foreign key error and deal with annoying questions related to concurrent fetch/delete operations driving one’s crazy (with soft deletes, foreign key errors would only be coming from either a malicious user trying to change the IDs or from a programmer bug).

Where Next?

Popular in Discussions Top

jeramyRR
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
fireproofsocks
I’ve been working on an Elixir project that has required a lot of scripting. I usually reach for Elixir because I like it more (and in th...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement