cpjk / canary

:hatching_chick: Elixir authorization and resource-loading library for Plug applications.
MIT License
474 stars 51 forks source link

The parent nested resource does not handle errors #82

Open quexpl opened 4 years ago

quexpl commented 4 years ago

We have the Network model which belongs to the Hypervisor, and /networks resources are nested like this:

    resources "/hypervisors", HypervisorController do
      resources "/networks", NetworkController, only: [:new, :create, :index]
    end

According to the docs persisted option is a bless for nested resources.

  plug :load_resource,
     model: Hypervisor,
     id_name: "hypervisor_id",
     only: [:index, :create, :new],
     preload: [:hypervisor_type],
     persisted: true

This is almost perfect. For :index, :new or :create action the handle_not_found/2 will never call error handler. For our case it's essential to allow visit nested pages only if parent resource exists.

So now when I visit /hypervisors/999/networks the conn.assigns[:hypervisor will be nil, I'd expect a 404 error.