From a3d400705e53b131997dc5e9c6ac10f2b5502761 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Fri, 22 Oct 2021 10:28:01 +0800 Subject: Define application-specific exceptions --- jimbrella/database.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'jimbrella/database.py') 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] = "" -- cgit v1.2.3