Ruby rescue exception during multiple methods -


i have built simple banking application, able perform usual operations; deposit, withdraw etc.

my controller methods perform these operations , rescue exceptions raised account or other entities.

here of these methods used in controller code:

def open(type, with:)     account = create type, (holders.find with)     add account     init_yearly_interest_for account     boundary.render accountsuccessmessage.new(account)   rescue itemexisterror => message     boundary.render message   end    def deposit(amount, into:)     account = find     account.deposit amount     boundary.render depositsuccessmessage.new(amount)   rescue itemexisterror => message     boundary.render message   end    def withdraw(amount, from:)     account = find     init_limit_reset_for account unless account.breached?     account.withdraw amount     boundary.render withdrawsuccessmessage.new(amount)   rescue itemexisterror, overlimit, insufficientfunds => message     boundary.render message   end    def get_balance_of(id)     account = find id     boundary.render balancemessage.new(account)   rescue itemexisterror => message     boundary.render message   end    def transfer(amount, from:, to:)     donar = find     recipitent = find     init_limit_reset_for donar unless donar.breached?     donar.withdraw amount     recipitent.deposit amount     boundary.render transfersuccessmessage.new(amount)   rescue itemexisterror, overlimit, insufficientfunds => message     boundary.render message   end    def add_holder(id, to:)     holder = holders.find id     account = find     account.add_holder holder     boundary.render addholdersuccessmessage.new(holder, account)   rescue itemexisterror, holderonaccount => message     boundary.render message   end    def get_transactions_of(id)     transactions = (find id).transactions     boundary.render transactionsmessage.new(transactions)   rescue itemexisterror => message     boundary.render message   end    def get_accounts_of(id)     holder = holders.find id     accounts = store.select { |_, a| a.holder? holder }.values     boundary.render displayaccountsmessage.new(accounts)   rescue itemexisterror => message     boundary.render message   end 

as can see rescuing multiple errors during multiple methods, same errors handled.

although working, wonder if possible refactor , handle these exceptions whenever of methods in controller called.

so example:

during:    open, deposit, withdraw rescue itemexisterror => message   boundary.render message 

any appreciated.

you can metaprogramming defining method wraps each of methods want rescue from. it's call whether or not cleaner code.

class mycontroller   # define unified exception handler methods   def self.rescue_from *meths, exception, &handler     meths.each |meth|       # store previous implementation       old = instance_method(meth)       # wrap       define_method(meth) |*args|         begin           old.bind(self).call(*args)         rescue exception => e           handler.call(e)         end       end     end   end    rescue_from :open, :deposit, :withdraw, itemexisterror |message|     boundary.render message   end end 

if aren't going reuse method (i.e. if want unified handler 1 set of methods , 1 exception class), remove rescue_from definition , put metaprogramming code right in class.


Comments