Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[server] [merged] Return HTTP errors in JSON format if Accept: contains application/json #488

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions isso/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
from isso import config, db, migrate, wsgi, ext, views
from isso.core import ThreadedMixin, ProcessMixin, uWSGIMixin
from isso.wsgi import origin, urlsplit
from isso.utils import http, JSONRequest, html, hash
from isso.utils import http, JSONRequest, JSONResponse, html, hash
from isso.views import comments

from isso.ext.notifications import Stdout, SMTP
Expand All @@ -81,6 +81,11 @@

logger = logging.getLogger("isso")

def error_handler(env, request, error):
if "application/json" in request.headers["Accept"]:
ix5 marked this conversation as resolved.
Show resolved Hide resolved
data = {'message': str(error)}
return JSONResponse(data, 500 if error.code == None else error.code)
return error

class Isso(object):

Expand Down Expand Up @@ -135,16 +140,16 @@ def dispatch(self, request):
try:
handler, values = adapter.match()
except HTTPException as e:
return e
return error_handler(request.environ, request, e)
else:
try:
response = handler(request.environ, request, **values)
except HTTPException as e:
return e
return error_handler(request.environ, request, e)
except Exception:
logger.exception("%s %s", request.method,
request.environ["PATH_INFO"])
return InternalServerError()
return error_handler(request.environ, request, InternalServerError())
else:
return response

Expand Down