Failing fast, Ruby style

While building up the server side RPC methods for client side calls from ExtJS’s new Direct RPC framework, I thought it would be useful to fail fast whenever the client caller violated the interface contract. Enter fail fast.

def submit(params)
  action = params.delete(:action)
  id = params.delete(:id)
  assert(action) {|v| ['update', 'create'].include?(v)}
...

Further up the stack, we can handle the exception and cleanly fail the client side request. From within active-direct.

    def invoke_method(model, method, parameters, tid)
...
      unless parameters.nil?
        return_val = model.constantize.send(method, *normalize_params_for(model,parameters))
      else
        return_val = model.constantize.send(method)
      end
      result['result'] = return_val.nil? ?  "" : return_val
 
    # Must catch descendant of Exception explicitly
    rescue FailFast::AssertionFailureError => e
      Rails.logger.error result['type'] = 'exception'
      Rails.logger.error result['message'] = e.message
      Rails.logger.error result['where'] = e.backtrace.join("\n")
...
    ensure
      return result
    end

The entry point to a RPC is a good place to ensure contract validity.

Post a Comment

Your email is never shared. Required fields are marked *

*
*