ref: refs/heads/master commit 31907a9865c3d7a7c3b62dcb515cfcc20e36f8cb Author: Duncan Mac-Vicar P <dmacvicar@suse.de> Date: Tue Oct 27 11:02:51 2009 +0100 move the backend exception decoding code to its own class --- webclient/lib/client_exception.rb | 56 ++++++++++++++++++++++++++ webclient/lib/error_constructor.rb | 3 +- webclient/test/unit/client_exception_test.rb | 54 +++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/webclient/lib/client_exception.rb b/webclient/lib/client_exception.rb new file mode 100644 index 0000000..b3a47dd --- /dev/null +++ b/webclient/lib/client_exception.rb @@ -0,0 +1,56 @@ +require 'error_constructor' + +# +# Exception class which support for remote +# exception information passed by the response body +# +# If you get an exception, and the exception has +# server side encoded information, you can create a +# ClientException from it and access the information +# in the same way you do with other kinds of exceptions +# +# If the exception has no encoded information, it will +# just forward every data +# +class ClientException < Exception + include ErrorConstructor + + # creates an client exception from another + # exception + def initialize(excpt) + # save the original exception + @excpt = excpt + @error_data = {} + if backend_exception? + @error_data = Hash.from_xml(excpt.response.body) rescue {} + # construct an exception from what we have + @err_msg = construct_error(@error_data) + end + end + + # message of the exception, this is the translated + # message for the error on the server side if the exception + # happened there, or the local one if it is a normal exception + def message + return @err_msg unless @err_msg.blank? + return @excpt.message + end + + # remote exception type as a symbol + def backend_exception_type + if error_data.has_key?('error') + return error_data['error']['type'].to_sym if error_data['error'].has_key?('type') + end + return :exception + end + + def backend_exception? + @excpt.is_a?(ActiveResource::ServerError) and + @excpt.response.code.to_s =~ /.*503.*/ + end + + def backtrace + @excpt.backtrace + end + +end diff --git a/webclient/lib/error_constructor.rb b/webclient/lib/error_constructor.rb index 575a8c9..bb7545e 100644 --- a/webclient/lib/error_constructor.rb +++ b/webclient/lib/error_constructor.rb @@ -1,9 +1,8 @@ # To change this template, choose Tools | Templates # and open the template in the editor. -module ErrorConstructor +module ErrorConstructor def construct_error (error) - error = error["error"] case error["type"] when "SERVICE_NOT_AVAILABLE" diff --git a/webclient/test/unit/client_exception_test.rb b/webclient/test/unit/client_exception_test.rb new file mode 100644 index 0000000..130110a --- /dev/null +++ b/webclient/test/unit/client_exception_test.rb @@ -0,0 +1,54 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'client_exception' + +def _(msg) + msg +end + +class ClientExceptionTest < ActiveSupport::TestCase + + def test_decode + response = Struct.new("Response", :code, :body) + body =<<EOF +<error> + <type>NO_PERM</type> + <description>A Message</description> +</error> +EOF + + body_unknown =<<EOF +<error> + <type>UNKNOWN_ERROR</type> + <description>A Message</description> +</error> +EOF + + # only 503 exceptions are backend exceptions + server_error = ActiveResource::ServerError.new("message") + server_error.stubs(:response).returns(response.new('503', body)) + client_exception = ClientException.new(server_error) + assert client_exception.backend_exception? + #assert_equal "", client_exception.message + + # a unknown backend exception would use the message from the + # xml + server_error = ActiveResource::ServerError.new("message") + server_error.stubs(:response).returns(response.new('503', body_unknown)) + client_exception = ClientException.new(server_error) + assert client_exception.backend_exception? + assert_equal "A Message", client_exception.message + + server_error = ActiveResource::ServerError.new("message") + server_error.stubs(:response).returns(response.new('502', body)) + client_exception = ClientException.new(server_error) + assert ! client_exception.backend_exception? + + server_error = ActiveResource::ServerError.new("message") + client_exception = ClientException.new(Exception.new("Hello!")) + assert ! client_exception.backend_exception? + assert_equal "Hello!", client_exception.message + + end + +end + -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org