Skip to content

Commit 3fb12ae

Browse files
Paillat-devopenhands-agentnicebots-xyz-bot
authored
Enhance error handling in start.py (#61)
Co-authored-by: openhands <openhands@all-hands.dev> Co-authored-by: nicebots-xyz-bot <hello@nicebots.xyz>
1 parent 125de62 commit 3fb12ae

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/start.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ async def start_bot(bot: custom.Bot, token: str) -> None:
3737
except LoginFailure as e:
3838
logger.critical("Failed to log in, is the bot token valid?")
3939
logger.debug("", exc_info=e)
40+
except Exception as e: # noqa: BLE001
41+
logger.critical("An unexpected error occurred while starting the bot.")
42+
logger.debug("", exc_info=e)
4043

4144

4245
async def start_backend(app: Quart, bot: discord.Bot, token: str) -> None:
@@ -64,9 +67,13 @@ def __init__(
6467
app_config.logger_class = CustomLogger
6568
app_config.include_server_header = False # security
6669
app_config.bind = ["0.0.0.0:5000"]
67-
await bot.login(token)
68-
await serve(app, app_config)
69-
patch("hypercorn.error")
70+
try:
71+
await bot.login(token)
72+
await serve(app, app_config)
73+
patch("hypercorn.error")
74+
except Exception as e: # noqa: BLE001
75+
logger.critical("An error occurred while starting the backend server.")
76+
logger.debug("", exc_info=e)
7077

7178

7279
def load_extensions() -> (
@@ -94,7 +101,12 @@ def load_extensions() -> (
94101
continue
95102

96103
its_config = config["extensions"].get(name, config["extensions"].get(name.replace("_", "-"), {}))
97-
module: ModuleType = importlib.import_module(f"src.extensions.{name}")
104+
try:
105+
module: ModuleType = importlib.import_module(f"src.extensions.{name}")
106+
except ImportError as e:
107+
logger.error(f"Failed to import extension {name}")
108+
logger.debug("", exc_info=e)
109+
continue
98110
if not its_config:
99111
its_config = module.default
100112
config["extensions"][name] = its_config

0 commit comments

Comments
 (0)