Created
June 2, 2010 22:54
-
-
Save robhudson/423138 to your computer and use it in GitHub Desktop.
Django error logging middleware for GoPlanApp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GPATicketErrorLogMiddleware(object): | |
@transaction.commit_on_success | |
def process_exception(self, request, exception): | |
import oauth2 as oauth | |
import sys | |
import traceback | |
create_ticket_url = 'http://%s.goplanapp.com/api/tickets/create' % ( | |
settings.GPA_COMPANY_NAME,) | |
if isinstance(exception, Http404): | |
return | |
if transaction.is_dirty(): | |
transaction.rollback() | |
try: | |
request_repr = repr(request) | |
except: | |
request_repr = "Request repr() unavailable" | |
title = '[%s] Error (%s IP): %s' % ( | |
settings.PROJECT_NAME, | |
(request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS \ | |
and 'internal' or 'EXTERNAL'), | |
request.path | |
) | |
message = "<pre><code>%s\n\n%s</code></pre>" % ( | |
'\n'.join(traceback.format_exception(*sys.exc_info())), | |
request_repr) | |
# Generate OAuth request to GoPlan | |
consumer = oauth.Consumer(settings.GPA_CONSUMER_KEY, settings.GPA_CONSUMER_SECRET) | |
token = oauth.Token(settings.GPA_OAUTH_TOKEN, settings.GPA_OAUTH_TOKEN_SECRET) | |
client = oauth.Client(consumer, token) | |
params = { | |
'project': settings.GPA_PROJECT_NAME, | |
'ticket[title]': title, | |
'ticket[description]': message, | |
'ticket[priority]': 5, | |
'company': settings.GPA_COMPANY_NAME, | |
'format': 'json', | |
'oauth_consumer_key': settings.GPA_CONSUMER_KEY, | |
'oauth_token': settings.GPA_OAUTH_TOKEN, | |
} | |
req = oauth.Request(method="POST", url=create_ticket_url, parameters=params) | |
req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token) | |
resp, content = client.request(req.url, "POST", body=req.to_postdata()) | |
# if resp.status != 200: pass # do something if API call fails? | |
# Regular emailing middleware will still happen | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment