Explicity raise Phoenix.Router.NoRouteError

I want to explicitly render the default debug page of Phoenix.Router.NoRouteError with stacktrace when a certain environment variable is not set and user tries to illegally access a route (/exampleroute).

When an environment variable is not set, in dev environment I want to display the debug stack trace page.

When it happens in prod, I want to display the JSON response {"errors":{"detail":"Not Found"}}.

But when the environment variable is set in both environments, I am using plug controller which allows the user to access the route (/exampleroute) succesfully.

raise Phoenix.Router.NoRouteError, conn: conn, router: MyProjectWeb.router, plug_Status: 404

This is not working from controller plug in dev environment as I am only getting {"errors":{"detail":"Internal Server Error"}} and no stacktrace is getting displayed.

P.S: The debug page is getting correctly displayed for any other unspecified routes.

I’m not very clear about your problem. But since I don’t see /fauce route in your available routes. Most probably you’re calling the wrong route.

This problem happens when it does not get the correct route

I want to know how to explicitly throw this error from controller/conn? I want to render this error debug webpage for a particular route. This error will be deliberately thrown for a particular route.

You can take a look at this

Phoenix.Controller.action_fallback/1

How do I render the stack trace page using this function?

why do you need to render stack trace? You can just return the error view which will give you no route found. And you will not get internal server error

You can get the stacktrace of the current process using Process.info(self(), :current_stacktrace)

1 Like