Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Custom error type to wrap around Beefcake errors.
Browse files Browse the repository at this point in the history
Change-Id: Id724690e9a86b55dd8f7ed58321bd2cf397e1ede
  • Loading branch information
Kowshik Prakasam committed Oct 12, 2012
1 parent d058888 commit ab5f67a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
39 changes: 34 additions & 5 deletions warden-protocol/lib/warden/protocol/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(", ")}"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion warden-protocol/lib/warden/protocol/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Warden
module Protocol
VERSION = "0.0.8"
VERSION = "0.0.9"
end
end
59 changes: 59 additions & 0 deletions warden-protocol/spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ab5f67a

Please sign in to comment.