       end
 
     quote do
-      opts     = Keyword.put(unquote(options), :singular, true)
-      resource = Resource.build(unquote(path), unquote(controller), opts)
-
-      path = resource.path
-      ctrl = resource.controller
-      opts = resource.route
-
-      Enum.each resource.actions, fn action ->
-        case action do
-          :show    -> get    path,            ctrl, :show, opts
-          :new     -> get    path <> "/new",  ctrl, :new, opts
-          :edit    -> get    path <> "/edit", ctrl, :edit, opts
-          :create  -> post   path,            ctrl, :create, opts
-          :delete  -> delete path,            ctrl, :delete, opts
-          :update  ->
-            patch path, ctrl, :update, opts
-            put   path, ctrl, :update, Keyword.put(opts, :as, nil)
-        end
-      end
-
-      unquote(scope)
+      try do
+        opts     = Keyword.put(unquote(options), :singular, true)
+        resource = Resource.build(unquote(path), unquote(controller), opts)
+
+        path = resource.path
+        ctrl = resource.controller
+        opts = resource.route
+
+        Enum.each resource.actions, fn action ->
+          case action do
+            :show    -> get    path,            ctrl, :show, opts
+            :new     -> get    path <> "/new",  ctrl, :new, opts
+            :edit    -> get    path <> "/edit", ctrl, :edit, opts
+            :create  -> post   path,            ctrl, :create, opts
+            :delete  -> delete path,            ctrl, :delete, opts
+            :update  ->
+              patch path, ctrl, :update, opts
+              put   path, ctrl, :update, Keyword.put(opts, :as, nil)
+          end
+        end
+
+        unquote(scope)
+      after
+        :ok
+      end
     end
   end
 

    add_socket(mount, Dict.merge(opts, alias: chan_alias), chan_block)
  end
  defp add_socket(mount, opts, chan_block) do
    quote bind_quoted: [mount: mount, opts: opts], unquote: true do
      if Scope.inside_scope?(__MODULE__) do
        raise """
        You are trying to call `socket` within a `scope` definition.
        Please move your socket and channel definitions outside of any scope block.
        """
      end

      @phoenix_socket_mount mount
      @phoenix_transports opts[:via]
      @phoenix_channel_alias opts[:alias]
      get  @phoenix_socket_mount, Phoenix.Transports.WebSocket, :upgrade, Dict.take(opts, [:as])
      post @phoenix_socket_mount, Phoenix.Transports.WebSocket, :upgrade
      get  @phoenix_socket_mount <> "/poll", Phoenix.Transports.LongPoller, :poll
      post @phoenix_socket_mount <> "/poll", Phoenix.Transports.LongPoller, :publish
      unquote(chan_block)
      @phoenix_socket_mount nil
      @phoenix_transports nil
      @phoenix_channel_alias nil
    end
  end


  end

  defp add_route(verb, path, controller, action, options) do
    route =
      quote do
        route = Scope.route(__MODULE__, unquote(verb), unquote(path), unquote(controller),
                                        unquote(action), unquote(options))
        exprs = Route.exprs(route)
        @phoenix_routes {route, exprs}
      end

    definition =
      quote unquote: false do
        defp match(var!(conn), unquote(route.verb), unquote(exprs.path), unquote(exprs.host)) do
          unquote(exprs.dispatch)
        end
      end

    quote do
      try do
        unquote(route)
        unquote(definition)
      after
        :ok
      end
    end
  end
