Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
improvement and i18n of elsewhere error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Mar 27, 2015
1 parent 9e3de09 commit f64d254
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions error.spt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera

from aspen.http import status_strings

from gratipay.utils import LazyResponse
from gratipay.utils.i18n import HTTP_ERRORS

[----------------------------------------]
Expand All @@ -13,6 +14,8 @@ try:
except Exception as e:
website.tell_sentry(e, state)

if isinstance(response, LazyResponse):
response.render_body(state)
err = response.body

[----------------------------------------] text/html
Expand Down
12 changes: 10 additions & 2 deletions gratipay/elsewhere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from requests_oauthlib import OAuth1Session, OAuth2Session

from gratipay.elsewhere._extractors import not_available
from gratipay.utils import LazyResponse


ACTIONS = {'opt-in', 'connect', 'lock', 'unlock'}
Expand Down Expand Up @@ -124,11 +125,18 @@ def api_get(self, path, sess=None, **kw):
if status == 404:
raise Response(404, response.text)
if status == 429 and is_user_session:
raise Response(429, response.text)
def msg(_, to_age):
if remaining == 0 and reset:
return _("You've consumed your quota of requests, you can try again in {0}.", to_age(reset))
else:
return _("You're making requests too fast, please try again later.")
raise LazyResponse(status, msg)
if status != 200:
log('{} api responded with {}:\n{}'.format(self.name, status, response.text)
, level=logging.ERROR)
raise Response(502, '{} lookup failed with {}'.format(self.name, status))
msg = lambda _: _("{0} returned an error, please try again later.",
self.display_name)
raise LazyResponse(502, msg)

return response

Expand Down
15 changes: 14 additions & 1 deletion gratipay/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

from aspen import Response, json
from aspen.utils import to_rfc822, utcnow
import gratipay
from dependency_injection import resolve_dependencies
from postgres.cursors import SimpleCursorBase

import gratipay


BEGINNING_OF_EPOCH = to_rfc822(datetime(1970, 1, 1)).encode('ascii')

Expand Down Expand Up @@ -186,3 +188,14 @@ def to_javascript(obj):
"""For when you want to inject an object into a <script> tag.
"""
return json.dumps(obj).replace('</', '<\\/')


class LazyResponse(Response):

def __init__(self, code, lazy_body, **kw):
Response.__init__(self, code, '', **kw)
self.lazy_body = lazy_body

def render_body(self, state):
f = self.lazy_body
self.body = f(*resolve_dependencies(f, state).as_args)

0 comments on commit f64d254

Please sign in to comment.