Skip to content

Commit

Permalink
Merge branch 'master' into andrew-prettyprint-json
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Burke committed May 2, 2013
2 parents 3852a7f + 7fbac1b commit 289735b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
11 changes: 7 additions & 4 deletions flask_restful/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask.views import MethodView
from flask.signals import got_request_exception
from werkzeug.exceptions import HTTPException, MethodNotAllowed, NotFound
from werkzeug.http import HTTP_STATUS_CODES
from flask.ext.restful.utils import unauthorized, error_data, unpack
from flask.ext.restful.representations.json import output_json

Expand Down Expand Up @@ -158,7 +159,8 @@ def handle_error(self, e):
if code >= 500:
self.app.logger.exception("Internal Error")

if code == 404:
if code == 404 and ('message' not in data or
data['message'] == HTTP_STATUS_CODES[404]):
rules = dict([(re.sub('(<.*>)', '', rule.rule), rule.rule)
for rule in self.app.url_map.iter_rules()])
close_matches = difflib.get_close_matches(request.path, rules.keys())
Expand All @@ -168,10 +170,11 @@ def handle_error(self, e):
data["message"] += ". "
else:
data["message"] = ""

data['message'] += 'You have requested this URI [' + request.path + \
'] but did you mean ' + \
' or '.join((rules[match]
for match in close_matches)) + ' ?'
'] but did you mean ' + \
' or '.join((rules[match]
for match in close_matches)) + ' ?'

resp = self.make_response(data, code)

Expand Down
18 changes: 14 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from flask_restful import OrderedDict
from json import dumps, loads
#noinspection PyUnresolvedReferences
from nose.tools import assert_equals # you need it for tests in form of continuations
from nose.tools import assert_equals, assert_true # you need it for tests in form of continuations

def check_unpack(expected, value):
assert_equals(expected, value)
Expand Down Expand Up @@ -262,9 +262,11 @@ def get(self):
app = app.test_client()

resp = app.get("/api")
self.assertEquals(resp.status_code, 404)
self.assertEquals('application/json', resp.headers['Content-Type'])
self.assertEquals(loads(resp.data).get('status'), 404)
assert_equals(resp.status_code, 404)
assert_equals('application/json', resp.headers['Content-Type'])
data = loads(resp.data)
assert_equals(data.get('status'), 404)
assert_true('message' in data)


def test_handle_non_api_error(self):
Expand Down Expand Up @@ -566,6 +568,14 @@ def test_abort_no_data(self):
self.assertEquals(False, hasattr(e, "data"))


def test_abort_custom_message(self):
try:
flask_restful.abort(404, message="no user")
assert False # We should never get here
except Exception as e:
assert_equals(e.data['message'], "no user")


def test_abort_type(self):
self.assertRaises(werkzeug.exceptions.HTTPException, lambda: flask_restful.abort(404))

Expand Down

0 comments on commit 289735b

Please sign in to comment.