diff --git a/code/errors.rb b/code/errors.rb new file mode 100644 index 0000000..2b184a9 --- /dev/null +++ b/code/errors.rb @@ -0,0 +1,6 @@ +module RQ + class RqError < ::StandardError; end + class RqCannotRelay < RqError; def message; 'Cannot relay message'; end; end + class RqQueueNotFound < RqError; def message; 'Queue not found'; end; end + class RqMissingArgument < RqError; def message; 'Missing argument'; end; end +end diff --git a/code/main.rb b/code/main.rb index a9de616..abc84aa 100644 --- a/code/main.rb +++ b/code/main.rb @@ -4,6 +4,7 @@ require 'version' require 'code/queuemgrclient' require 'code/queueclient' +require 'code/errors' require 'code/hashdir' require 'code/portaproc' require 'code/overrides' @@ -175,7 +176,7 @@ def flash_now(type, msg) begin qc = RQ::QueueClient.new(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -190,7 +191,7 @@ def flash_now(type, msg) begin qc = RQ::QueueClient.new(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -205,7 +206,7 @@ def flash_now(type, msg) get '/q/:name/new_message' do begin qc = RQ::QueueClient.new(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -261,7 +262,7 @@ def flash_now(type, msg) begin qc = get_queueclient(q_name) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -313,7 +314,7 @@ def flash_now(type, msg) get '/q/:name/config' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -333,7 +334,7 @@ def flash_now(type, msg) begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -361,7 +362,7 @@ def flash_now(type, msg) begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -378,7 +379,7 @@ def flash_now(type, msg) post '/q/:name/:msg_id/clone' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -398,7 +399,7 @@ def flash_now(type, msg) post '/q/:name/:msg_id/run_now' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -419,7 +420,7 @@ def flash_now(type, msg) post '/q/:name/:msg_id/attach/new' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -478,7 +479,7 @@ def flash_now(type, msg) post '/q/:name/:msg_id/attach/:attachment_name' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -506,7 +507,7 @@ def flash_now(type, msg) get '/q/:name/:msg_id/log/:log_name' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -536,7 +537,7 @@ def flash_now(type, msg) get '/q/:name/:msg_id/attach/:attach_name' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -567,7 +568,7 @@ def flash_now(type, msg) get '/q/:name/:msg_id/tailview/:attach_name' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -600,7 +601,7 @@ def flash_now(type, msg) get '/q/:name/:msg_id/tailviewlog/:log_name' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end @@ -624,7 +625,7 @@ def flash_now(type, msg) post '/q/:name/:msg_id' do begin qc = get_queueclient(params[:name]) - rescue RqQueueNotFound + rescue RQ::RqQueueNotFound throw :halt, [404, "404 - Queue not found"] end diff --git a/code/queueclient.rb b/code/queueclient.rb index 53b9a73..a51f340 100644 --- a/code/queueclient.rb +++ b/code/queueclient.rb @@ -2,6 +2,7 @@ require 'socket' require 'json' require 'unixrack' +require 'code/errors' module RQ class QueueClient @@ -17,7 +18,7 @@ def initialize(name, path=".") @queue_path = File.join(path, 'queue', @name) @queue_sock_path = File.join(@queue_path, 'queue.sock') - raise RqQueueNotFound unless File.directory?(@queue_path) + raise RQ::RqQueueNotFound unless File.directory?(@queue_path) end def running?(pid=read_pid) diff --git a/code/rq.rb b/code/rq.rb index 30448bd..b5b18df 100644 --- a/code/rq.rb +++ b/code/rq.rb @@ -1,14 +1,7 @@ require 'vendor/environment' require 'code/rule_processor' require 'code/queueclient' - -# needs some sort of error handling -- move out of this file -# so that higher level up services can handle them (for us -- RqUtils2) -class RqError < ::StandardError; end - -class RqCannotRelay < RqError; def message; 'Cannot relay message'; end; end -class RqQueueNotFound < RqError; def message; 'Queue not found'; end; end -class RqMissingArgument < RqError; def message; 'Missing argument'; end; end +require 'code/errors' def check_usage(arg_list) if not arg_list.length > 0 or arg_list.include?('-h') or arg_list.include?('--help') @@ -65,10 +58,10 @@ def get_queue_client(q_name) # - param[1234] def cmd_sendmesg(args) q_name = args['dest'] - raise RqMissingArgument if not q_name + raise RQ::RqMissingArgument if not q_name if q_name.index('http:') == 0 - raise RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message'] + raise RQ::RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message'] q_name = 'relay' end @@ -88,10 +81,10 @@ def cmd_sendmesg(args) def cmd_prepmesg(args) q_name = args['dest'] - raise RqMissingArgument if not q_name + raise RQ::RqMissingArgument if not q_name if q_name.index('http:') == 0 - raise RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message'] + raise RQ::RqCannotRelay if !args.has_key?('relay-ok') # throw :halt, [404, 'Sorry - cannot relay message'] q_name = 'relay' end @@ -120,7 +113,7 @@ def check_attachment(msg) def cmd_attachmesg(args) full_mesg_id = args['msg_id'] - raise RqMissingArgument if not full_mesg_id + raise RQ::RqMissingArgument if not full_mesg_id q_name = full_mesg_id[/\/q\/([^\/]+)/, 1] msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1] @@ -137,7 +130,7 @@ def cmd_attachmesg(args) msg['pathname'] = File.expand_path(msg['pathname']) results = check_attachment(msg) - raise RqError(results[0]) if not results[0] # throw :halt, [404, "404 - #{results[0]}"] + raise RQ::RqError(results[0]) if not results[0] # throw :halt, [404, "404 - #{results[0]}"] result = qc.attach_message(msg) print "#{result[0]} #{result[1]} for Message: #{full_mesg_id} attachment\n" result[0] == "ok" ? 0 : 1 @@ -145,7 +138,7 @@ def cmd_attachmesg(args) def cmd_commitmesg(args) full_mesg_id = args['msg_id'] - raise RqMissingArgument if not full_mesg_id + raise RQ::RqMissingArgument if not full_mesg_id q_name = full_mesg_id[/\/q\/([^\/]+)/, 1] msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1] @@ -162,7 +155,7 @@ def cmd_commitmesg(args) def cmd_statusmesg(args) full_mesg_id = args['msg_id'] - raise RqMissingArgument if not full_mesg_id + raise RQ::RqMissingArgument if not full_mesg_id q_name = full_mesg_id[/\/q\/([^\/]+)/, 1] msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1] @@ -182,7 +175,7 @@ def cmd_statusmesg(args) def cmd_state(args) full_mesg_id = args['msg_id'] - raise RqMissingArgument if not full_mesg_id + raise RQ::RqMissingArgument if not full_mesg_id q_name = full_mesg_id[/\/q\/([^\/]+)/, 1] msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1] @@ -202,7 +195,7 @@ def cmd_state(args) def cmd_statuscountmesg(args) full_mesg_id = args['msg_id'] - raise RqMissingArgument if not full_mesg_id + raise RQ::RqMissingArgument if not full_mesg_id q_name = full_mesg_id[/\/q\/([^\/]+)/, 1] msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1] @@ -222,7 +215,7 @@ def cmd_statuscountmesg(args) def cmd_single_que(args) q_name = args['dest'] - raise RqMissingArgument if not q_name + raise RQ::RqMissingArgument if not q_name #if (q_name.index('http:') == 0) && args.has_key?('relay-ok') # q_name = 'relay' @@ -246,7 +239,7 @@ def cmd_single_que(args) def cmd_attachstatusmesg(args) full_mesg_id = args['msg_id'] - raise RqMissingArgument if not full_mesg_id + raise RQ::RqMissingArgument if not full_mesg_id q_name = full_mesg_id[/\/q\/([^\/]+)/, 1] msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1] @@ -275,7 +268,7 @@ def cmd_attachstatusmesg(args) def cmd_clone(args) full_mesg_id = args['msg_id'] - raise RqMissingArgument if not full_mesg_id + raise RQ::RqMissingArgument if not full_mesg_id q_name = full_mesg_id[/\/q\/([^\/]+)/, 1] msg_id = full_mesg_id[/\/q\/[^\/]+\/([^\/]+)/, 1] @@ -289,7 +282,7 @@ def cmd_clone(args) end def cmd_verify_rules(args) - raise RqMissingArgument if not args['path'] + raise RQ::RqMissingArgument if not args['path'] rp = RQ::RuleProcessor.process_pathname(args['path'], args['verbose'])