commit 903f0ea75d9dd9386c25bbc831b1c766d731c81c
parent 325243760fcb22d6cd056ee2bf965370ea4e2239
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Wed, 30 Jan 2013 23:11:43 +0100
Better error reporting
Diffstat:
4 files changed, 21 insertions(+), 9 deletions(-)
diff --git a/error.py b/error.py
@@ -209,3 +209,7 @@ class ErrorOverflowedTemplate(Error):
return Error.report(self, _("Verse is beyond end of poem"),
short)
+class TemplateLoadError(BaseException):
+ def __init__(self, msg):
+ self.msg = msg
+
diff --git a/plint_web.py b/plint_web.py
@@ -4,6 +4,7 @@
import localization
import re
import template
+import error
from bottle import run, Bottle, request, static_file
from jinja2 import Environment, PackageLoader
@@ -85,10 +86,11 @@ def q():
d['poem'] = re.sub(r'<>&', '', d['poem'])
print(d['poem'])
poem = check(d['poem'])
- poem.append(None)
if not poem:
+ d['error'] = "Poem is empty, too long, or has too long lines"
return env.get_template('error.html').render(**d)
if not re.match("^[a-z_]+$", d['template']):
+ d['error'] = "Invalid template name"
return env.get_template('error.html').render(**d)
if d['template'] == 'custom':
x = request.forms.get('custom_template')
@@ -98,11 +100,16 @@ def q():
x = f.read()
f.close()
except IOError:
+ d['error'] = "No such template"
return env.get_template('error.html').render(**d)
try:
templ = template.Template(x)
- except ValueError:
+ except error.TemplateLoadError as e:
+ d['error'] = "Error when reading template: " + e.msg
return env.get_template('error.html').render(**d)
+ print(d['template'])
+ print(x)
+ poem.append(None)
r = []
firsterror = None
nerror = 0
diff --git a/template.py b/template.py
@@ -33,7 +33,7 @@ class Pattern:
"""Parse from a metric description"""
verse = [int(x) for x in self.metric.split('/')]
if sum(verse) > 16:
- raise ValueError
+ raise error.TemplateLoadError("Verse length limit exceeded")
self.hemistiches = []
self.length = 0
for v in verse:
@@ -79,7 +79,7 @@ class Template:
value = "classical"
self.diaeresis = value
if value not in ["permissive", "classical"]:
- raise ValueError
+ raise error.TemplateLoadError("Bad value for global option %s" % key)
elif key in ["check_end_hemistiche", "verifie_fin_hemistiche"]:
self.check_end_hemistiche = str2bool(value)
elif key in ["check_occurrences", "verifie_occurrences"]:
@@ -89,7 +89,7 @@ class Template:
elif key in ["incomplete_ok"]:
self.incomplete_ok = str2bool(value)
else:
- raise ValueError
+ raise error.TemplateLoadError("Unknown global option")
def load(self, s):
"""Load from a string"""
@@ -103,6 +103,8 @@ class Template:
self.read_option(option)
else:
self.template.append(self.parse_line(line.strip()))
+ if len(self.template) == 0:
+ raise error.TemplateLoadError("Template is empty")
def count(self, align):
"""total weight of an align"""
@@ -340,5 +342,5 @@ def str2bool(x):
return True
if x.lower() in ["no", "non", "n"]:
return False
- raise ValueError
+ raise error.TemplateLoadError("Bad value in global option.")
diff --git a/views/error.html b/views/error.html
@@ -2,11 +2,10 @@
{% block body %}
{% if lang == 'fr' %}
-<p>Les valeurs fournies sont incorrectes. (Peut-être que votre entrée est trop
-longue.) <a href="/about#help">Besoin
+<p>Une erreur est survenue: {{error}}. <a href="/about#help">Besoin
d'aide ?</a></p>
{% else %}
-<p>Invalid values supplied. (Maybe your input is too large.) Need some <a
+<p>An error occurred: {{error}}. Need some <a
href="/about#help">help</a>?</p>
{% endif %}
{% endblock %}