Skip to content

Commit bb67d87

Browse files
committed
Add rate limiting for error emails, adjust error message and get them shown in frontend.
1 parent 24c3a09 commit bb67d87

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

backend/boltathon/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ def handle_notfound_error(err):
5353

5454
@app.errorhandler(RequestError)
5555
def handle_request_error(err):
56-
return jsonify({"message": err.message}), err.code
56+
return jsonify({"error": err.message}), err.code
5757

5858
@app.errorhandler(Exception)
5959
def handle_exception(err):
6060
app.logger.debug(traceback.format_exc())
6161
app.logger.debug("Uncaught exception at {} {}, see above for traceback".format(request.method, request.path))
62-
return jsonify({"message": "Something went wrong"}), 500
62+
return jsonify({"error": "Something went wrong"}), 500
6363

6464
return app

backend/boltathon/util/mail.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def tip_error_info(email_args):
3535
}
3636

3737

38-
def generate_email(type, email_args, user=None):
38+
def generate_email(user, type, email_args):
3939
info = get_info_lookup[type](email_args)
4040
body_text = render_template(
4141
'emails/%s.txt' % (type),
@@ -88,7 +88,7 @@ def send_email(user, type, email_args):
8888
return
8989

9090
try:
91-
email = generate_email(type, email_args)
91+
email = generate_email(user, type, email_args)
9292
sg = SendGridAPIClient(SENDGRID_API_KEY)
9393
print(SENDGRID_DEFAULT_FROM)
9494
print(user.email)
@@ -107,3 +107,20 @@ def send_email(user, type, email_args):
107107
except Exception as e:
108108
current_app.logger.info('An unknown error occured while sending an email to %s - %s: %s' % (user.email, e.__class__.__name__, e))
109109
current_app.logger.error(e)
110+
111+
# Sends an email once and only once this session of the app. This is pretty
112+
# low-stakes, so it's OK that it rests on restart. Mostly meant to avoid spam.
113+
send_once_map = {}
114+
115+
def send_email_once(user, type, email_args, extra_key='default'):
116+
if not user or not user.email:
117+
return
118+
119+
key = '%s - %s - %s' % (user.id, type, extra_key)
120+
121+
if send_once_map.get(key):
122+
current_app.logger.debug('Already sent with key `%s`' % (key))
123+
return
124+
125+
send_once_map[key] = True
126+
return send_email(user, type, email_args)

backend/boltathon/views/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from boltathon.util.auth import requires_auth
88
from boltathon.util.node import get_pubkey_from_credentials, make_invoice, lookup_invoice
99
from boltathon.util.errors import RequestError
10-
from boltathon.util.mail import send_email
10+
from boltathon.util.mail import send_email_once
1111
from boltathon.models.user import User, self_user_schema, public_user_schema, public_users_schema
1212
from boltathon.models.connection import Connection, public_connections_schema
1313
from boltathon.models.tip import Tip, tip_schema, tips_schema
@@ -125,12 +125,12 @@ def post_invoice(args, user_id, **kwargs):
125125
current_app.logger.error(e)
126126

127127
if err:
128-
send_email(user, 'tip_error', {
128+
send_email_once(user, 'tip_error', {
129129
'error': err,
130130
'config_url': frontend_url('/user/me?tab=config'),
131131
'support_url': 'https://github.com/tiphub-io/tiphub/issues',
132-
})
133-
raise RequestError(code=500, message='Failed to generate a tip invoice')
132+
}, err)
133+
raise RequestError(code=500, message='Failed to generate a tip invoice, their node may be offline or otherwise inaccessible')
134134

135135

136136
@blueprint.route('/users/search/<query>', methods=['GET'])

0 commit comments

Comments
 (0)