# SPDX-License-Identifier: MIT
# Copyright (c) 2001-{{ COPYRIGHT_END_YEAR }} JCMwave GmbH
"""Exception hierarchy for `jcmsuite`."""
from __future__ import annotations
from collections.abc import Mapping
from pathlib import Path
from typing import Literal
[docs]
class JCMsuiteError(Exception):
"""Common parent for errors reported by `jcmsuite`."""
[docs]
class RuntimeContextError(JCMsuiteError):
"""Error raised when JCMsuite settings are missing or cannot be used."""
[docs]
class InvalidConfigurationError(JCMsuiteError):
"""Error raised when an input or setting is not valid."""
[docs]
class UnsupportedExportError(JCMsuiteError, NotImplementedError):
"""Error raised when result data cannot use the requested export format."""
[docs]
class BackendUnavailableError(JCMsuiteError):
"""Error raised when the requested execution method is not available."""
[docs]
class SolveExecutionError(RuntimeError):
"""Error raised when a local or daemon solve fails.
Public solve and deferred-wait calls trim deep internal traceback frames
before they re-raise this error. The exception and its solver diagnostics
remain available to callers.
Parameters
----------
backend : Literal["local", "daemon"]
Backend that executed the solve.
exit_code : int
Exit code reported by the solver or daemon.
log : str
Diagnostic text reported by the backend.
job_id : int | None
Daemon job identifier, or `None` for a local solve.
job_info : Mapping[str, object] | None
Additional daemon information, or `None` for a local solve.
Attributes
----------
backend : Literal["local", "daemon"]
Backend that executed the solve.
exit_code : int
Exit code reported by the solver or daemon.
log : str
Diagnostic text reported by the backend.
job_id : int | None
Daemon job identifier, or `None` for a local solve.
job_info : dict[str, object] | None
Additional daemon information, or `None` for a local solve.
"""
def __init__(
self,
*,
backend: Literal["local", "daemon"],
exit_code: int,
log: str,
job_id: int | None = None,
job_info: Mapping[str, object] | None = None,
) -> None:
self.backend: Literal["local", "daemon"] = backend
self.job_id: int | None = job_id
self.exit_code: int = exit_code
self.log: str = log
self.job_info: dict[str, object] | None = (
None if job_info is None else dict(job_info)
)
message: str
if backend == "local":
message = f"*** JCMsolve failed. ==>\n\n{log}"
elif log.strip():
message = (
f"Daemon job {job_id} failed with exit code {exit_code}. ==>\n\n"
f"{log}"
)
else:
message = (
f"Daemon job {job_id} failed with exit code {exit_code}, "
"but daemon returned an empty log. "
f"Raw job info: {self.job_info!r}"
)
super().__init__(message)
[docs]
class ContextConflictError(JCMsuiteError):
"""Error raised when one operation receives two different contexts."""
[docs]
class SourceMappingError(JCMsuiteError):
"""Error raised for a problem at a known position in a source file.
Parameters
----------
message : str
Description of the problem.
source : str | Path | None, optional
Source file associated with the diagnostic.
line : int | None, optional
One-based source line, if known.
column : int | None, optional
One-based source column, if known.
"""
def __init__(
self,
message: str,
*,
source: str | Path | None = None,
line: int | None = None,
column: int | None = None,
) -> None:
source_text: str = str(source) if source is not None else "<unknown>"
line_text: str = "?" if line is None else str(line)
column_text: str = "?" if column is None else str(column)
detail: str = (
f"{message} (source={source_text}, line={line_text}, column={column_text})"
)
super().__init__(detail)
self.source: str | Path | None = source
self.line: int | None = line
self.column: int | None = column
[docs]
class TemplateMappingError(SourceMappingError):
"""Error raised for a problem in a JCMsuite template file.
Parameters
----------
message : str
Description of the problem.
source : str | Path | None, optional
Template file associated with the diagnostic.
line : int | None, optional
One-based source line, if known.
column : int | None, optional
One-based source column, if known.
"""
[docs]
class ConfigMappingError(SourceMappingError):
"""Error raised for a JCMsuite input error at a known source position.
Parameters
----------
message : str
Description of the problem.
source : str | Path | None, optional
Original JCMsuite input file associated with the diagnostic. For daemon
execution in a separate working directory, this is the source file, not
its staged copy.
line : int | None, optional
One-based source line, if known.
column : int | None, optional
One-based source column, if known.
"""