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

@@ -11,7 +11,6 @@ import weakref
import functools
from collections import namedtuple, OrderedDict
from datetime import datetime
from enum import IntEnum
from importlib.machinery import ModuleSpec
from pathlib import Path
from typing import (
@@ -39,6 +38,7 @@ from discord.ext import commands as dpy_commands
from discord.ext.commands import when_mentioned_or
from . import Config, i18n, commands, errors, drivers, modlog, bank
from .cli import ExitCodes
from .cog_manager import CogManager, CogManagerUI
from .core_commands import Core
from .data_manager import cog_data_path
@@ -69,7 +69,7 @@ SHARED_API_TOKENS = "SHARED_API_TOKENS"
log = logging.getLogger("red")
__all__ = ("Red", "ExitCodes")
__all__ = ("Red",)
NotMessage = namedtuple("NotMessage", "guild")
@@ -2190,11 +2190,3 @@ class Red(
failed_cogs=failures["cog"],
unhandled=failures["unhandled"],
)
class ExitCodes(IntEnum):
# This needs to be an int enum to be used
# with sys.exit
CRITICAL = 1
SHUTDOWN = 0
RESTART = 26

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"):

View File

@@ -12,6 +12,7 @@ import appdirs
from discord.utils import deprecated
from . import commands
from .cli import ExitCodes
__all__ = [
"create_temp_config",
@@ -118,7 +119,7 @@ def load_basic_configuration(instance_name_: str):
"You need to configure the bot instance using `redbot-setup`"
" prior to running the bot."
)
sys.exit(1)
sys.exit(ExitCodes.CONFIGURATION_ERROR)
try:
basic_config = config[instance_name]
except KeyError:
@@ -126,7 +127,7 @@ def load_basic_configuration(instance_name_: str):
"Instance with this name doesn't exist."
" You can create new instance using `redbot-setup` prior to running the bot."
)
sys.exit(1)
sys.exit(ExitCodes.INVALID_CLI_USAGE)
def _base_data_path() -> Path:

View File

@@ -8,6 +8,8 @@ from aiohttp_json_rpc.rpc import JsonRpcMethod
import logging
from redbot.core.cli import ExitCodes
log = logging.getLogger("red.rpc")
__all__ = ["RPC", "RPCMixin", "get_name"]
@@ -89,7 +91,7 @@ class RPC:
)
except Exception as exc:
log.exception("RPC setup failure", exc_info=exc)
sys.exit(1)
sys.exit(ExitCodes.CRITICAL)
else:
await self._site.start()
log.debug("Created RPC server listener on port %s", port)