lib/flapjack/api.rb in flapjack-0.6.29 vs lib/flapjack/api.rb in flapjack-0.6.30
- old
+ new
@@ -23,11 +23,13 @@
class API < Sinatra::Base
# doesn't work with Rack::Test for some reason
unless 'test'.eql?(FLAPJACK_ENV)
rescue_exception = Proc.new { |env, exception|
- [503, {}, exception.message]
+ logger.error exception.message
+ logger.error exception.backtrace.join("\n")
+ [503, {}, {:status => 503, :reason => exception.message}.to_json]
}
use Rack::FiberPool, :size => 25, :rescue_exception => rescue_exception
end
use Rack::MethodOverride
@@ -36,21 +38,10 @@
before do
# will only initialise the first time it's run
Flapjack::API.bootstrap
end
- helpers do
- def json_status(code, reason)
- status code
- {:status => code, :reason => reason}.to_json
- end
-
- def logger
- Flapjack::API.logger
- end
- end
-
get '/entities' do
content_type :json
ret = Flapjack::Data::Entity.all(:redis => @@redis).sort_by(&:name).collect {|e|
{'id' => e.id, 'name' => e.name,
'checks' => e.check_list.sort.collect {|c|
@@ -209,11 +200,11 @@
end
entity_check = Flapjack::Data::EntityCheck.for_entity(entity,
params[:check], :redis => @@redis)
entity_check.create_scheduled_maintenance(:start_time => params[:start_time],
:duration => params[:duration], :summary => params[:summary])
- status 201
+ status 204
end
# create an acknowledgement for a service on an entity
# NB currently, this does not acknowledge a specific failure event, just
# the entity-check as a whole
@@ -230,21 +221,17 @@
entity_check = Flapjack::Data::EntityCheck.for_entity(entity,
params[:check], :redis => @@redis)
entity_check.create_acknowledgement('summary' => params[:summary],
'duration' => duration)
- status 201
+ status 204
end
not_found do
- json_status 404, "Not found"
+ [404, {}, {:status => 404, :reason => "Not found"}.to_json]
end
- error do
- json_status 500, env['sinatra.error'].message
- end
-
private
def entity_check_status(entity, check)
entity_check = Flapjack::Data::EntityCheck.for_entity(entity,
check, :redis => @@redis)
@@ -263,10 +250,10 @@
# NB: casts to UTC before converting to a timestamp
def validate_and_parsetime(value)
return unless value
Time.iso8601(value).getutc.to_i
rescue ArgumentError => e
- # FIXME log error
+ logger.error "Couldn't parse time from '#{value}'"
nil
end
end