summaryrefslogtreecommitdiff
path: root/jimbrella
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2021-10-22 10:28:01 +0800
committerFrederick Yin <fkfd@fkfd.me>2021-10-22 10:28:01 +0800
commita3d400705e53b131997dc5e9c6ac10f2b5502761 (patch)
treeeb072599b76c947024f1218b88f3366782a79087 /jimbrella
parent2697f216b707ae83bcdaa5688fdb91d1f662d61d (diff)
Define application-specific exceptions
Diffstat (limited to 'jimbrella')
-rw-r--r--jimbrella/database.py9
-rw-r--r--jimbrella/exceptions.py11
2 files changed, 16 insertions, 4 deletions
diff --git a/jimbrella/database.py b/jimbrella/database.py
index 014b5b5..63cba49 100644
--- a/jimbrella/database.py
+++ b/jimbrella/database.py
@@ -3,6 +3,7 @@ import os
from datetime import datetime, timedelta
from .utils import human_datetime, human_timedelta
from .config import DUE_HOURS
+from .exceptions import *
class Database:
@@ -201,9 +202,9 @@ class Database:
"""When a user has borrowed an umbrella."""
umb = self._find_by_serial(serial)
if umb is None:
- raise ValueError(f"No umbrella with serial {serial} found.")
+ raise UmbrellaNotFoundError
elif umb["status"] != "available":
- raise ValueError(f"Umbrella with serial {serial} is inavailable.")
+ raise UmbrellaStatusError
umb["status"] = "lent"
umb["tenant_name"] = tenant_name
umb["tenant_id"] = tenant_id
@@ -216,9 +217,9 @@ class Database:
"""When a user has returned an umbrella."""
umb = self._find_by_serial(serial)
if umb is None:
- raise ValueError(f"No umbrella with serial {serial} found.")
+ raise UmbrellaNotFoundError
elif umb["status"] not in ("lent", "overdue"):
- raise ValueError(f"Umbrella with serial {serial} is not lent out.")
+ raise UmbrellaStatusError
umb["status"] = "available"
for key in ["tenant_name", "tenant_id", "tenant_phone", "tenant_email"]:
umb[key] = ""
diff --git a/jimbrella/exceptions.py b/jimbrella/exceptions.py
new file mode 100644
index 0000000..3b7fa4c
--- /dev/null
+++ b/jimbrella/exceptions.py
@@ -0,0 +1,11 @@
+"""Defines a few application-specific exceptions for Pythonic code."""
+class UmbrellaNotFoundError(Exception):
+ """For when an umbrella with required serial is not found in database."""
+ pass
+
+
+class UmbrellaStatusError(Exception):
+ """For when the umbrella to be lent is not available, or when an umbrella to be returned has
+ never been taken away. Most likely duplicate or erroneous answer sheets submitted by user.
+ """
+ pass