chatbot1-12 Exercise 1: Every Configuration Problem At Once ============================================================== ex1.py (save in the project root): # Exercise 1: find every configuration problem at once, then fix them one by one. from bot.crypto import generate_key from bot.settings import load_settings env = {"CHATBOT_ENV": "production", "CHATBOT_PROVIDER": "claude", "CHATBOT_PORT": "eighty", "CHATBOT_CONTENT_KEYS": "not-a-key", "CHATBOT_WEB_SECRET": "short", "SLACK_BOT_TOKEN": "xoxb-1"} def show(title): s = load_settings(env) print(f"{title}: {len(s.problems)} problem(s)") for p in s.problems: print(" -", p) show("as first written") env.update(CHATBOT_PORT="8000", CHATBOT_CONTENT_KEYS=generate_key(), CHATBOT_WEB_SECRET="w" * 40, CHATBOT_LOG_KEY="l" * 20, CHATBOT_ALLOWED_ORIGINS="https://chat.example.org") show("after fixing the first group") env.update(ANTHROPIC_API_KEY="sk-ant-example", CHATBOT_MODEL="example-model", SLACK_SIGNING_SECRET="s3cret") show("after fixing the rest") Run with: py ex1.py Output: as first written: 8 problem(s) - ANTHROPIC_API_KEY is required for provider 'claude' - CHATBOT_MODEL is required: read the current model name from your provider's model list - CHATBOT_WEB_SECRET must be at least 32 characters in production - CHATBOT_LOG_KEY must be at least 16 characters in production - CHATBOT_ALLOWED_ORIGINS is required in production (for example https://chat.example.org) - CHATBOT_CONTENT_KEYS contains a value that is not a valid Fernet key - SLACK_BOT_TOKEN and SLACK_SIGNING_SECRET must be set together - CHATBOT_PORT must be a whole number after fixing the first group: 3 problem(s) - ANTHROPIC_API_KEY is required for provider 'claude' - CHATBOT_MODEL is required: read the current model name from your provider's model list - SLACK_BOT_TOKEN and SLACK_SIGNING_SECRET must be set together after fixing the rest: 0 problem(s) Reading the result: - All eight problems appeared in the first pass, so one round of editing can fix them; a program that stopped at the first would have taken eight restarts. - The invalid port is reported rather than crashing with a traceback, and the content key is reported without being printed. - Slack needs its token AND its signing secret; setting one alone is reported as a pair problem. - The real provider needs both its key variable and a model name. The model name is never defaulted, because a name written into a course goes out of date. WHY THIS WORKS AS AN ANSWER --------------------------- load_settings() never raises for a bad value; it appends to a list of problems and carries on with a harmless default so the later checks can still run. The caller decides what to do: --check prints them and exits with status 1, and a production start refuses to run. The same function is used for both, so what --check says is what the service would do.