plint

French poetry validator (local mirror of https://gitlab.com/a3nm/plint)
git clone https://a3nm.net/git/plint/
Log | Files | Refs | README

commit 594c1762e51de255c335c32806419c9a059a868b
parent 1be1f71a5f940417caf44f7a8bd0dd130f3f96c8
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Thu, 17 Aug 2017 00:22:14 +0200

better error handling

Diffstat:
plint_web.py | 49+++++++++++++++++++++++++++++++++++++------------
1 file changed, 37 insertions(+), 12 deletions(-)

diff --git a/plint_web.py b/plint_web.py @@ -105,14 +105,23 @@ def about(lang): return env.get_template('about.html').render(title=get_title(lang), lang=lang, path="about") +MAX_POEM_LEN = 8192 +MAX_LINE_LEN = 512 + +class TooBigException(Exception): + pass + +class TooLongLinesException(Exception): + pass + def check(poem): - if len(poem) > 8192: - return None + if len(poem) > MAX_POEM_LEN: + raise TooBigException s = poem.split("\n") for x in range(len(s)): - if len(s[x]) > 512: - return None - s[x].strip() + if len(s[x]) > MAX_LINE_LEN: + raise TooLongLinesException + s[x] = s[x].strip() return s @app.route('/<lang>/checkjs', method='POST') @@ -141,13 +150,29 @@ def q(lang): throttle.add((ip, t)) poem = re.sub(r'<>&', '', request.forms.get('poem')) print(poem) - poem = check(poem) - if not poem: - if lang == 'fr': - msg = "Le poème est vide, trop long, ou a des lignes trop longues" - else: - msg = "Poem is empty, too long, or has too long lines" - return dumps({'error': msg}) + + # default message + if lang == 'fr': + msg = "Le poème est vide" + else: + msg = "Poem is empty" + + try: + poem = check(poem) + except TooBigException: + poem = None + if lang == 'fr': + msg = "Le poème est trop long (maximum %d caractères)" % MAX_POEM_LEN + else: + msg = "Poem is too long (maximum %d characters)" % MAX_POEM_LEN + except TooLongLinesException: + poem = None + if lang == 'fr': + msg = "Certaines lignes du poème sont trop longues (maximum %d caractères)" % MAX_LINE_LEN + else: + msg = "Some lines of the poem are too long (maximum %d characters)" % MAX_LINE_LEN + if not poem or len(poem) == 0 or (len(poem) == 1 and len(poem[0]) == 0): + return dumps({'error': msg}) templateName = re.sub(r'[^a-z_]', '', request.forms.get('template')) print(templateName) if templateName == 'custom':