Skip to content
Browse files

Custom error type to wrap around Beefcake errors.

Change-Id: Id724690e9a86b55dd8f7ed58321bd2cf397e1ede
  • Loading branch information...
1 parent d058888 commit ab5f67aa07d0d750790d73c08950781447eeaad9 @kowshik kowshik committed Oct 8, 2012
View
39 warden-protocol/lib/warden/protocol/base.rb
@@ -94,6 +94,19 @@ def self.to_response_klass(type)
:string => lambda { |arg| String(arg) },
}
+ # Used to wrap around Beefcake errors.
+ class ProtocolError < StandardError
+ attr_reader :cause
+
+ def initialize(cause)
+ @cause = cause
+ end
+
+ def message
+ return @cause.message
+ end
+ end
+
def self.protocol_type_to_str(protocol_type)
if protocol_type.class == Module
return "#{protocol_type.constants.join(", ")}"
@@ -121,8 +134,16 @@ def self.to_ruby_type(str, protocol_type)
class BaseMessage
include Beefcake::Message
+ def safe
+ yield
+ rescue WrongTypeError, InvalidValueError, RequiredFieldNotSetError => e
+ raise ProtocolError, e
+ end
+
def reload
- self.class.decode(encode)
+ safe do
+ self.class.decode(encode)
+ end
end
class << self
@@ -155,7 +176,9 @@ def create_response(attributes = {})
end
def wrap
- WrappedRequest.new(:type => self.class.type, :payload => encode)
+ safe do
+ WrappedRequest.new(:type => self.class.type, :payload => encode)
+ end
end
def self.description
@@ -173,7 +196,9 @@ def error?
end
def wrap
- WrappedResponse.new(:type => self.class.type, :payload => encode)
+ safe do
+ WrappedResponse.new(:type => self.class.type, :payload => encode)
+ end
end
end
@@ -182,7 +207,9 @@ class WrappedRequest < BaseRequest
required :payload, :string, 2
def request
- Type.to_request_klass(type).decode(payload)
+ safe do
+ Type.to_request_klass(type).decode(payload)
+ end
end
end
@@ -191,7 +218,9 @@ class WrappedResponse < BaseResponse
required :payload, :string, 2
def response
- Type.to_response_klass(type).decode(payload)
+ safe do
+ Type.to_response_klass(type).decode(payload)
+ end
end
end
end
View
2 warden-protocol/lib/warden/protocol/version.rb
@@ -2,6 +2,6 @@
module Warden
module Protocol
- VERSION = "0.0.8"
+ VERSION = "0.0.9"
end
end
View
59 warden-protocol/spec/base_spec.rb
@@ -3,6 +3,46 @@
require "spec_helper"
require "warden/protocol"
+describe Warden::Protocol::BaseRequest do
+ it "should respond to #wrap" do
+ request = Warden::Protocol::SpawnRequest.new(:handle => "blah",
+ :script => "script")
+ wrapped = request.wrap
+ wrapped.should be_an_instance_of(Warden::Protocol::WrappedRequest)
+ wrapped.type.should == Warden::Protocol::SpawnRequest.type
+ decoded = Warden::Protocol::SpawnRequest.decode(wrapped.payload)
+ decoded.handle.should == request.handle
+ decoded.script.should == request.script
+ end
+
+ it "should wrap beefcake errors" do
+ expect {
+ Warden::Protocol::SpawnRequest.new.wrap
+ }.to raise_error(Warden::Protocol::ProtocolError) { |e|
+ e.cause.class.name.should =~ /^Beefcake/
+ }
+ end
+end
+
+describe Warden::Protocol::BaseResponse do
+ it "should respond to #wrap" do
+ response = Warden::Protocol::SpawnResponse.new(:job_id => 1)
+ wrapped = response.wrap
+ wrapped.should be_an_instance_of(Warden::Protocol::WrappedResponse)
+ wrapped.type.should == Warden::Protocol::SpawnResponse.type
+ decoded = Warden::Protocol::SpawnResponse.decode(wrapped.payload)
+ decoded.job_id.should == response.job_id
+ end
+
+ it "should wrap beefcake errors" do
+ expect {
+ Warden::Protocol::SpawnResponse.new.wrap
+ }.to raise_error(Warden::Protocol::ProtocolError) { |e|
+ e.cause.class.name.should =~ /^Beefcake/
+ }
+ end
+end
+
describe Warden::Protocol::WrappedRequest do
it "should respond to #request" do
w = Warden::Protocol::WrappedRequest.new
@@ -13,6 +53,15 @@
w.request.should be_a(Warden::Protocol::SpawnRequest)
end
+
+ it "should wrap beefcake errors" do
+ w = Warden::Protocol::WrappedRequest.new
+ w.type = Warden::Protocol::Type::Spawn
+ w.payload = "bad payload"
+ w.should be_valid
+
+ expect { w.request }.to raise_error(Warden::Protocol::ProtocolError)
+ end
end
describe Warden::Protocol::WrappedResponse do
@@ -25,6 +74,16 @@
w.response.should be_a(Warden::Protocol::SpawnResponse)
end
+
+ it "should wrap beefcake errors" do
+ w = Warden::Protocol::WrappedResponse.new
+ w.type = Warden::Protocol::Type::Spawn
+ w.payload = "bad payload"
+
+ w.should be_valid
+
+ expect { w.response }.to raise_error(Warden::Protocol::ProtocolError)
+ end
end
describe Warden::Protocol do

0 comments on commit ab5f67a

Please sign in to comment.
Something went wrong with that request. Please try again.