зеркало из https://github.com/arthurnn/twirp-ruby.git
Differentiate between returning a Twirp::Error and raising a Twirp::Exception
This commit is contained in:
Родитель
ff72884a74
Коммит
9887ede0a8
|
@ -1,3 +1,4 @@
|
|||
require_relative 'twirp/version'
|
||||
require_relative 'twirp/error'
|
||||
require_relative 'twirp/service'
|
||||
require_relative 'twirp/exception'
|
||||
require_relative 'twirp/service'
|
||||
|
|
|
@ -29,7 +29,7 @@ module Twirp
|
|||
ERROR_CODES = ERROR_CODES_TO_HTTP_STATUS.keys
|
||||
|
||||
# Twirp::Error represents a valid error from a Twirp service
|
||||
class Error < StandardError
|
||||
class Error
|
||||
|
||||
# Wrap an arbitrary error as a Twirp :internal
|
||||
def self.InternalWith(err)
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
require_relative "./error"
|
||||
|
||||
module Twirp
|
||||
|
||||
class Exception < StandardError
|
||||
|
||||
def initialize(code, msg, meta=nil)
|
||||
@twerr = Twirp::Error.new(code, msg, meta)
|
||||
end
|
||||
|
||||
def message
|
||||
"#{code}: #{msg}"
|
||||
end
|
||||
|
||||
def code; @twerr.code; end
|
||||
def msg; @twerr.msg; end
|
||||
def meta; @twerr.meta; end
|
||||
def to_json; @twerr.to_json; end
|
||||
def as_json; @twerr.as_json; end
|
||||
end
|
||||
end
|
|
@ -96,11 +96,8 @@ module Twirp
|
|||
resp = @handler.send(rpc[:handler_method], proto_req)
|
||||
return rack_response_from_handler(rpc, content_type, resp)
|
||||
|
||||
rescue Twirp::Error => twerr
|
||||
rescue Twirp::Exception => twerr
|
||||
error_response(twerr)
|
||||
rescue StandardError => err
|
||||
puts err.backtrace
|
||||
error_response(Twirp::Error.InternalWith(err))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -180,8 +180,8 @@ class ServiceTest < Minitest::Test
|
|||
assert_equal Example::Hat.new(inches: 0, color: ""), Example::Hat.decode(body[0])
|
||||
end
|
||||
|
||||
# Handler should be able to return Twirp::Exception values, that will trigger error responses
|
||||
def test_handler_returns_twirp_exception
|
||||
# Handler should be able to return Twirp::Error values, that will trigger error responses
|
||||
def test_handler_returns_twirp_error
|
||||
svc = Example::Haberdasher.new(HaberdasherHandler.new do |size|
|
||||
return Twirp::Error.new(:invalid_argument, "I don't like that size")
|
||||
end)
|
||||
|
@ -199,7 +199,7 @@ class ServiceTest < Minitest::Test
|
|||
# Handler should be able to raise a Twirp::Exception, that will trigger error responses
|
||||
def test_handler_raises_twirp_exception
|
||||
svc = Example::Haberdasher.new(HaberdasherHandler.new do |size|
|
||||
raise Twirp::Error.new(:invalid_argument, "I don't like that size")
|
||||
raise Twirp::Exception.new(:invalid_argument, "I don't like that size")
|
||||
end)
|
||||
|
||||
env = proto_req "/twirp/example.Haberdasher/MakeHat", Example::Size.new(inches: 666)
|
||||
|
|
Загрузка…
Ссылка в новой задаче