Class: TwirpRails::ErrorHandling::Base
- Inherits:
-
Object
- Object
- TwirpRails::ErrorHandling::Base
- Defined in:
- lib/twirp_rails/error_handling/base.rb
Defined Under Namespace
Classes: ErrorHandler, ExceptionHandler
Class Method Summary collapse
- .error_handlers ⇒ Object
- .exception_handlers ⇒ Object
- .exception_to_twirp(exception, handler) ⇒ Object
-
.translate_error(*codes, with: nil, &block) ⇒ Object
translate_error :invalid_argument, InvalidArgument.
-
.translate_exception(*exceptions, with: nil, &block) ⇒ Object
translate_exception InvalidArgument, :invalid_argument.
- .twirp_to_exception(error, client) ⇒ Object
Class Method Details
permalink .error_handlers ⇒ Object
[View source]
83 84 85 |
# File 'lib/twirp_rails/error_handling/base.rb', line 83 def error_handlers @error_handlers ||= [] end |
permalink .exception_handlers ⇒ Object
[View source]
79 80 81 |
# File 'lib/twirp_rails/error_handling/base.rb', line 79 def exception_handlers @exception_handlers ||= [] end |
permalink .exception_to_twirp(exception, handler) ⇒ Object
[View source]
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/twirp_rails/error_handling/base.rb', line 87 def exception_to_twirp(exception, handler) result = nil exception_handlers.take_while do |exception_handler| result = exception_handler.process(exception, handler) result.nil? end result || ::Twirp::Error.internal_with(exception) end |
permalink .translate_error(*codes, with: nil, &block) ⇒ Object
translate_error :invalid_argument, InvalidArgument
translate_error :internal_error { |error, client| StandardError.new(error.msg) }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/twirp_rails/error_handling/base.rb', line 61 def translate_error(*codes, with: nil, &block) raise 'unexpected with and block' if block_given? && with.nil? raise 'with or block must be defined' unless block_given? || !with.nil? raise "invalid twirp code(s) #{codes - ::Twirp::ERROR_CODES}" if (codes - ::Twirp::ERROR_CODES).any? proc = if with raise 'with should be a exception class' unless with.is_a?(Class) proc { |error, _client| with.new error.msg } else proc { |error, client| block.call(error, client) } end codes.each do |code| error_handlers << ErrorHandler.new(code, proc) end end |
permalink .translate_exception(*exceptions, with: nil, &block) ⇒ Object
translate_exception InvalidArgument, :invalid_argument
translate_exception StandardError { |exception, service| Twirp::Error.internal_with exception }
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/twirp_rails/error_handling/base.rb', line 38 def translate_exception(*exceptions, with: nil, &block) raise 'unexpected with and block' if block_given? && with raise 'with or block must be defined' unless block_given? || with proc = if with raise "invalid twirp code #{with}" unless ::Twirp::ERROR_CODES.include?(with) proc do |exception, _service| ::Twirp::Error.new(with, exception.).tap { |t| t.cause = exception } end else proc { |exception, service| block.call(exception, service) } end exceptions.each do |exception| exception_handlers << ExceptionHandler.new(exception, proc) end end |
permalink .twirp_to_exception(error, client) ⇒ Object
[View source]
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/twirp_rails/error_handling/base.rb', line 99 def twirp_to_exception(error, client) result = nil error_handlers.take_while do |handler| result = handler.process(error, client) result.nil? end result || StandardError.new(error.msg) end |