Skip to content

Instantly share code, notes, and snippets.

@grodniewicz
Created January 3, 2014 09:03
Show Gist options
  • Save grodniewicz/8235034 to your computer and use it in GitHub Desktop.
Save grodniewicz/8235034 to your computer and use it in GitHub Desktop.
honeybadger
import collections
import json
import requests
import sys
import traceback
import pprint
class HoneyBadger(object):
headers = {
"X-API-Key": "",
"Content-Type": "application/json"
}
url = "https://api.honeybadger.io/v1/notices"
def template(self, error, message, backtrace):
template = {
"notifier": {
"name": "pyhoneybadger",
"url": "",
"version": "0.1"
},
"error": {
"class": error,
"message": message,
"backtrace": backtrace,
"source": {
"2": "",
"3": " def runtime_error",
"4": " ",
"5": "end",
"6": ""
}
},
"request": {
"url": "http://example/runtime_error?a=1&b=2",
"component": "pages",
"action": "runtime_error",
"params": {
"_method": "post",
"authenticity_token": "",
"a": "1",
"b": "2",
"controller": "pages",
"action": "runtime_error"
},
"session": {
"session_id": "57fb796258046e92b3201ece44531320",
"_csrf_token": "tuZ7y1PUEMadgKevSzgSUK6T0p267I1+NL0+rnR7xrI="
},
#"cgi_data": {
# "rack.version": [
# "1",
# "1"
# ],
# "rack.input": "#<StringIO:0x007f87da1f86f0>",
# "rack.errors": "#<IO:0x007f87d886fa30>",
# "rack.multithread": "false",
# "rack.multiprocess": "true",
# "rack.run_once": "false",
# "rack.url_scheme": "http",
# "REQUEST_METHOD": "POST",
# "PATH_INFO": "/pages/runtime_error",
# "QUERY_STRING": "a=1&b=2",
# "SCRIPT_NAME": "",
# "REMOTE_ADDR": "127.0.0.1",
# "SERVER_ADDR": "0.0.0.0",
# "SERVER_NAME": "crywolf.dev",
# "SERVER_PORT": "80",
# "HTTP_HOST": "crywolf.dev",
# "HTTP_CONNECTION": "keep-alive",
# "CONTENT_LENGTH": "82",
# "HTTP_CACHE_CONTROL": "max-age=0",
# "HTTP_ORIGIN": "http://crywolf.dev",
# "HTTP_USER_AGENT": "",
# "CONTENT_TYPE": "application/x-www-form-urlencoded",
# "HTTP_ACCEPT": "",
# "HTTP_REFERER": "http://crywolf.dev/",
# "HTTP_ACCEPT_ENCODING": "gzip,deflate,sdch",
# "HTTP_ACCEPT_LANGUAGE": "en-US,en;q=0.8",
# "HTTP_ACCEPT_CHARSET": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
# "HTTP_COOKIE": "",
# "REMOTE_PORT": "52509",
# "ORIGINAL_FULLPATH": "/pages/runtime_error?a=1&b=2",
# "action_dispatch.routes": "",
# "action_dispatch.parameter_filter": [
# "password"
# ],
# "action_dispatch.secret_token": "",
# "action_dispatch.show_exceptions": "true",
# "action_dispatch.show_detailed_exceptions": "true",
# "action_dispatch.logger": "",
# "action_dispatch.backtrace_cleaner": "",
# "rack.request.form_input": "#<StringIO:0x007f87da1f86f0>",
# "rack.request.form_hash": {
# "_method": "post",
# "authenticity_token": ""
# },
# "rack.methodoverride.original_method": "POST",
# "action_dispatch.request_id": "",
# "action_dispatch.remote_ip": "127.0.0.1",
# "rack.session": {
# "session_id": "57fb796258046e92b3201ece44531320",
# "_csrf_token": ""
# },
# "rack.session.options": {
# "path": "/",
# "domain": "",
# "expire_after": "",
# "secure": "false",
# "httponly": "true",
# "defer": "false",
# "renew": "false",
# "coder": "",
# "id": "57fb796258046e92b3201ece44531320"
# },
# "rack.request.cookie_hash": {
# "_crywolf_session": ""
# },
# "rack.request.cookie_string": "",
# "action_dispatch.cookies": "",
# "action_dispatch.request.unsigned_session_cookie": {
# "session_id": "57fb796258046e92b3201ece44531320",
# "_csrf_token": ""
# },
# "action_dispatch.request.content_type": "",
# "action_dispatch.request.path_parameters": {
# "controller": "pages",
# "action": "runtime_error"
# },
# "action_controller.instance": "",
# "action_dispatch.request.request_parameters": {
# "_method": "post",
# "authenticity_token": ""
# },
# "rack.request.query_string": "a=1&b=2",
# "rack.request.query_hash": {
# "a": "1",
# "b": "2"
# },
# "action_dispatch.request.query_parameters": {
# "a": "1",
# "b": "2"
# },
# "action_dispatch.request.parameters": {
# "_method": "post",
# "authenticity_token": "",
# "a": "1",
# "b": "2",
# "controller": "pages",
# "action": "runtime_error"
# },
# "action_dispatch.request.formats": [
# "text/html"
# ]
# },
# "context": {
# "user_id": 1,
# "user_email": "foo@bar.com"
# }
},
"server": {
"project_root": {
"path": ""
},
"environment_name": "development",
"hostname": ""
}
}
return template
def send(self, data):
response = requests.post(self.url,
data=json.dumps(data),
headers=self.headers)
print(response.text)
def handle(self):
extype, exval, tb = sys.exc_info()
error = extype.__name__
tb_formatted = [i.split(',')
for i in traceback.format_exception(extype, exval, tb)]
pprint.pprint(tb_formatted)
backtrace = [collections.OrderedDict((
("number", i[1]),
("file", i[0]),
("method", i[2])
)) for i in tb_formatted[1:-1]]
pprint.pprint(backtrace)
message = exval.message
data = self.template(error, message, backtrace)
self.send(data)
if __name__ == "__main__":
hb = HoneyBadger()
def produce_exception(recursion_level=2):
sys.stdout.flush()
if recursion_level:
produce_exception(recursion_level-1)
else:
raise RuntimeError()
def call_function(f, recursion_level=2):
if recursion_level:
return call_function(f, recursion_level-1)
else:
return f()
try:
produce_exception()
except:
hb.handle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment