halostatue

halostatue

Phoenix Error Rendering & Multiple Endpoints

These are both likely a case of either “holding the tool wrong” or “using the wrong tool”, but I am looking for information and advice.

We have built our Phoenix app in such a way that we have, excepting for ErrorView, never used a view (we are an API-only setup, and use GraphQL as our primary API, but have integrations and other needs where we have some non-GraphQL (mostly JSON) APIs. There are very few “resources”. Our controller methods (often not called by the traditional names because they aren’t resources) mostly use Phoenix.Controller.json/2 directly for rendering.

Question 1

Is there a way to handle errors directly in a Phoenix application without defining render_errors[view: …] (up to 1.6) or render_errors[formats: …] (1.7+)?

Question 2

I believe that we have been also doing things wrong with how we handle certain sub-applications (we use an umbrella).

Our app structure looks something like this:

  • MyApp
  • clients:
    • MyApp.Client1
    • MyApp.Client2
  • MyApp.GraphQL
  • MyApp.Web

MyApp.Web has defined an endpoint, and it defines forward routes (excessively simplified here):

forward "/graphql", MyApp.GraphQL
forward "/clients/one", MyApp.Client1.Router
forward "/clients/two", MyApp.Client2.Router
# ...

As of right now, each client (MyApp.Client1, MyApp.Client2, etc.) defines another Phoenix.Endpoint for testing (but not for non-test mode). Is there a good reason to define such an Endpoint, or is there a way to avoid defining such an Endpoint as it is not used in the normal implementation?

We can’t reference MyApp.Web.Endpoint from MyApp.Client1 (etc.) without getting a cross-reference issue (access is always up in the list; client apps have no knowledge of one another, etc.).

Most Liked

LostKobrakai

LostKobrakai

What’s the reason for trying to avoid them? You’ll always have errors that you cannot handle explicitly. E.g. a 500 is almost by definition an error you didn’t handle. Your remarks make it sound like views are something to be avoided, which isn’t the case.

You could use a test endpoint, where you forward requests at runtime to different routers based on some information in the conn, which you setup in tests.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New