Module: Sinatra::RPC::Fault

Defined in:
lib/sinatra/rpc/fault.rb

Overview

This module is used to generate all custom RPC errors.

Class Method Summary collapse

Class Method Details

.register(fault_name, error_code) ⇒ Object

Generate a new fault class. The class will be a subclass of RuntimeError, and always include the Fault module.

Examples:

Sinatra::RPC::Fault.register :bad_request, 400
Sinatra::RPC::BadRequestFault::CODE                       # => 400
raise Sinatra::RPC::BadRequestFault, "Bad request"
RuntimeError === Sinatra::RPC::BadRequestFault.new        # => true
Sinatra::RPC::Fault === Sinatra::RPC::BadRequestFault.new # => true

20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sinatra/rpc/fault.rb', line 20

def self.register(fault_name, error_code)
  fault_class = Class.new(RuntimeError) do
    include Sinatra::RPC::Fault
    def code
      self.class.const_get 'CODE'
    end
  end

  fault_class.const_set 'CODE', error_code

  class_name = "#{Sinatra::RPC::Utils.camelize fault_name}Fault"
  Sinatra::RPC.const_set(class_name, fault_class)
end