Skip to content

Commit

Permalink
Move exceptions to module RQ
Browse files Browse the repository at this point in the history
  • Loading branch information
sodabrew committed Nov 6, 2013
1 parent db59de6 commit 50ecb93
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
6 changes: 6 additions & 0 deletions code/errors.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 17 additions & 16 deletions code/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'version'
require 'code/queuemgrclient'
require 'code/queueclient'
require 'code/errors'
require 'code/hashdir'
require 'code/portaproc'
require 'code/overrides'
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion code/queueclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'socket'
require 'json'
require 'unixrack'
require 'code/errors'

module RQ
class QueueClient
Expand All @@ -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)
Expand Down
37 changes: 15 additions & 22 deletions code/rq.rb
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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]
Expand All @@ -137,15 +130,15 @@ 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
end

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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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'
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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'])

Expand Down

0 comments on commit 50ecb93

Please sign in to comment.