Skip to content

Commit

Permalink
Base Sanic request support (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
saabeilin authored and rokob committed Feb 17, 2018
1 parent 87ba05b commit a251836
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion rollbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
except ImportError:
BottleRequest = None

try:
from sanic.request import Request as SanicRequest
except ImportError:
SanicRequest = None

try:
from google.appengine.api.urlfetch import fetch as AppEngineFetch
except ImportError:
Expand Down Expand Up @@ -373,7 +378,7 @@ def report_exc_info(exc_info=None, request=None, extra_data=None, payload_data=N
Reports an exception to Rollbar, using exc_info (from calling sys.exc_info())
exc_info: optional, should be the result of calling sys.exc_info(). If omitted, sys.exc_info() will be called here.
request: optional, a WebOb or Werkzeug-based request object.
request: optional, a WebOb, Werkzeug-based or Sanic request object.
extra_data: optional, will be included in the 'custom' section of the payload
payload_data: optional, dict that will override values in the final payload
(e.g. 'level' or 'fingerprint')
Expand Down Expand Up @@ -1040,6 +1045,10 @@ def _build_request_data(request):
if BottleRequest and isinstance(request, BottleRequest):
return _build_bottle_request_data(request)

# Sanic
if SanicRequest and isinstance(request, SanicRequest):
return _build_sanic_request_data(request)

# Plain wsgi (should be last)
if isinstance(request, dict) and 'wsgi.version' in request:
return _build_wsgi_request_data(request)
Expand Down Expand Up @@ -1153,6 +1162,26 @@ def _build_bottle_request_data(request):
return request_data


def _build_sanic_request_data(request):
request_data = {
'url': request.url,
'user_ip': request.remote_addr,
'headers': request.headers,
'method': request.method,
'GET': dict(request.args)
}

if request.json:
try:
request_data['body'] = request.json
except:
pass
else:
request_data['POST'] = request.form

return request_data


def _build_wsgi_request_data(request):
request_data = {
'url': wsgiref.util.request_uri(request),
Expand Down

0 comments on commit a251836

Please sign in to comment.