[Ovirt-devel] [PATCH server 2/4] * app/controllers/application.rb: handler for permission errors

Scott Seago sseago at redhat.com
Tue May 5 16:21:37 UTC 2009


David Lutterkort wrote:
> Use Rails' rescue_from to catch permission errors and render an appropriate
> response.
> ---
>  src/app/controllers/application.rb |   35 +++++++++++++++++++++++++++++++++++
>  1 files changed, 35 insertions(+), 0 deletions(-)
>
> diff --git a/src/app/controllers/application.rb b/src/app/controllers/application.rb
> index e5f4d4b..3902e78 100644
> --- a/src/app/controllers/application.rb
> +++ b/src/app/controllers/application.rb
> @@ -48,6 +48,8 @@ class ApplicationController < ActionController::Base
>    before_filter :tmp_authorize_admin, :only => [:create, :update, :destroy]
>    before_filter :is_logged_in, :get_help_section
>  
> +  # General error handlers
> +  rescue_from PermissionError, :with => :handle_perm_error
>  
>    def choose_layout
>      if(params[:component_layout])
> @@ -140,6 +142,39 @@ class ApplicationController < ActionController::Base
>      end
>    end
>  
> +  def handle_perm_error(error)
> +    handle_error(:error => error, :status => :forbidden,
> +                 :title => "Access denied")
> +  end
> +
> +  def handle_error(hash)
> +    msg = hash[:message] || hash[:error].message
> +    title = hash[:title] || "Internal Server Error"
> +    status = hash[:status] || :internal_server_error
> +    respond_to do |format|
> +      format.html do
> +        @title = title || "Something went very wrong"
> +        @errmsg = msg
> +        @ajax = params[:ajax]
> +        @nolayout = params[:nolayout]
> +        if @ajax
> +          render :template => 'layouts/popup-error', :layout => 'tabs-and-content'
> +        elsif @nolayout
> +          render :template => 'layouts/popup-error', :layout => 'help-and-content'
> +        else
> +          render :template => 'layouts/popup-error', :layout => 'popup'
> +        end
> +      end
> +      format.json do
> +        @json_hash ||= {}
> +        @json_hash[:success] = (status == :ok)
> +        @json_hash[:alert] = msg
> +        render :json => @json_hash
> +      end
> +      format.xml { head status }
> +    end
> +  end
> +
>    # don't define find_opts for array inputs
>    def json_hash(full_items, attributes, arg_list=[], find_opts={}, id_method=:id)
>      page = params[:page].to_i
>   
This looks good.
ACK.

We just need to figure out how we're handling the more general error 
types so we can properly handle actions that currently catch 
PermissionError _and_ others.

Scott




More information about the ovirt-devel mailing list