You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/02/10 13:55:59 UTC

Improved error handling / reporting in client

Hi,

This patch will add support for more detailed and verbose error
reporting to client.

I added all HTTP exceptions that may occur on server side and they
should be properly translated to client exceptions.

Basically there will be three kinds of exceptions:

ClientError -> 404 and friends
ServerError -> 50x
UnknownError -> anything else ;-)

For ServerError, in addition to the standard exception a backtrace from
the DC server will be printed to client console and client can also
check request parameters and current driver/provider handshake.

I hope this will help us improve debugging and make clients happy :-)

  -- Michal


Re: [PATCH core 2/2] Client: Added a better way how to handle client/server errors

Posted by Michal Fojtik <mf...@redhat.com>.
On Thursday, February 16, 2012 at 2:07 PM, marios@redhat.com wrote:
> ACK.
>  
> Can you add 's.has_rdoc' to the deltacloud-client.gemspec (couldn't
> build it without)
>  
> I played with the client errors, like client.realms("foo") - looks
> good. Can we do something about 401? I tried like:
>  
> client = DeltaCloud.new( "foo", "bar", "http://localhost:3001/api" )
> client.images
>  
>   
> but instead of 401 Auth Exception I got NoMethodError: undefined method
> `[]' for nil:NilClass


Yes, I hit this error too and I was not sure how or from where it come. Seems like
we're using something weird to report 401 error (need to check the server code)…

Anyway if you want to look into this issue I will be more than happy :)
  
>  
> ( I can take a look at this if you like - I think we might need to
> change something server side too... like with valid_credentials? )
>  
> marios
>  
>  
> On 10/02/12 14:56, mfojtik@redhat.com (mailto:mfojtik@redhat.com) wrote:
> > From: Michal Fojtik <mfojtik@redhat.com (mailto:mfojtik@redhat.com)>
> >  
> >  
> > Signed-off-by: Michal fojtik <mfojtik@redhat.com (mailto:mfojtik@redhat.com)>
> > ---
> > client/lib/deltacloud.rb | 67 +++++++++-----------------
> > client/lib/errors.rb | 111 +++++++++++++++++++++++++++++++++++++++++++
> > client/specs/errors_spec.rb | 59 +++++++++++++++++++++++
> > 3 files changed, 193 insertions(+), 44 deletions(-)
> > create mode 100644 client/lib/errors.rb
> > create mode 100644 client/specs/errors_spec.rb
> >  
> > diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
> > index 614eab2..97722de 100644
> > --- a/client/lib/deltacloud.rb
> > +++ b/client/lib/deltacloud.rb
> > @@ -21,6 +21,7 @@ require 'hwp_properties'
> > require 'instance_state'
> > require 'documentation'
> > require 'base_object'
> > +require 'errors'
> > require 'client_bucket_methods'
> >  
> > module DeltaCloud
> > @@ -314,9 +315,7 @@ module DeltaCloud
> >  
> > request(:post, entry_points[:"#{$1}s"], {}, params) do |response|
> > obj = base_object(:"#{$1}", response)
> > - # All create calls must respond 201 HTTP code
> > - # to indicate that resource was created.
> > - handle_backend_error(response) if response.code!=201
> > + response_error(response) unless response_successfull?(response.code)
> > yield obj if block_given?
> > end
> > return obj
> > @@ -349,6 +348,23 @@ module DeltaCloud
> > headers
> > end
> >  
> > + def response_successfull?(code)
> > + return true if code.to_s =~ /^2(\d{2})$/
> > + return true if code.to_s =~ /^3(\d{2})$/
> > + return false
> > + end
> > +
> > + def response_error(response)
> > + xml = Nokogiri::XML(response.to_s)
> > + opts = response.code.to_s =~ /^5(\d{2})$/ ? {
> > + :driver => (xml/'backend').first[:driver],
> > + :provider => (xml/'backend').first[:provider],
> > + :params => (xml/'request/param').inject({}) { |r,p| r[:"#{p[:name]}"] = p.text; r }
> > + } : {}
> > + backtrace = (xml/'backtrace').empty? ? nil : (xml/'backtrace').first.text.split("\n")[1..10].map { |l| l.strip }
> > + DeltaCloud::HTTPError.raise_error(xml.root[:status] || response.code, (xml/'message').first.text, opts, backtrace)
> > + end
> > +
> > # Basic request method
> > #
> > def request(*args, &block)
> > @@ -367,55 +383,18 @@ module DeltaCloud
> > if conf[:method].eql?(:post)
> > resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout], :timeout => conf[:timeout])
> > resource.send(:post, conf[:form_data], default_headers.merge(extended_headers)) do |response, request, block|
> > - handle_backend_error(response) if [500, 502, 501, 401, 504].include? response.code
> > - if response.respond_to?('body')
> > - yield response.body if block_given?
> > - else
> > - yield response.to_s if block_given?
> > - end
> > + response_error(response) unless response_successfull? response.code
> > + yield response.to_s
> > end
> > else
> > resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout], :timeout => conf[:timeout])
> > resource.send(conf[:method], default_headers.merge(extended_headers)) do |response, request, block|
> > - handle_backend_error(response) if [500, 502, 501, 504, 401].include? response.code
> > - if conf[:method].eql?(:get) and [301, 302, 307].include? response.code
> > - response.follow_redirection(request) do |response, request, block|
> > - if response.respond_to?('body')
> > - yield response.body if block_given?
> > - else
> > - yield response.to_s if block_given?
> > - end
> > - end
> > - else
> > - if response.respond_to?('body')
> > - yield response.body if block_given?
> > - else
> > - yield response.to_s if block_given?
> > - end
> > - end
> > + response_error(response) unless response_successfull? response.code
> > + yield response.to_s
> > end
> > end
> > end
> >  
> > - # Re-raise backend errors as on exception in client with message from
> > - # backend
> > - class BackendError < StandardError
> > -
> > - def initialize(opts={})
> > - opts[:message] = "Not authorized / Invalid credentials" if opts[:code] == 401
> > - super("#{opts[:code]} : #{opts[:message]}")
> > - set_backtrace(opts[:backtrace].split("\n").map { |l| l.strip }[0..10]) if opts[:backtrace]
> > - end
> > -
> > - end
> > -
> > - def handle_backend_error(response)
> > - response_xml = Nokogiri::XML(response)
> > - backtrace = (response_xml/'error/backtrace').empty? ? nil : (response_xml/'error/backtrace').text
> > - raise BackendError.new(:message => (response_xml/'error/message').text,
> > - :code => response.code,
> > - :backtrace => backtrace)
> > - end
> >  
> > # Check if specified collection have wanted feature
> > def feature?(collection, name)
> > diff --git a/client/lib/errors.rb b/client/lib/errors.rb
> > new file mode 100644
> > index 0000000..60d59e1
> > --- /dev/null
> > +++ b/client/lib/errors.rb
> > @@ -0,0 +1,111 @@
> > +# Licensed to the Apache Software Foundation (ASF) under one or more
> > +# contributor license agreements. See the NOTICE file distributed with
> > +# this work for additional information regarding copyright ownership. The
> > +# ASF licenses this file to you under the Apache License, Version 2.0 (the
> > +# "License"); you may not use this file except in compliance with the
> > +# License. You may obtain a copy of the License at
> > +#
> > +# http://www.apache.org/licenses/LICENSE-2.0
> > +#
> > +# Unless required by applicable law or agreed to in writing, software
> > +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> > +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
> > +# License for the specific language governing permissions and limitations
> > +# under the License.
> > +
> > +module DeltaCloud
> > + module HTTPError
> > +
> > + class ClientError < StandardError
> > +
> > + attr_reader :params, :driver, :provider
> > +
> > + def initialize(code, message, opts={}, backtrace=nil)
> > + @params, @driver, @provider = opts[:params], opts[:driver], opts[:provider]
> > + if code.to_s =~ /^5(\d{2})/
> > + message += "\nParameters: #{@params.inspect}\n"
> > + message += "Driver: #{@driver}@#{@provider}"
> > + end
> > + super("#{code}\n\n#{self.class.superclass}: #{message}\n\n")
> > + # If server provided us the backtrace, then replace client backtrace
> > + # with the server one.
> > + set_backtrace(backtrace) unless backtrace.nil?
> > + end
> > + end
> > +
> > + class ServerError < ClientError; end
> > + class UknownError < ClientError; end
> > +
> > + # For sake of consistent documentation we need to create
> > + # this exceptions manually, instead of using some meta-programming.
> > + # Client will really appreciate this it will try to catch some
> > + # specific exception.
> > +
> > + # Client errors (4xx)
> > + class BadRequest < ClientError; end
> > + class Unauthorized < ClientError; end
> > + class Forbidden < ClientError; end
> > + class NotFound < ClientError; end
> > + class MethodNotAllowed < ClientError; end
> > + class NotAcceptable < ClientError; end
> > + class RequestTimeout < ClientError; end
> > + class Gone < ClientError; end
> > + class ExpectationFailed < ClientError; end
> > + class UnsupportedMediaType < ClientError; end
> > +
> > + # Server errors (5xx)
> > + class DeltacloudError < ServerError; end
> > + class ProviderError < ServerError; end
> > + class ProviderTimeout < ServerError; end
> > + class ServiceUnavailable < ServerError; end
> > + class NotImplemented < ServerError; end
> > +
> > + class ExceptionHandler
> > +
> > + attr_reader :http_status_code, :message, :trace
> > +
> > + def initialize(status_code, message, opts={}, backtrace=nil, &block)
> > + @http_status_code = status_code.to_i
> > + @trace = backtrace
> > + @message = message
> > + @options = opts
> > + instance_eval(&block) if block_given?
> > + end
> > +
> > + def on(code, exception_class)
> > + if code == @http_status_code
> > + raise exception_class.new(code, @message, @options, @trace)
> > + end
> > + end
> > +
> > + end
> > +
> > + def self.parse_response_error(response)
> > +  
> > + end
> > +
> > + def self.raise_error(code, message, opts={}, backtrace=nil)
> > + ExceptionHandler.new(code, message, opts, backtrace) do
> > + # Client errors
> > + on 400, BadRequest
> > + on 401, Unauthorized
> > + on 403, Forbidden
> > + on 404, NotFound
> > + on 405, MethodNotAllowed
> > + on 406, NotAcceptable
> > + on 408, RequestTimeout
> > + on 410, Gone
> > + on 415, UnsupportedMediaType
> > + on 417, ExpectationFailed
> > + # Server errors
> > + on 500, DeltacloudError
> > + on 501, NotImplemented
> > + on 502, ProviderError
> > + on 503, ServiceUnavailable
> > + on 504, ProviderTimeout
> > + end
> > + raise Deltacloud::HTTPError::UnknownError.new(code, message, opts, backtrace)
> > + end
> > +
> > + end
> > +end
> > diff --git a/client/specs/errors_spec.rb b/client/specs/errors_spec.rb
> > new file mode 100644
> > index 0000000..031e3b7
> > --- /dev/null
> > +++ b/client/specs/errors_spec.rb
> > @@ -0,0 +1,59 @@
> > +#
> > +# Copyright (C) 2009-2011 Red Hat, Inc.
> > +#
> > +# Licensed to the Apache Software Foundation (ASF) under one or more
> > +# contributor license agreements. See the NOTICE file distributed with
> > +# this work for additional information regarding copyright ownership. The
> > +# ASF licenses this file to you under the Apache License, Version 2.0 (the
> > +# "License"); you may not use this file except in compliance with the
> > +# License. You may obtain a copy of the License at
> > +#
> > +# http://www.apache.org/licenses/LICENSE-2.0
> > +#
> > +# Unless required by applicable law or agreed to in writing, software
> > +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> > +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
> > +# License for the specific language governing permissions and limitations
> > +# under the License.
> > +
> > +require 'specs/spec_helper'
> > +
> > +describe "server error handler" do
> > +
> > + it_should_behave_like "all resources"
> > +
> > + it 'should capture HTTP 500 error as DeltacloudError' do
> > + DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> > + expect { client.realm('500') }.should raise_error(DeltaCloud::HTTPError::DeltacloudError)
> > + end
> > + end
> > +
> > + it 'should capture HTTP 502 error as ProviderError' do
> > + DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> > + expect { client.realm('502') }.should raise_error(DeltaCloud::HTTPError::ProviderError)
> > + end
> > + end
> > +
> > + it 'should capture HTTP 501 error as NotImplemented' do
> > + DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> > + expect { client.realm('501') }.should raise_error(DeltaCloud::HTTPError::NotImplemented)
> > + end
> > + end
> > +
> > + it 'should capture HTTP 504 error as ProviderTimeout' do
> > + DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> > + expect { client.realm('504') }.should raise_error(DeltaCloud::HTTPError::ProviderTimeout)
> > + end
> > + end
> > +
> > +end
> > +
> > +describe "client error handler" do
> > +
> > + it 'should capture HTTP 404 error as NotFound' do
> > + DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> > + expect { client.realm('non-existing-realm') }.should raise_error(DeltaCloud::HTTPError::NotFound)
> > + end
> > + end
> > +
> > +end
>  




Re: [PATCH core 2/2] Client: Added a better way how to handle client/server errors

Posted by "marios@redhat.com" <ma...@redhat.com>.
ACK.

Can you add 's.has_rdoc' to the deltacloud-client.gemspec (couldn't
build it without)

I played with the client errors, like client.realms("foo")  - looks
good. Can we do something about 401? I tried like:

client = DeltaCloud.new( "foo", "bar", "http://localhost:3001/api" )
client.images

but instead of 401 Auth Exception I got NoMethodError: undefined method
`[]' for nil:NilClass

( I can take a look at this if you like - I think we might need to
change something server side too... like with valid_credentials? )

marios


On 10/02/12 14:56, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  client/lib/deltacloud.rb    |   67 +++++++++-----------------
>  client/lib/errors.rb        |  111 +++++++++++++++++++++++++++++++++++++++++++
>  client/specs/errors_spec.rb |   59 +++++++++++++++++++++++
>  3 files changed, 193 insertions(+), 44 deletions(-)
>  create mode 100644 client/lib/errors.rb
>  create mode 100644 client/specs/errors_spec.rb
> 
> diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
> index 614eab2..97722de 100644
> --- a/client/lib/deltacloud.rb
> +++ b/client/lib/deltacloud.rb
> @@ -21,6 +21,7 @@ require 'hwp_properties'
>  require 'instance_state'
>  require 'documentation'
>  require 'base_object'
> +require 'errors'
>  require 'client_bucket_methods'
>  
>  module DeltaCloud
> @@ -314,9 +315,7 @@ module DeltaCloud
>  
>          request(:post, entry_points[:"#{$1}s"], {}, params) do |response|
>            obj = base_object(:"#{$1}", response)
> -          # All create calls must respond 201 HTTP code
> -          # to indicate that resource was created.
> -          handle_backend_error(response) if response.code!=201
> +          response_error(response) unless response_successfull?(response.code)
>            yield obj if block_given?
>          end
>          return obj
> @@ -349,6 +348,23 @@ module DeltaCloud
>        headers
>      end
>  
> +    def response_successfull?(code)
> +      return true if code.to_s =~ /^2(\d{2})$/
> +      return true if code.to_s =~ /^3(\d{2})$/
> +      return false
> +    end
> +
> +    def response_error(response)
> +      xml = Nokogiri::XML(response.to_s)
> +      opts = response.code.to_s =~ /^5(\d{2})$/ ? {
> +        :driver => (xml/'backend').first[:driver],
> +        :provider => (xml/'backend').first[:provider],
> +        :params => (xml/'request/param').inject({}) { |r,p| r[:"#{p[:name]}"] = p.text; r }
> +      } : {}
> +      backtrace = (xml/'backtrace').empty? ? nil : (xml/'backtrace').first.text.split("\n")[1..10].map { |l| l.strip }
> +      DeltaCloud::HTTPError.raise_error(xml.root[:status] || response.code, (xml/'message').first.text, opts, backtrace)
> +    end
> +
>      # Basic request method
>      #
>      def request(*args, &block)
> @@ -367,55 +383,18 @@ module DeltaCloud
>        if conf[:method].eql?(:post)
>          resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout], :timeout => conf[:timeout])
>          resource.send(:post, conf[:form_data], default_headers.merge(extended_headers)) do |response, request, block|
> -          handle_backend_error(response) if [500, 502, 501, 401, 504].include? response.code
> -          if response.respond_to?('body')
> -            yield response.body if block_given?
> -          else
> -            yield response.to_s if block_given?
> -          end
> +          response_error(response) unless response_successfull? response.code
> +          yield response.to_s
>          end
>        else
>          resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout], :timeout => conf[:timeout])
>          resource.send(conf[:method], default_headers.merge(extended_headers)) do |response, request, block|
> -          handle_backend_error(response) if [500, 502, 501, 504, 401].include? response.code
> -          if conf[:method].eql?(:get) and [301, 302, 307].include? response.code
> -            response.follow_redirection(request) do |response, request, block|
> -              if response.respond_to?('body')
> -                yield response.body if block_given?
> -              else
> -                yield response.to_s if block_given?
> -              end
> -            end
> -          else
> -            if response.respond_to?('body')
> -              yield response.body if block_given?
> -            else
> -              yield response.to_s if block_given?
> -            end
> -          end
> +          response_error(response) unless response_successfull? response.code
> +          yield response.to_s
>          end
>        end
>      end
>  
> -    # Re-raise backend errors as on exception in client with message from
> -    # backend
> -    class BackendError < StandardError
> -
> -      def initialize(opts={})
> -        opts[:message] = "Not authorized / Invalid credentials" if opts[:code] == 401
> -        super("#{opts[:code]} : #{opts[:message]}")
> -        set_backtrace(opts[:backtrace].split("\n").map { |l| l.strip }[0..10]) if opts[:backtrace]
> -      end
> -
> -    end
> -
> -    def handle_backend_error(response)
> -      response_xml = Nokogiri::XML(response)
> -      backtrace = (response_xml/'error/backtrace').empty? ? nil : (response_xml/'error/backtrace').text
> -      raise BackendError.new(:message => (response_xml/'error/message').text,
> -                             :code => response.code,
> -                             :backtrace => backtrace)
> -    end
>  
>      # Check if specified collection have wanted feature
>      def feature?(collection, name)
> diff --git a/client/lib/errors.rb b/client/lib/errors.rb
> new file mode 100644
> index 0000000..60d59e1
> --- /dev/null
> +++ b/client/lib/errors.rb
> @@ -0,0 +1,111 @@
> +# Licensed to the Apache Software Foundation (ASF) under one or more
> +# contributor license agreements.  See the NOTICE file distributed with
> +# this work for additional information regarding copyright ownership.  The
> +# ASF licenses this file to you under the Apache License, Version 2.0 (the
> +# "License"); you may not use this file except in compliance with the
> +# License.  You may obtain a copy of the License at
> +#
> +#       http://www.apache.org/licenses/LICENSE-2.0
> +#
> +# Unless required by applicable law or agreed to in writing, software
> +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
> +# License for the specific language governing permissions and limitations
> +# under the License.
> +
> +module DeltaCloud
> +  module HTTPError
> +
> +    class ClientError < StandardError
> +
> +      attr_reader :params, :driver, :provider
> +
> +      def initialize(code, message, opts={}, backtrace=nil)
> +        @params, @driver, @provider = opts[:params], opts[:driver], opts[:provider]
> +        if code.to_s =~ /^5(\d{2})/
> +          message += "\nParameters: #{@params.inspect}\n"
> +          message += "Driver: #{@driver}@#{@provider}"
> +        end
> +        super("#{code}\n\n#{self.class.superclass}: #{message}\n\n")
> +        # If server provided us the backtrace, then replace client backtrace
> +        # with the server one.
> +        set_backtrace(backtrace) unless backtrace.nil?
> +      end
> +    end
> +
> +    class ServerError < ClientError; end
> +    class UknownError < ClientError; end
> +
> +    # For sake of consistent documentation we need to create
> +    # this exceptions manually, instead of using some meta-programming.
> +    # Client will really appreciate this it will try to catch some
> +    # specific exception.
> +
> +    # Client errors (4xx)
> +    class BadRequest < ClientError; end
> +    class Unauthorized < ClientError; end
> +    class Forbidden < ClientError; end
> +    class NotFound < ClientError; end
> +    class MethodNotAllowed < ClientError; end
> +    class NotAcceptable < ClientError; end
> +    class RequestTimeout < ClientError; end
> +    class Gone < ClientError; end
> +    class ExpectationFailed < ClientError; end
> +    class UnsupportedMediaType < ClientError; end
> +
> +    # Server errors (5xx)
> +    class DeltacloudError < ServerError; end
> +    class ProviderError < ServerError; end
> +    class ProviderTimeout < ServerError; end
> +    class ServiceUnavailable < ServerError; end
> +    class NotImplemented < ServerError; end
> +
> +    class ExceptionHandler
> +
> +      attr_reader :http_status_code, :message, :trace
> +
> +      def initialize(status_code, message, opts={}, backtrace=nil, &block)
> +        @http_status_code = status_code.to_i
> +        @trace = backtrace
> +        @message = message
> +        @options = opts
> +        instance_eval(&block) if block_given?
> +      end
> +
> +      def on(code, exception_class)
> +        if code == @http_status_code
> +          raise exception_class.new(code, @message, @options, @trace)
> +        end
> +      end
> +
> +    end
> +
> +    def self.parse_response_error(response)
> +    
> +    end
> +
> +    def self.raise_error(code, message, opts={}, backtrace=nil)
> +      ExceptionHandler.new(code, message, opts, backtrace) do
> +        # Client errors
> +        on 400, BadRequest
> +        on 401, Unauthorized
> +        on 403, Forbidden
> +        on 404, NotFound
> +        on 405, MethodNotAllowed
> +        on 406, NotAcceptable
> +        on 408, RequestTimeout
> +        on 410, Gone
> +        on 415, UnsupportedMediaType
> +        on 417, ExpectationFailed
> +        # Server errors
> +        on 500, DeltacloudError
> +        on 501, NotImplemented
> +        on 502, ProviderError
> +        on 503, ServiceUnavailable
> +        on 504, ProviderTimeout
> +      end
> +      raise Deltacloud::HTTPError::UnknownError.new(code, message, opts, backtrace)
> +    end
> +
> +  end
> +end
> diff --git a/client/specs/errors_spec.rb b/client/specs/errors_spec.rb
> new file mode 100644
> index 0000000..031e3b7
> --- /dev/null
> +++ b/client/specs/errors_spec.rb
> @@ -0,0 +1,59 @@
> +#
> +# Copyright (C) 2009-2011  Red Hat, Inc.
> +#
> +# Licensed to the Apache Software Foundation (ASF) under one or more
> +# contributor license agreements.  See the NOTICE file distributed with
> +# this work for additional information regarding copyright ownership.  The
> +# ASF licenses this file to you under the Apache License, Version 2.0 (the
> +# "License"); you may not use this file except in compliance with the
> +# License.  You may obtain a copy of the License at
> +#
> +#       http://www.apache.org/licenses/LICENSE-2.0
> +#
> +# Unless required by applicable law or agreed to in writing, software
> +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
> +# License for the specific language governing permissions and limitations
> +# under the License.
> +
> +require 'specs/spec_helper'
> +
> +describe "server error handler" do
> +
> +  it_should_behave_like "all resources"
> +
> +  it 'should capture HTTP 500 error as DeltacloudError' do
> +    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> +      expect { client.realm('500') }.should raise_error(DeltaCloud::HTTPError::DeltacloudError)
> +    end
> +  end
> +
> +  it 'should capture HTTP 502 error as ProviderError' do
> +    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> +      expect { client.realm('502') }.should raise_error(DeltaCloud::HTTPError::ProviderError)
> +    end
> +  end
> +
> +  it 'should capture HTTP 501 error as NotImplemented' do
> +    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> +      expect { client.realm('501') }.should raise_error(DeltaCloud::HTTPError::NotImplemented)
> +    end
> +  end
> +
> +  it 'should capture HTTP 504 error as ProviderTimeout' do
> +    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> +      expect { client.realm('504') }.should raise_error(DeltaCloud::HTTPError::ProviderTimeout)
> +    end
> +  end
> +
> +end
> +
> +describe "client error handler" do
> +
> +  it 'should capture HTTP 404 error as NotFound' do
> +    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
> +      expect { client.realm('non-existing-realm') }.should raise_error(DeltaCloud::HTTPError::NotFound)
> +    end
> +  end
> +
> +end


[PATCH core 2/2] Client: Added a better way how to handle client/server errors

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 client/lib/deltacloud.rb    |   67 +++++++++-----------------
 client/lib/errors.rb        |  111 +++++++++++++++++++++++++++++++++++++++++++
 client/specs/errors_spec.rb |   59 +++++++++++++++++++++++
 3 files changed, 193 insertions(+), 44 deletions(-)
 create mode 100644 client/lib/errors.rb
 create mode 100644 client/specs/errors_spec.rb

diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index 614eab2..97722de 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -21,6 +21,7 @@ require 'hwp_properties'
 require 'instance_state'
 require 'documentation'
 require 'base_object'
+require 'errors'
 require 'client_bucket_methods'
 
 module DeltaCloud
@@ -314,9 +315,7 @@ module DeltaCloud
 
         request(:post, entry_points[:"#{$1}s"], {}, params) do |response|
           obj = base_object(:"#{$1}", response)
-          # All create calls must respond 201 HTTP code
-          # to indicate that resource was created.
-          handle_backend_error(response) if response.code!=201
+          response_error(response) unless response_successfull?(response.code)
           yield obj if block_given?
         end
         return obj
@@ -349,6 +348,23 @@ module DeltaCloud
       headers
     end
 
+    def response_successfull?(code)
+      return true if code.to_s =~ /^2(\d{2})$/
+      return true if code.to_s =~ /^3(\d{2})$/
+      return false
+    end
+
+    def response_error(response)
+      xml = Nokogiri::XML(response.to_s)
+      opts = response.code.to_s =~ /^5(\d{2})$/ ? {
+        :driver => (xml/'backend').first[:driver],
+        :provider => (xml/'backend').first[:provider],
+        :params => (xml/'request/param').inject({}) { |r,p| r[:"#{p[:name]}"] = p.text; r }
+      } : {}
+      backtrace = (xml/'backtrace').empty? ? nil : (xml/'backtrace').first.text.split("\n")[1..10].map { |l| l.strip }
+      DeltaCloud::HTTPError.raise_error(xml.root[:status] || response.code, (xml/'message').first.text, opts, backtrace)
+    end
+
     # Basic request method
     #
     def request(*args, &block)
@@ -367,55 +383,18 @@ module DeltaCloud
       if conf[:method].eql?(:post)
         resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout], :timeout => conf[:timeout])
         resource.send(:post, conf[:form_data], default_headers.merge(extended_headers)) do |response, request, block|
-          handle_backend_error(response) if [500, 502, 501, 401, 504].include? response.code
-          if response.respond_to?('body')
-            yield response.body if block_given?
-          else
-            yield response.to_s if block_given?
-          end
+          response_error(response) unless response_successfull? response.code
+          yield response.to_s
         end
       else
         resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout], :timeout => conf[:timeout])
         resource.send(conf[:method], default_headers.merge(extended_headers)) do |response, request, block|
-          handle_backend_error(response) if [500, 502, 501, 504, 401].include? response.code
-          if conf[:method].eql?(:get) and [301, 302, 307].include? response.code
-            response.follow_redirection(request) do |response, request, block|
-              if response.respond_to?('body')
-                yield response.body if block_given?
-              else
-                yield response.to_s if block_given?
-              end
-            end
-          else
-            if response.respond_to?('body')
-              yield response.body if block_given?
-            else
-              yield response.to_s if block_given?
-            end
-          end
+          response_error(response) unless response_successfull? response.code
+          yield response.to_s
         end
       end
     end
 
-    # Re-raise backend errors as on exception in client with message from
-    # backend
-    class BackendError < StandardError
-
-      def initialize(opts={})
-        opts[:message] = "Not authorized / Invalid credentials" if opts[:code] == 401
-        super("#{opts[:code]} : #{opts[:message]}")
-        set_backtrace(opts[:backtrace].split("\n").map { |l| l.strip }[0..10]) if opts[:backtrace]
-      end
-
-    end
-
-    def handle_backend_error(response)
-      response_xml = Nokogiri::XML(response)
-      backtrace = (response_xml/'error/backtrace').empty? ? nil : (response_xml/'error/backtrace').text
-      raise BackendError.new(:message => (response_xml/'error/message').text,
-                             :code => response.code,
-                             :backtrace => backtrace)
-    end
 
     # Check if specified collection have wanted feature
     def feature?(collection, name)
diff --git a/client/lib/errors.rb b/client/lib/errors.rb
new file mode 100644
index 0000000..60d59e1
--- /dev/null
+++ b/client/lib/errors.rb
@@ -0,0 +1,111 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+module DeltaCloud
+  module HTTPError
+
+    class ClientError < StandardError
+
+      attr_reader :params, :driver, :provider
+
+      def initialize(code, message, opts={}, backtrace=nil)
+        @params, @driver, @provider = opts[:params], opts[:driver], opts[:provider]
+        if code.to_s =~ /^5(\d{2})/
+          message += "\nParameters: #{@params.inspect}\n"
+          message += "Driver: #{@driver}@#{@provider}"
+        end
+        super("#{code}\n\n#{self.class.superclass}: #{message}\n\n")
+        # If server provided us the backtrace, then replace client backtrace
+        # with the server one.
+        set_backtrace(backtrace) unless backtrace.nil?
+      end
+    end
+
+    class ServerError < ClientError; end
+    class UknownError < ClientError; end
+
+    # For sake of consistent documentation we need to create
+    # this exceptions manually, instead of using some meta-programming.
+    # Client will really appreciate this it will try to catch some
+    # specific exception.
+
+    # Client errors (4xx)
+    class BadRequest < ClientError; end
+    class Unauthorized < ClientError; end
+    class Forbidden < ClientError; end
+    class NotFound < ClientError; end
+    class MethodNotAllowed < ClientError; end
+    class NotAcceptable < ClientError; end
+    class RequestTimeout < ClientError; end
+    class Gone < ClientError; end
+    class ExpectationFailed < ClientError; end
+    class UnsupportedMediaType < ClientError; end
+
+    # Server errors (5xx)
+    class DeltacloudError < ServerError; end
+    class ProviderError < ServerError; end
+    class ProviderTimeout < ServerError; end
+    class ServiceUnavailable < ServerError; end
+    class NotImplemented < ServerError; end
+
+    class ExceptionHandler
+
+      attr_reader :http_status_code, :message, :trace
+
+      def initialize(status_code, message, opts={}, backtrace=nil, &block)
+        @http_status_code = status_code.to_i
+        @trace = backtrace
+        @message = message
+        @options = opts
+        instance_eval(&block) if block_given?
+      end
+
+      def on(code, exception_class)
+        if code == @http_status_code
+          raise exception_class.new(code, @message, @options, @trace)
+        end
+      end
+
+    end
+
+    def self.parse_response_error(response)
+    
+    end
+
+    def self.raise_error(code, message, opts={}, backtrace=nil)
+      ExceptionHandler.new(code, message, opts, backtrace) do
+        # Client errors
+        on 400, BadRequest
+        on 401, Unauthorized
+        on 403, Forbidden
+        on 404, NotFound
+        on 405, MethodNotAllowed
+        on 406, NotAcceptable
+        on 408, RequestTimeout
+        on 410, Gone
+        on 415, UnsupportedMediaType
+        on 417, ExpectationFailed
+        # Server errors
+        on 500, DeltacloudError
+        on 501, NotImplemented
+        on 502, ProviderError
+        on 503, ServiceUnavailable
+        on 504, ProviderTimeout
+      end
+      raise Deltacloud::HTTPError::UnknownError.new(code, message, opts, backtrace)
+    end
+
+  end
+end
diff --git a/client/specs/errors_spec.rb b/client/specs/errors_spec.rb
new file mode 100644
index 0000000..031e3b7
--- /dev/null
+++ b/client/specs/errors_spec.rb
@@ -0,0 +1,59 @@
+#
+# Copyright (C) 2009-2011  Red Hat, Inc.
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+require 'specs/spec_helper'
+
+describe "server error handler" do
+
+  it_should_behave_like "all resources"
+
+  it 'should capture HTTP 500 error as DeltacloudError' do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      expect { client.realm('500') }.should raise_error(DeltaCloud::HTTPError::DeltacloudError)
+    end
+  end
+
+  it 'should capture HTTP 502 error as ProviderError' do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      expect { client.realm('502') }.should raise_error(DeltaCloud::HTTPError::ProviderError)
+    end
+  end
+
+  it 'should capture HTTP 501 error as NotImplemented' do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      expect { client.realm('501') }.should raise_error(DeltaCloud::HTTPError::NotImplemented)
+    end
+  end
+
+  it 'should capture HTTP 504 error as ProviderTimeout' do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      expect { client.realm('504') }.should raise_error(DeltaCloud::HTTPError::ProviderTimeout)
+    end
+  end
+
+end
+
+describe "client error handler" do
+
+  it 'should capture HTTP 404 error as NotFound' do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      expect { client.realm('non-existing-realm') }.should raise_error(DeltaCloud::HTTPError::NotFound)
+    end
+  end
+
+end
-- 
1.7.4.4


Re: [PATCH core 1/2] Core: Added XML output for 504 and 502 errors

Posted by Michal Fojtik <mf...@redhat.com>.
On Thursday, February 16, 2012 at 2:02 PM, marios@redhat.com wrote:
> ACK, couple thoughts:
> 
> On 10/02/12 14:56, mfojtik@redhat.com (mailto:mfojtik@redhat.com) wrote:
> > From: Michal Fojtik <mfojtik@redhat.com (mailto:mfojtik@redhat.com)>
> > 
> > 
> > Signed-off-by: Michal fojtik <mfojtik@redhat.com (mailto:mfojtik@redhat.com)>
> > ---
> > server/lib/deltacloud/base_driver/exceptions.rb | 16 +++++++
> > server/lib/deltacloud/drivers/mock/mock_driver.rb | 32 +++++++++++++-
> > .../lib/deltacloud/helpers/application_helper.rb | 2 +-
> > server/lib/sinatra/lazy_auth.rb | 2 +-
> > server/views/errors/501.html.haml | 43 ++++++++++++++++++++
> > server/views/errors/501.xml.haml | 12 +++++
> > server/views/errors/502.xml.haml | 13 ++++--
> > server/views/errors/504.html.haml | 43 ++++++++++++++++++++
> > server/views/errors/504.xml.haml | 12 +++++
> 
> 
> 
> we really need all of these? like 501.html.haml 504.html.haml are
> identical, as are 501.xml.haml 502.xml.haml and 504.xml.haml. If we
> really want seperate files for 'neatness' perhaps just include them in
> each other? just thinking in terms of maintaining if we want to change
> something its gonna be messy.


I agree. We should probably create one generic error template and then just render
them in this files (or change the error reporting code completely for this).

Will do it in next revision.
> One more thought below:
> 
> > diff --git a/server/lib/deltacloud/base_driver/exceptions.rb b/server/lib/deltacloud/base_driver/exceptions.rb
> > index e44c89b..9effe29 100644
> > --- a/server/lib/deltacloud/base_driver/exceptions.rb
> > 
> > def realms(credentials, opts=nil)
> > - return REALMS if ( opts.nil? )
> > - results = REALMS
> > + check_credentials( credentials )
> > + results = []
> > + safely do
> > + # This hack is used to test if client capture exceptions correctly
> > + # To raise an exception do GET /api/realms/50[0-2]
> > + raise "DeltacloudErrorTest" if opts and opts[:id] == "500"
> > + raise "NotImplementedTest" if opts and opts[:id] == "501"
> > + raise "ProviderErrorTest" if opts and opts[:id] == "502"
> > + raise "ProviderTimeoutTest" if opts and opts[:id] == "504"
> > + return REALMS if ( opts.nil? )
> > + results = REALMS
> > + end
> > results = filter_on( results, :id, opts )
> > results
> 
> 
> 
> you want to keep this stuff here? I saw that you are using them in
> client/specs/error_spec but feels a bit too 'hacky' - maybe define some
> special method just for this?


Well, yes they are hacky :-) But on the other side this is the most easy way
how to test/trigger exception handling in server/client.

This could be also extra useful for conductor guys to test their exception handling
code.

I was thinking about special method but that would need to be integrated to Rabbit
which can be a mess. I think we should keep this code there for now (to make our
client tests happy) and properly document this as a feature (exception testing.)

 -- michal

Re: [PATCH core 1/2] Core: Added XML output for 504 and 502 errors

Posted by "marios@redhat.com" <ma...@redhat.com>.
ACK, couple thoughts:

On 10/02/12 14:56, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/lib/deltacloud/base_driver/exceptions.rb    |   16 +++++++
>  server/lib/deltacloud/drivers/mock/mock_driver.rb  |   32 +++++++++++++-
>  .../lib/deltacloud/helpers/application_helper.rb   |    2 +-
>  server/lib/sinatra/lazy_auth.rb                    |    2 +-
>  server/views/errors/501.html.haml                  |   43 ++++++++++++++++++++
>  server/views/errors/501.xml.haml                   |   12 +++++
>  server/views/errors/502.xml.haml                   |   13 ++++--
>  server/views/errors/504.html.haml                  |   43 ++++++++++++++++++++
>  server/views/errors/504.xml.haml                   |   12 +++++

we really need all of these? like 501.html.haml 504.html.haml are
identical, as are 501.xml.haml 502.xml.haml and 504.xml.haml. If we
really want seperate files for 'neatness' perhaps just include them in
each other? just thinking in terms of maintaining if we want to change
something its gonna be messy.

One more thought below:

> diff --git a/server/lib/deltacloud/base_driver/exceptions.rb b/server/lib/deltacloud/base_driver/exceptions.rb
> index e44c89b..9effe29 100644
> --- a/server/lib/deltacloud/base_driver/exceptions.rb
>  
>      def realms(credentials, opts=nil)
> -      return REALMS if ( opts.nil? )
> -      results = REALMS
> +      check_credentials( credentials )
> +      results = []
> +      safely do
> +        # This hack is used to test if client capture exceptions correctly
> +        # To raise an exception do GET /api/realms/50[0-2]
> +        raise "DeltacloudErrorTest" if opts and opts[:id] == "500"
> +        raise "NotImplementedTest" if opts and opts[:id] == "501"
> +        raise "ProviderErrorTest" if opts and opts[:id] == "502"
> +        raise "ProviderTimeoutTest" if opts and opts[:id] == "504"
> +        return REALMS if ( opts.nil? )
> +        results = REALMS
> +      end
>        results = filter_on( results, :id, opts )
>        results

you want to keep this stuff here? I saw that you are using them in
client/specs/error_spec but feels a bit too 'hacky' - maybe define some
special method just for this?

marios



[PATCH core 1/2] Core: Added XML output for 504 and 502 errors

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/base_driver/exceptions.rb    |   16 +++++++
 server/lib/deltacloud/drivers/mock/mock_driver.rb  |   32 +++++++++++++-
 .../lib/deltacloud/helpers/application_helper.rb   |    2 +-
 server/lib/sinatra/lazy_auth.rb                    |    2 +-
 server/views/errors/501.html.haml                  |   43 ++++++++++++++++++++
 server/views/errors/501.xml.haml                   |   12 +++++
 server/views/errors/502.xml.haml                   |   13 ++++--
 server/views/errors/504.html.haml                  |   43 ++++++++++++++++++++
 server/views/errors/504.xml.haml                   |   12 +++++
 9 files changed, 166 insertions(+), 9 deletions(-)
 create mode 100644 server/views/errors/501.html.haml
 create mode 100644 server/views/errors/501.xml.haml
 create mode 100644 server/views/errors/504.html.haml
 create mode 100644 server/views/errors/504.xml.haml

diff --git a/server/lib/deltacloud/base_driver/exceptions.rb b/server/lib/deltacloud/base_driver/exceptions.rb
index e44c89b..9effe29 100644
--- a/server/lib/deltacloud/base_driver/exceptions.rb
+++ b/server/lib/deltacloud/base_driver/exceptions.rb
@@ -72,6 +72,20 @@ module Deltacloud
       end
     end
 
+    class ProviderTimeout < DeltacloudException
+      def initialize(e, message)
+        message ||= e.message
+        super(504, e.class.name, message, e.backtrace)
+      end
+    end
+
+    class NotImplemented < DeltacloudException
+      def initialize(e, message)
+        message ||= e.message
+        super(501, e.class.name, message, e.backtrace)
+      end
+    end
+
     class ObjectNotFound < DeltacloudException
       def initialize(e, message)
         message ||= e.message
@@ -121,7 +135,9 @@ module Deltacloud
           when 405 then Deltacloud::ExceptionHandler::MethodNotAllowed.new(e, @message)
           when 400 then Deltacloud::ExceptionHandler::ValidationFailure.new(e, @message)
           when 500 then Deltacloud::ExceptionHandler::BackendError.new(e, @message)
+          when 501 then Deltacloud::ExceptionHandler::NotImplemented.new(e, @message)
           when 502 then Deltacloud::ExceptionHandler::ProviderError.new(e, @message)
+          when 504 then Deltacloud::ExceptionHandler::ProviderTimeout.new(e, @message)
         end
       end
 
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index 1f36b36..c0dfd2e 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -100,8 +100,18 @@ module Deltacloud::Drivers::Mock
     end
 
     def realms(credentials, opts=nil)
-      return REALMS if ( opts.nil? )
-      results = REALMS
+      check_credentials( credentials )
+      results = []
+      safely do
+        # This hack is used to test if client capture exceptions correctly
+        # To raise an exception do GET /api/realms/50[0-2]
+        raise "DeltacloudErrorTest" if opts and opts[:id] == "500"
+        raise "NotImplementedTest" if opts and opts[:id] == "501"
+        raise "ProviderErrorTest" if opts and opts[:id] == "502"
+        raise "ProviderTimeoutTest" if opts and opts[:id] == "504"
+        return REALMS if ( opts.nil? )
+        results = REALMS
+      end
       results = filter_on( results, :id, opts )
       results
     end
@@ -520,8 +530,24 @@ module Deltacloud::Drivers::Mock
         message "Could not delete a non existent blob"
       end
 
-      on /Err/ do
+      on /DeltacloudErrorTest/ do
         status 500
+        message "DeltacloudErrorMessage"
+      end
+
+      on /NotImplementedTest/ do
+        status 501
+        message "NotImplementedMessage"
+      end
+
+      on /ProviderErrorTest/ do
+        status 502
+        message "ProviderErrorMessage"
+      end
+
+      on /ProviderTimeoutTest/ do
+        status 504
+        message "ProviderTimeoutMessage"
       end
 
     end
diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb
index d44f108..4353c4d 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -101,7 +101,7 @@ module ApplicationHelper
         format.json { convert_to_json(model, @element) }
       end
     else
-        report_error(404)
+      report_error(404)
     end
   end
 
diff --git a/server/lib/sinatra/lazy_auth.rb b/server/lib/sinatra/lazy_auth.rb
index ac8f5c7..9556bbc 100644
--- a/server/lib/sinatra/lazy_auth.rb
+++ b/server/lib/sinatra/lazy_auth.rb
@@ -63,7 +63,7 @@ module Sinatra
     def authorize!
       r = "#{driver_symbol}-deltacloud@#{HOSTNAME}"
       response['WWW-Authenticate'] = %(Basic realm="#{r}")
-      throw(:halt, [401, "Not authorized\n"])
+      report_error(401)
     end
 
     # Request the current user's credentials. Actual credentials are only
diff --git a/server/views/errors/501.html.haml b/server/views/errors/501.html.haml
new file mode 100644
index 0000000..19cf090
--- /dev/null
+++ b/server/views/errors/501.html.haml
@@ -0,0 +1,43 @@
+%div{ :'data-role' => :content, :'data-theme' => 'b'}
+  %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+    %li{ :'data-role' => 'list-divider'} Server message
+    %li
+      %h3=[@error.class.name, @error.message].join(' - ')
+    %li{ :'data-role' => 'list-divider'} Original request URI
+    %li
+      %a{ :href => request.env['REQUEST_URI'], :'data-ajax' => 'false'}
+        %span=request.env['REQUEST_URI']
+        %span{ :class => 'ui-li-count'} Retry
+    %li{ :'data-role' => 'list-divider'} Error details
+    %li
+      - if @error.class.method_defined? :details
+        %p= @error.details
+      - else
+        %em No details
+
+  %div{ 'data-role' => :collapsible, 'data-collapsed' => "true"}
+    %h3 Backtrace
+    %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+      %li
+        %pre=@error.backtrace.join("\n")
+
+  %div{ 'data-role' => :collapsible, 'data-collapsed' => "true"}
+    %h3 Parameters
+    %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+      - if params.keys.empty?
+        %li{ :'data-role' => 'list-divider'} No parameters
+      - params.each do |key, value|
+        - next if value.inspect.to_s == '#'
+        %li{ :'data-role' => 'list-divider'}=key
+        %li
+          %span{:style => 'font-weight:normal;'}=value.inspect
+
+
+  %div{ 'data-role' => :collapsible, 'data-collapsed' => "true"}
+    %h3 Request details
+    %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+      - request.env.each do |key, value|
+        - next if value.inspect.to_s == '#'
+        %li{ :'data-role' => 'list-divider'}=key
+        %li
+          %span{:style => 'font-weight:normal;'}=value.inspect
diff --git a/server/views/errors/501.xml.haml b/server/views/errors/501.xml.haml
new file mode 100644
index 0000000..788fe4b
--- /dev/null
+++ b/server/views/errors/501.xml.haml
@@ -0,0 +1,12 @@
+%error{:url => "#{request.env['REQUEST_URI']}", :status => "#{response.status}"}
+  %kind backend_error
+  %backend{ :driver => driver_symbol, :provider => "#{Thread::current[:provider] || ENV['API_PROVIDER'] || 'default'}" }
+  - if @error.respond_to?(:details) && @error.details
+    %details< #{cdata @error.details.join("\n")}
+  %message< #{cdata @error.message}
+  - if @error.respond_to? :backtrace
+    %backtrace=cdata @error.backtrace.join("\n")
+  - if params
+    %request
+      - params.each do |k, v|
+        %param{ :name => k}=v
diff --git a/server/views/errors/502.xml.haml b/server/views/errors/502.xml.haml
index 6e7a7b8..788fe4b 100644
--- a/server/views/errors/502.xml.haml
+++ b/server/views/errors/502.xml.haml
@@ -1,7 +1,12 @@
 %error{:url => "#{request.env['REQUEST_URI']}", :status => "#{response.status}"}
   %kind backend_error
-  %backend{ :driver => driver_symbol }
-    %code= @error.code
-    - if @error.respond_to?(:details) && @error.details
-      %details< #{cdata @error.details.join("\n")}
+  %backend{ :driver => driver_symbol, :provider => "#{Thread::current[:provider] || ENV['API_PROVIDER'] || 'default'}" }
+  - if @error.respond_to?(:details) && @error.details
+    %details< #{cdata @error.details.join("\n")}
   %message< #{cdata @error.message}
+  - if @error.respond_to? :backtrace
+    %backtrace=cdata @error.backtrace.join("\n")
+  - if params
+    %request
+      - params.each do |k, v|
+        %param{ :name => k}=v
diff --git a/server/views/errors/504.html.haml b/server/views/errors/504.html.haml
new file mode 100644
index 0000000..19cf090
--- /dev/null
+++ b/server/views/errors/504.html.haml
@@ -0,0 +1,43 @@
+%div{ :'data-role' => :content, :'data-theme' => 'b'}
+  %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+    %li{ :'data-role' => 'list-divider'} Server message
+    %li
+      %h3=[@error.class.name, @error.message].join(' - ')
+    %li{ :'data-role' => 'list-divider'} Original request URI
+    %li
+      %a{ :href => request.env['REQUEST_URI'], :'data-ajax' => 'false'}
+        %span=request.env['REQUEST_URI']
+        %span{ :class => 'ui-li-count'} Retry
+    %li{ :'data-role' => 'list-divider'} Error details
+    %li
+      - if @error.class.method_defined? :details
+        %p= @error.details
+      - else
+        %em No details
+
+  %div{ 'data-role' => :collapsible, 'data-collapsed' => "true"}
+    %h3 Backtrace
+    %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+      %li
+        %pre=@error.backtrace.join("\n")
+
+  %div{ 'data-role' => :collapsible, 'data-collapsed' => "true"}
+    %h3 Parameters
+    %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+      - if params.keys.empty?
+        %li{ :'data-role' => 'list-divider'} No parameters
+      - params.each do |key, value|
+        - next if value.inspect.to_s == '#'
+        %li{ :'data-role' => 'list-divider'}=key
+        %li
+          %span{:style => 'font-weight:normal;'}=value.inspect
+
+
+  %div{ 'data-role' => :collapsible, 'data-collapsed' => "true"}
+    %h3 Request details
+    %ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'e'}
+      - request.env.each do |key, value|
+        - next if value.inspect.to_s == '#'
+        %li{ :'data-role' => 'list-divider'}=key
+        %li
+          %span{:style => 'font-weight:normal;'}=value.inspect
diff --git a/server/views/errors/504.xml.haml b/server/views/errors/504.xml.haml
new file mode 100644
index 0000000..788fe4b
--- /dev/null
+++ b/server/views/errors/504.xml.haml
@@ -0,0 +1,12 @@
+%error{:url => "#{request.env['REQUEST_URI']}", :status => "#{response.status}"}
+  %kind backend_error
+  %backend{ :driver => driver_symbol, :provider => "#{Thread::current[:provider] || ENV['API_PROVIDER'] || 'default'}" }
+  - if @error.respond_to?(:details) && @error.details
+    %details< #{cdata @error.details.join("\n")}
+  %message< #{cdata @error.message}
+  - if @error.respond_to? :backtrace
+    %backtrace=cdata @error.backtrace.join("\n")
+  - if params
+    %request
+      - params.each do |k, v|
+        %param{ :name => k}=v
-- 
1.7.4.4