Add better error message for registration target server not found

This commit is contained in:
Tulir Asokan 2020-04-22 23:31:37 +03:00
parent 4b88971b3b
commit 6b5dedb29f

View file

@ -30,6 +30,14 @@ history_count: int = 10
enc = functools.partial(quote, safe="") enc = functools.partial(quote, safe="")
friendly_errors = {
"server_not_found": "Registration target server not found.\n\n"
"To log in or register through maubot, you must add the server to the\n"
"registration_secrets section in the config. If you only want to log in,\n"
"leave the `secret` field empty."
}
@cliq.command(help="Log into a Matrix account via the Maubot server") @cliq.command(help="Log into a Matrix account via the Maubot server")
@cliq.option("-h", "--homeserver", help="The homeserver to log into", required=True) @cliq.option("-h", "--homeserver", help="The homeserver to log into", required=True)
@cliq.option("-u", "--username", help="The username to log in with", required=True) @cliq.option("-u", "--username", help="The username to log in with", required=True)
@ -62,8 +70,9 @@ def auth(homeserver: str, username: str, password: str, server: str, register: b
print(f"{Fore.GREEN}Access token: {Fore.CYAN}{resp['access_token']}{Fore.RESET}") print(f"{Fore.GREEN}Access token: {Fore.CYAN}{resp['access_token']}{Fore.RESET}")
except HTTPError as e: except HTTPError as e:
try: try:
err = json.load(e) err_data = json.load(e)
except json.JSONDecodeError: error = friendly_errors.get(err_data["errcode"], err_data["error"])
err = {} except (json.JSONDecodeError, KeyError):
error = str(e)
action = "register" if register else "log in" action = "register" if register else "log in"
print(f"{Fore.RED}Failed to {action}: {err.get('error', str(e))}{Fore.RESET}") print(f"{Fore.RED}Failed to {action}: {error}{Fore.RESET}")