summaryrefslogtreecommitdiff
path: root/jimbrella
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2021-10-27 11:20:27 +0800
committerFrederick Yin <fkfd@fkfd.me>2021-10-27 11:20:27 +0800
commit3fdc1d0b91cf4be7768e940cf930e74005768177 (patch)
tree31eec01b95a200f8f32e44eb7e6fd566c538fde4 /jimbrella
parent2a5870f529b5defe9154834c252b0d30cccc9c8d (diff)
Provide exception messages
UmbrellaNotFoundError: serial UmbrellaValueError: field
Diffstat (limited to 'jimbrella')
-rw-r--r--jimbrella/admin.py25
-rw-r--r--jimbrella/database.py8
-rw-r--r--jimbrella/exceptions.py10
3 files changed, 34 insertions, 9 deletions
diff --git a/jimbrella/admin.py b/jimbrella/admin.py
index df751ee..4c604be 100644
--- a/jimbrella/admin.py
+++ b/jimbrella/admin.py
@@ -1,6 +1,7 @@
from flask import Blueprint, request, render_template, redirect, url_for
from user_agents import parse as user_agent
from .database import Database
+from .exceptions import *
from .config import *
bp = Blueprint("admin", __name__, url_prefix="/admin")
@@ -25,11 +26,17 @@ def index():
def umbrellas():
umbrellas = db.read()
edit = request.args.get("edit")
+ error = request.args.get("error")
+ template = (
+ "admin/umbrellas_mobile.html"
+ if user_agent(request.user_agent.string).is_mobile
+ else "admin/umbrellas_desktop.html"
+ )
return render_template(
- "admin/umbrellas.html",
+ template,
umbrellas=umbrellas,
edit=int(edit) if edit else None,
- mobile=user_agent(request.user_agent.string).is_mobile,
+ error=error,
)
@@ -48,5 +55,17 @@ def umbrellas_edit():
]:
data[key] = request.form.get(key)
- db.update(data)
+ error = None
+ try:
+ db.update(data)
+ except UmbrellaValueError as e:
+ # invalid field is in `e.message`.
+ return redirect(
+ "{0}?edit={1}&error={2}".format(
+ url_for("admin.umbrellas"), request.form.get("serial"), e.message
+ )
+ )
+ except UmbrellaNotFoundError:
+ pass # impossible on web console
+
return redirect(url_for("admin.umbrellas"))
diff --git a/jimbrella/database.py b/jimbrella/database.py
index ab945da..e43b8f2 100644
--- a/jimbrella/database.py
+++ b/jimbrella/database.py
@@ -190,7 +190,7 @@ class Database:
umb["serial"] = int(umb["serial"])
umb_in_db = self._find_by_serial(umb["serial"])
if umb_in_db is None:
- raise UmbrellaNotFoundError
+ raise UmbrellaNotFoundError(umb["serial"])
status = umb_in_db["status"]
if "status" in umb and umb["status"]:
@@ -261,7 +261,7 @@ class Database:
"""When a user has borrowed an umbrella."""
umb = self._find_by_serial(serial)
if umb is None:
- raise UmbrellaNotFoundError
+ raise UmbrellaNotFoundError(serial)
elif umb["status"] != "available":
raise UmbrellaStatusError
umb["status"] = "lent"
@@ -276,7 +276,7 @@ class Database:
"""When a user has returned an umbrella."""
umb = self._find_by_serial(serial)
if umb is None:
- raise UmbrellaNotFoundError
+ raise UmbrellaNotFoundError(serial)
elif umb["status"] not in ("lent", "overdue"):
raise UmbrellaStatusError
umb["status"] = "available"
@@ -289,7 +289,7 @@ class Database:
"""When an umbrella is overdue, change its status to "overdue"."""
umb = self._find_by_serial(serial)
if umb is None:
- raise UmbrellaNotFoundError
+ raise UmbrellaNotFoundError(serial)
elif umb["status"] != "lent":
raise UmbrellaStatusError
umb["status"] = "overdue"
diff --git a/jimbrella/exceptions.py b/jimbrella/exceptions.py
index 2c62bc4..ad92b50 100644
--- a/jimbrella/exceptions.py
+++ b/jimbrella/exceptions.py
@@ -4,7 +4,10 @@
class UmbrellaNotFoundError(Exception):
"""For when an umbrella with required serial is not found in database."""
- pass
+ def __init__(self, serial):
+ self.serial = serial
+ self.message = f"No umbrella {serial} found."
+ super().__init__(self.message)
class UmbrellaStatusError(Exception):
@@ -18,4 +21,7 @@ class UmbrellaStatusError(Exception):
class UmbrellaValueError(Exception):
"""For when an admin enters an invalid value when modifying the database."""
- pass
+ def __init__(self, field: str):
+ self.field = field
+ self.message = f"Invalid field: {field}."
+ super().__init__(self.message)