#-- # Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # Made in Japan. #++ module Ruote # # A ruote service for turning exceptions into process errors (or letting # those error fire any potential :on_error attributes in the process # definition). # # This service is used, by the worker, the dispatch pool and some # receivers (like the one in ruote-beanstalk). # class ErrorHandler def initialize(context) @context = context end # As used by the dispatch pool and the worker. # def msg_handle(msg, exception) fexp = Ruote::Exp::FlowExpression.fetch( @context, msg['fei'] || msg['workitem']['fei'] ) rescue nil handle(msg, fexp, exception) end # As used by some receivers (see ruote-beanstalk's receiver). # def action_handle(action, fei, exception) fexp = Ruote::Exp::FlowExpression.fetch(@context, fei) msg = { 'action' => action, 'fei' => fei, 'participant_name' => fexp.h.participant_name, 'workitem' => fexp.h.applied_workitem } handle(msg, fexp, exception) end protected # As used by the worker. # def handle(msg, fexp, exception) wfid = msg['wfid'] || (msg['fei']['wfid'] rescue nil) fei = msg['fei'] || (fexp.h.fei rescue nil) backtrace = exception.backtrace || [] # debug only if $DEBUG || ARGV.include?('-d') puts "\n== worker intercepted error ==" puts p exception puts backtrace[0, 20].join("\n") puts '...' puts puts '-- msg --' key_length = msg.keys.collect { |k| k.length }.max + 1 msg.keys.sort.each { |k| v = msg[k] v = (Ruote.sid(v) rescue nil) if k == 'fei' || k == 'parent_id' printf("%*s : %s\n", key_length, k, v.inspect) } puts '-- . --' puts end # on_error ? return if fexp && fexp.handle_on_error(msg, exception) # emit 'msg' # # (this message might get intercepted by a tracker) dev = exception.respond_to?(:deviations) ? exception.deviations : nil @context.storage.put_msg( 'error_intercepted', 'error' => { 'fei' => fei, 'at' => Ruote.now_to_utc_s, 'class' => exception.class.name, 'message' => exception.message, 'trace' => backtrace, 'deviations' => dev }, 'wfid' => wfid, 'fei' => fei, 'msg' => msg) # fill error in the error journal @context.storage.put( 'type' => 'errors', '_id' => "err_#{Ruote.to_storage_id(fei)}", 'message' => exception.inspect, 'trace' => backtrace.join("\n"), 'deviations' => dev, 'fei' => fei, 'msg' => msg ) if fei end end end