Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Created April 3, 2013 14:20
Show Gist options
  • Save sycobuny/5301627 to your computer and use it in GitHub Desktop.
Save sycobuny/5301627 to your computer and use it in GitHub Desktop.
module MyApp
class APIError < Exception; end
class Web < Sinatra::Base
def self.api(&block)
# some crazy stuff to wrap any get/put/delete/post/patch
# requests into a block with automatic error handling, and
# pass on all other requests to the sinatra base class.
end
api do
get '/hi' do
raise APIError, "Not implemented!"
end
put '/hi' do
raise APIError, "Not implemented!"
end
end
end
end
module MyApp
class APIError < Exception; end
class Web < Sinatra::Base
get '/hi' do
raise APIError, "Not implemented!"
rescue APIError => e
json :ok => false, :message => e.to_s
end
end
end
module MyApp
class APIError < Exception; end
class Web < Sinatra::Base
error APIError do
json :ok => false, :message => env['whatever the exception key is'].to_s
end
get '/hi' do
raise APIError, "Not implemented!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment