Skip to content

Commit 4680ec4

Browse files
authored
V2.4 clearer error messages (#47)
1 parent 4f92d1c commit 4680ec4

File tree

10 files changed

+839
-599
lines changed

10 files changed

+839
-599
lines changed

bot/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ class ConversationState(Enum):
3434
CPF,
3535
BACKLOG,
3636
ADD_BACKLOG_ENTRY,
37-
) = range(26)
37+
NOTIFICATION,
38+
) = range(27)

bot/database_service/firestore_service.py

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from bot.database_service.auth import get_db_client
22
from datetime import datetime, timedelta
33
import pytz
4+
from bot.error_handler import DatabaseError
45

56

67
class FirestoreService:
@@ -14,25 +15,37 @@ def __init__(self, collection_name="users"):
1415

1516
# New user setup
1617
def new_user_setup(self, telegram_id, sheet_id, telegram_username):
17-
user_ref = self.db.collection(self.collection_name).document(str(telegram_id))
18-
timestamp = datetime.now(pytz.timezone("Asia/Singapore"))
19-
user_ref.set(
20-
{
21-
"sheet_id": sheet_id,
22-
"datetime_created": timestamp,
23-
"username": telegram_username,
24-
"usage_count": 0,
25-
"last_accessed": timestamp,
26-
"hourly_accessed": timestamp,
27-
"overusage_count": 0,
28-
}
29-
)
18+
try:
19+
user_ref = self.db.collection(self.collection_name).document(
20+
str(telegram_id)
21+
)
22+
timestamp = datetime.now(pytz.timezone("Asia/Singapore"))
23+
user_ref.set(
24+
{
25+
"sheet_id": sheet_id,
26+
"datetime_created": timestamp,
27+
"username": telegram_username,
28+
"usage_count": 0,
29+
"last_accessed": timestamp,
30+
"hourly_accessed": timestamp,
31+
"overusage_count": 0,
32+
}
33+
)
34+
except Exception as e:
35+
raise DatabaseError(message="Error setting up new user", extra_info=str(e))
3036

3137
# Check if user exists
3238
def check_if_user_exists(self, telegram_id):
33-
user_ref = self.db.collection(self.collection_name).document(str(telegram_id))
34-
user_doc = user_ref.get()
35-
return user_doc.exists
39+
try:
40+
user_ref = self.db.collection(self.collection_name).document(
41+
str(telegram_id)
42+
)
43+
user_doc = user_ref.get()
44+
return user_doc.exists
45+
except Exception as e:
46+
raise DatabaseError(
47+
message="Error checking if user exists", extra_info=str(e)
48+
)
3649

3750
# Get user sheet id
3851
def get_user_sheet_id(self, telegram_id, telegram_username):
@@ -76,19 +89,18 @@ def get_user_sheet_id(self, telegram_id, telegram_username):
7689
)
7790
return user_doc.get("sheet_id")
7891
except Exception as e:
79-
raise e
80-
return None
92+
raise DatabaseError(
93+
message="Error getting user sheet id", extra_info=str(e)
94+
)
95+
raise DatabaseError(
96+
message="User does not exist", extra_info="User does not exist"
97+
)
8198

8299
# Get all user IDs
83100
def get_all_user_id(self):
84-
users_ref = self.db.collection(self.collection_name)
85-
user_ids = [int(user.id) for user in users_ref.stream()]
86-
return user_ids
87-
88-
# Get all sheet IDs
89-
def get_all_sheet_id(self):
90-
users_ref = self.db.collection(self.collection_name)
91-
sheet_ids = []
92-
for user in users_ref.stream():
93-
sheet_ids.append(user.get("sheet_id"))
94-
return sheet_ids
101+
try:
102+
users_ref = self.db.collection(self.collection_name)
103+
user_ids = [int(user.id) for user in users_ref.stream()]
104+
return user_ids
105+
except Exception as e:
106+
raise DatabaseError(message="Error getting all user ids", extra_info=str(e))

bot/error_handler.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import logging
2+
3+
4+
logging.basicConfig(level=logging.ERROR, filename='telegram_bot.log',
5+
format='%(asctime)s - %(levelname)s - %(message)s')
6+
7+
class BaseError(Exception):
8+
"""Base class for other exceptions"""
9+
def __init__(self, error_class, message="An error occurred", extra_info=""):
10+
self.message = message
11+
self.extra_info = extra_info
12+
self.error_class = error_class
13+
super().__init__(f"{self.error_class}: {self.message}")
14+
15+
class GoogleSheetError(BaseError):
16+
"""Exception raised for errors in the Google Sheet service."""
17+
def __init__(self, message="", extra_info=""):
18+
super().__init__("GoogleSheetError", message, extra_info)
19+
20+
class TelegramBotError(BaseError):
21+
"""Exception raised for errors in the Telegram bot operations."""
22+
def __init__(self, message="", extra_info=""):
23+
super().__init__("TelegramBotError", message, extra_info)
24+
25+
class DatabaseError(BaseError):
26+
"""Exception raised for errors in database operations."""
27+
def __init__(self, message="", extra_info=""):
28+
super().__init__("DatabaseError", message, extra_info)

0 commit comments

Comments
 (0)