Use different exit codes for critical errors vs configuration errors (#5674)

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
Jakub Kuczys
2022-12-25 22:27:07 +01:00
committed by GitHub
parent 0e58897bfc
commit e8c044a9bf
9 changed files with 60 additions and 44 deletions

View File

@@ -2,6 +2,7 @@ import argparse
import asyncio
import logging
import sys
from enum import IntEnum
from typing import Optional
import discord
@@ -10,6 +11,21 @@ from discord import __version__ as discord_version
from redbot.core.utils._internal_utils import cli_level_to_log_level
# This needs to be an int enum to be used
# with sys.exit
class ExitCodes(IntEnum):
#: Clean shutdown (through signals, keyboard interrupt, [p]shutdown, etc.).
SHUTDOWN = 0
#: An unrecoverable error occurred during application's runtime.
CRITICAL = 1
#: The CLI command was used incorrectly, such as when the wrong number of arguments are given.
INVALID_CLI_USAGE = 2
#: Restart was requested by the bot owner (probably through [p]restart command).
RESTART = 26
#: Some kind of configuration error occurred.
CONFIGURATION_ERROR = 78 # Exit code borrowed from os.EX_CONFIG.
def confirm(text: str, default: Optional[bool] = None) -> bool:
if default is None:
options = "y/n"
@@ -23,9 +39,12 @@ def confirm(text: str, default: Optional[bool] = None) -> bool:
while True:
try:
value = input(f"{text}: [{options}] ").lower().strip()
except (KeyboardInterrupt, EOFError):
except KeyboardInterrupt:
print("\nAborted!")
sys.exit(1)
sys.exit(ExitCodes.SHUTDOWN)
except EOFError:
print("\nAborted!")
sys.exit(ExitCodes.INVALID_CLI_USAGE)
if value in ("y", "yes"):
return True
if value in ("n", "no"):