Skip to content

exceptions

napt.exceptions

Exception hierarchy for NAPT.

This module defines a custom exception hierarchy for distinguishing between different types of errors. All exceptions inherit from NAPTError, allowing callers to catch all NAPT errors with a single except clause if needed.

NAPTError

Bases: Exception

Base exception for all NAPT errors.

All NAPT-specific exceptions inherit from this class, allowing callers to catch all NAPT errors with a single except clause if needed.

Source code in napt/exceptions.py
class NAPTError(Exception):
    """Base exception for all NAPT errors.

    All NAPT-specific exceptions inherit from this class, allowing callers
    to catch all NAPT errors with a single except clause if needed.
    """

    pass

ConfigError

Bases: NAPTError

Raised for configuration-related errors.

This exception is raised when there are problems with:

  • YAML parse errors (syntax errors, invalid structure)
  • Missing required configuration fields (e.g., no 'name', no 'discovery.strategy')
  • Invalid strategy configuration (unknown strategy name, invalid strategy parameters)
  • Missing recipe files (file not found)
  • Recipe validation failures (wrong field types, values out of range)
  • A configured path that does not exist (e.g., the brand pack folder)
Example

Catching configuration errors:

from napt.exceptions import ConfigError

try:
    config = load_effective_config(Path("invalid.yaml"))
except ConfigError as e:
    print(f"Config error: {e}")

Source code in napt/exceptions.py
class ConfigError(NAPTError):
    """Raised for configuration-related errors.

    This exception is raised when there are problems with:

    - YAML parse errors (syntax errors, invalid structure)
    - Missing required configuration fields (e.g., no 'name', no
        'discovery.strategy')
    - Invalid strategy configuration (unknown strategy name, invalid strategy
        parameters)
    - Missing recipe files (file not found)
    - Recipe validation failures (wrong field types, values out of range)
    - A configured path that does not exist (e.g., the brand pack folder)

    Example:
        Catching configuration errors:
            ```python
            from napt.exceptions import ConfigError

            try:
                config = load_effective_config(Path("invalid.yaml"))
            except ConfigError as e:
                print(f"Config error: {e}")
            ```
    """

    pass

NetworkError

Bases: NAPTError

Raised for network/download-related errors.

This exception is raised when there are problems with:

  • Download failures (HTTP errors, connection timeouts, network unreachable)
  • API call failures (GitHub API errors, JSON API endpoint failures, authentication issues)
  • Network-related version extraction errors (API response parsing failures)

Attributes:

Name Type Description
status_code

The HTTP status of the failed response, when the error came from one; None for a transport failure or any other cause.

Example

Catching network errors:

from napt.exceptions import NetworkError

try:
    result = discover_recipe(Path("recipe.yaml"), Path("./downloads"))
except NetworkError as e:
    print(f"Network error: {e}")

Source code in napt/exceptions.py
class NetworkError(NAPTError):
    """Raised for network/download-related errors.

    This exception is raised when there are problems with:

    - Download failures (HTTP errors, connection timeouts, network
        unreachable)
    - API call failures (GitHub API errors, JSON API endpoint failures,
        authentication issues)
    - Network-related version extraction errors (API response parsing
        failures)

    Attributes:
        status_code: The HTTP status of the failed response, when the error
            came from one; None for a transport failure or any other cause.

    Example:
        Catching network errors:
            ```python
            from napt.exceptions import NetworkError

            try:
                result = discover_recipe(Path("recipe.yaml"), Path("./downloads"))
            except NetworkError as e:
                print(f"Network error: {e}")
            ```
    """

    def __init__(self, message: str, *, status_code: int | None = None) -> None:
        """Creates the error with its message and, when known, the HTTP status.

        Args:
            message: What failed, for the user.
            status_code: The HTTP status of the failed response, if any.
        """
        super().__init__(message)
        self.status_code = status_code

__init__

__init__(message: str, *, status_code: int | None = None) -> None

Creates the error with its message and, when known, the HTTP status.

Parameters:

Name Type Description Default
message str

What failed, for the user.

required
status_code int | None

The HTTP status of the failed response, if any.

None
Source code in napt/exceptions.py
def __init__(self, message: str, *, status_code: int | None = None) -> None:
    """Creates the error with its message and, when known, the HTTP status.

    Args:
        message: What failed, for the user.
        status_code: The HTTP status of the failed response, if any.
    """
    super().__init__(message)
    self.status_code = status_code

PackagingError

Bases: NAPTError

Raised for packaging/build-related errors.

This exception is raised when there are problems with:

  • Build failures (PSADT template processing errors, file operations, directory creation failures)
  • Missing build tools (IntuneWinAppUtil.exe not found, PSADT template missing)
  • MSI extraction errors (failed to read MSI ProductVersion, unsupported MSI format)
  • Packaging operations (IntuneWinAppUtil.exe execution failures, invalid build directory structure)
  • Malformed .intunewin files (invalid ZIP structure, missing Detection.xml, missing required encryption fields)
Example

Catching packaging errors:

from napt.exceptions import PackagingError

try:
    build_package(Path("recipe.yaml"), Path("./builds"))
except PackagingError as e:
    print(f"Packaging error: {e}")

Source code in napt/exceptions.py
class PackagingError(NAPTError):
    """Raised for packaging/build-related errors.

    This exception is raised when there are problems with:

    - Build failures (PSADT template processing errors, file operations,
        directory creation failures)
    - Missing build tools (IntuneWinAppUtil.exe not found, PSADT template
        missing)
    - MSI extraction errors (failed to read MSI ProductVersion, unsupported
        MSI format)
    - Packaging operations (IntuneWinAppUtil.exe execution failures, invalid
        build directory structure)
    - Malformed .intunewin files (invalid ZIP structure, missing Detection.xml,
        missing required encryption fields)

    Example:
        Catching packaging errors:
            ```python
            from napt.exceptions import PackagingError

            try:
                build_package(Path("recipe.yaml"), Path("./builds"))
            except PackagingError as e:
                print(f"Packaging error: {e}")
            ```
    """

    pass

StateError

Bases: NAPTError

Raised for persisted-state integrity errors.

This exception is raised when NAPT's own persisted state (the deployment state and plan files under state/) is unreadable or invalid:

  • Corrupted deployment state or plan files (invalid JSON, wrong shape)
  • Invalid deployment state fields (e.g., an unparseable ring entered_at timestamp, or a recorded version that cannot be used as a folder name)

Deployment state is authoritative and never auto-replaced; the error message says how to recover.

Example

Catching state errors:

from napt.exceptions import StateError

try:
    state = load_deployment_state(path)
except StateError as e:
    print(f"State error: {e}")

Source code in napt/exceptions.py
class StateError(NAPTError):
    """Raised for persisted-state integrity errors.

    This exception is raised when NAPT's own persisted state (the
    deployment state and plan files under ``state/``) is unreadable or
    invalid:

    - Corrupted deployment state or plan files (invalid JSON, wrong shape)
    - Invalid deployment state fields (e.g., an unparseable ring
        ``entered_at`` timestamp, or a recorded version that cannot be used
        as a folder name)

    Deployment state is authoritative and never auto-replaced; the error
    message says how to recover.

    Example:
        Catching state errors:
            ```python
            from napt.exceptions import StateError

            try:
                state = load_deployment_state(path)
            except StateError as e:
                print(f"State error: {e}")
            ```
    """

    pass

AuthError

Bases: NAPTError

Raised for authentication and authorization errors.

This exception is raised when there are problems with:

  • No credential available (no service principal / OIDC environment and no session from 'napt auth login')
  • Graph API 401 Unauthorized or 403 Forbidden responses
  • Interactive sign-in failure, timeout, or cancellation
  • A cached sign-in session that can no longer be refreshed
Example

Catching auth errors:

from napt.exceptions import AuthError

try:
    result = upload_package(Path("recipe.yaml"))
except AuthError as e:
    print(f"Auth error: {e}")

Source code in napt/exceptions.py
class AuthError(NAPTError):
    """Raised for authentication and authorization errors.

    This exception is raised when there are problems with:

    - No credential available (no service principal / OIDC environment and
        no session from 'napt auth login')
    - Graph API 401 Unauthorized or 403 Forbidden responses
    - Interactive sign-in failure, timeout, or cancellation
    - A cached sign-in session that can no longer be refreshed

    Example:
        Catching auth errors:
            ```python
            from napt.exceptions import AuthError

            try:
                result = upload_package(Path("recipe.yaml"))
            except AuthError as e:
                print(f"Auth error: {e}")
            ```
    """

    pass

NotModifiedError

Bases: Exception

Raised when a conditional HTTP request finds the content unchanged.

This exception is raised when download_file() receives HTTP 304, or a 200 that repeats the ETag or Last-Modified it was sent (a server that ignores conditional requests), meaning the remote content has not changed since the last request. The function is expected to return a file on disk, and there is no new file to return, so it raises instead. The message names which it was: HTTP 304, unchanged ETag, or unchanged Last-Modified.

Inherits from Exception (not NAPTError) so that broad catches of all NAPT errors do not suppress it. Callers that supply an ETag must handle this explicitly.

Example

Handling a conditional download:

from napt.exceptions import NotModifiedError

try:
    result = download_file(url, dest, etag=cached_etag)
except NotModifiedError:
    # Server confirmed content unchanged, use the cached file
    pass

Source code in napt/exceptions.py
class NotModifiedError(Exception):
    """Raised when a conditional HTTP request finds the content unchanged.

    This exception is raised when download_file() receives HTTP 304, or a
    200 that repeats the ETag or Last-Modified it was sent (a server that
    ignores conditional requests), meaning the remote content has not
    changed since the last request. The function is expected to return a
    file on disk, and there is no new file to return, so it raises instead.
    The message names which it was: ``HTTP 304``, ``unchanged ETag``, or
    ``unchanged Last-Modified``.

    Inherits from Exception (not NAPTError) so that broad catches of all NAPT
    errors do not suppress it. Callers that supply an ETag must handle this
    explicitly.

    Example:
        Handling a conditional download:
            ```python
            from napt.exceptions import NotModifiedError

            try:
                result = download_file(url, dest, etag=cached_etag)
            except NotModifiedError:
                # Server confirmed content unchanged, use the cached file
                pass
            ```
    """

    pass