root/lib/helpers.rb @ fcc0d38b
bba6b24b | Ohad Levy | class SmartProxy < Sinatra::Base
|
|
helpers do
|
|||
b0e03d34 | Paul Kelly | # Accepts a html error code and a message, which is then returned to the caller after adding to the proxy log
|
|
# OR a block which is executed and its errors handled in a similar way.
|
|||
# If no code is supplied when the block is declared then the html error used is 400.
|
|||
fcc0d38b | Ohad Levy | def log_halt code=nil, exception=nil
|
|
message = exception.is_a?(String) ? exception : exception.to_s
|
|||
b0e03d34 | Paul Kelly | begin
|
|
if block_given?
|
|||
return yield
|
|||
end
|
|||
rescue => e
|
|||
message += e.message
|
|||
code = code || 400
|
|||
end
|
|||
aa7de29a | Paul Kelly | content_type :json if request.accept.include?("application/json")
|
|
bba6b24b | Ohad Levy | logger.error message
|
|
fcc0d38b | Ohad Levy | logger.debug exception.backtrace.join("\n") if exception.is_a?(Exception)
|
|
bba6b24b | Ohad Levy | halt code, message
|
|
end
|
|||
end
|
|||
end
|