try{300,301,400}: fix

This commit is contained in:
Jörg Thalheim
2025-08-26 15:14:23 +02:00
parent 0a1802c341
commit cb9c8e5b5a
9 changed files with 30 additions and 33 deletions

View File

@@ -148,8 +148,8 @@ class HttpBridge(ApiBridge, BaseHTTPRequestHandler):
self.send_header("Content-Type", content_type)
self.end_headers()
self.wfile.write(file_data)
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as e:
log.error(f"Error reading Swagger file: {e!s}")
except (OSError, json.JSONDecodeError, UnicodeDecodeError):
log.exception("Error reading Swagger file")
self.send_error(500, "Internal Server Error")
def _get_swagger_file_path(self, rel_path: str) -> Path:
@@ -264,10 +264,10 @@ class HttpBridge(ApiBridge, BaseHTTPRequestHandler):
"""Read and parse the request body. Returns None if there was an error."""
try:
content_length = int(self.headers.get("Content-Length", 0))
if content_length > 0:
body = self.rfile.read(content_length)
return json.loads(body.decode("utf-8"))
return {}
if content_length == 0:
return {}
body = self.rfile.read(content_length)
return json.loads(body.decode("utf-8"))
except json.JSONDecodeError:
self.send_api_error_response(
"post",

View File

@@ -539,11 +539,11 @@ def main() -> None:
try:
args.func(args)
except ClanError as e:
except ClanError:
if debug:
log.exception("Exited with error")
else:
log.error("%s", e)
log.exception("Exited with error")
sys.exit(1)
except KeyboardInterrupt as ex:
log.warning("Interrupted by user", exc_info=ex)

View File

@@ -71,7 +71,7 @@ def create_clan(opts: CreateOptions) -> None:
try:
nix_metadata(str(opts.src_flake))
except ClanError:
log.error(
log.exception(
f"Found a repository, but it is not a valid flake: {opts.src_flake}",
)
log.warning("Setting src_flake to None")

View File

@@ -45,7 +45,6 @@ def check_machine_ssh_login(
["true"],
RunOpts(timeout=opts.timeout, needs_user_terminal=True),
)
return
except ClanCmdTimeoutError as e:
msg = f"SSH login timeout after {opts.timeout}s"
raise ClanError(msg) from e
@@ -54,6 +53,8 @@ def check_machine_ssh_login(
raise ClanError(e.cmd.stderr.strip()) from e
msg = f"SSH login failed: {e}"
raise ClanError(msg) from e
else:
return
@API.register

View File

@@ -158,15 +158,15 @@ def read_qr_image(image_path: Path) -> dict[str, Any]:
try:
res = run(cmd)
data = res.stdout.strip()
if not data:
msg = f"No QR code found in image: {image_path}"
raise ClanError(msg)
return json.loads(data)
except json.JSONDecodeError as e:
msg = f"Invalid JSON in QR code: {e}"
raise ClanError(msg) from e
except Exception as e:
except OSError as e:
msg = f"Failed to read QR code from {image_path}: {e}"
raise ClanError(msg) from e
if not data:
msg = f"No QR code found in image: {image_path}"
raise ClanError(msg)
return json.loads(data)

View File

@@ -82,14 +82,14 @@ class SudoAskpassProxy:
prompt = stripped_line[len("PASSWORD_REQUESTED:") :].strip()
password = self.handle_password_request(prompt)
if ssh_process.stdin is None:
msg = "SSH process stdin is None"
raise ClanError(msg)
logger.error("SSH process stdin is None")
return
print(password, file=ssh_process.stdin)
ssh_process.stdin.flush()
else:
print(stripped_line)
except (OSError, ClanError) as e:
logger.error(f"Error processing passwords requests output: {e}")
except (OSError, ClanError):
logger.exception("Error processing passwords requests output")
def run(self) -> str:
"""Run the SSH command with password proxying. Returns the askpass script path."""

View File

@@ -89,8 +89,8 @@ def machine_template(
try:
yield dst_machine_dir
except Exception as e:
log.error(f"An error occurred inside the 'machine_template' context: {e}")
except Exception:
log.exception("An error occurred inside the 'machine_template' context")
# Ensure that the directory is removed to avoid half-created machines
# Everything in the with block is considered part of the context
@@ -182,7 +182,7 @@ def clan_template(
try:
post_process(dst_dir)
except Exception as e:
log.error(f"Error during post-processing of clan template: {e}")
log.exception("Error during post-processing of clan template")
log.info(f"Removing left-over directory: {dst_dir}")
shutil.rmtree(dst_dir, ignore_errors=True)
msg = (
@@ -191,8 +191,8 @@ def clan_template(
raise ClanError(msg) from e
try:
yield dst_dir
except Exception as e:
log.error(f"An error occurred inside the 'clan_template' context: {e}")
except Exception:
log.exception("An error occurred inside the 'clan_template' context")
log.info(f"Removing left-over directory: {dst_dir}")
shutil.rmtree(dst_dir, ignore_errors=True)
raise

View File

@@ -340,11 +340,9 @@ class VMObject(GObject.Object):
# Try to shutdown the VM gracefully using QMP
try:
if self.qmp_wrap is None:
msg = "QMP wrapper is not available"
raise ClanError(msg)
with self.qmp_wrap.qmp_ctx() as qmp:
qmp.command("system_powerdown")
if self.qmp_wrap is not None:
with self.qmp_wrap.qmp_ctx() as qmp:
qmp.command("system_powerdown")
except (ClanError, OSError, ConnectionError) as ex:
log.debug(f"QMP command 'system_powerdown' ignored. Error: {ex}")

View File

@@ -62,8 +62,6 @@ lint.ignore = [
"PLR0912",
"PLR0913",
"PLR0915",
"TRY300",
"TRY301",
"FBT003",
"INP001",
]