client: handle 422 errors on the client

When the client encounters a 422 HTTP error, we now raise a
RecoverableClientError. Client controllers can catch the error and print
a dialog with the error message in the UI.

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2024-04-27 01:04:24 +02:00
parent b45da42346
commit 1cca91f9fe
1 changed files with 9 additions and 1 deletions

View File

@ -16,6 +16,7 @@
import asyncio
import contextvars
import inspect
import json
import logging
import os
import signal
@ -59,6 +60,10 @@ class Abort(Exception):
self.error_report_ref = error_report_ref
class RecoverableClientError(ValueError):
pass
DEBUG_SHELL_INTRO = _(
"""\
Installer shell session activated.
@ -206,7 +211,10 @@ class SubiquityClient(TuiApplication):
raise Abort(ref)
try:
response.raise_for_status()
except aiohttp.ClientError:
except aiohttp.ClientError as exc:
if isinstance(exc, aiohttp.ClientResponseError) and exc.status == 422:
raise RecoverableClientError(json.loads(headers["x-error-msg"]))
report = self.error_reporter.make_apport_report(
ErrorReportKind.SERVER_REQUEST_FAIL,
"request to {}".format(response.url.path),