class TwirpRails::ErrorHandling::Base

Constants

ErrorHandler
ExceptionHandler

Public Class Methods

error_handlers() click to toggle source
# File lib/twirp_rails/error_handling/base.rb, line 83
def error_handlers
  @error_handlers ||= []
end
exception_handlers() click to toggle source
# File lib/twirp_rails/error_handling/base.rb, line 79
def exception_handlers
  @exception_handlers ||= []
end
exception_to_twirp(exception, handler) click to toggle source
# 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
translate_error(*codes, with: nil, &block) click to toggle source

translate_error :invalid_argument, InvalidArgument

translate_error :internal_error { |error, client| StandardError.new(error.msg) }

# 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
translate_exception(*exceptions, with: nil, &block) click to toggle source

translate_exception InvalidArgument, :invalid_argument

translate_exception StandardError { |exception, service| Twirp::Error.internal_with exception }

# 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.message).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
twirp_to_exception(error, client) click to toggle source
# 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