Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
IPPError,
IPPParseError,
IPPResponseError,
IPPVersionNotSupportedError,
)
from .models import Printer # noqa
6 changes: 6 additions & 0 deletions pyipp/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ class IPPResponseError(IPPError):
"""IPP response exception."""

pass


class IPPVersionNotSupportedError(IPPError):
"""IPP version not supported."""

pass
8 changes: 6 additions & 2 deletions pyipp/ipp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
DEFAULT_PRINTER_ATTRIBUTES,
DEFAULT_PROTO_VERSION,
)
from .enums import IppOperation
from .enums import IppOperation, IppStatus
from .exceptions import (
IPPConnectionError,
IPPConnectionUpgradeRequired,
IPPError,
IPPParseError,
IPPResponseError,
IPPVersionNotSupportedError,
)
from .models import Printer
from .parser import parse as parse_response
Expand Down Expand Up @@ -180,7 +181,10 @@ async def execute(self, operation: IppOperation, message: dict) -> dict:
except (StructError, Exception) as exc: # disable=broad-except
raise IPPParseError from exc

if parsed["status-code"] != 0:
if parsed["status-code"] == IppStatus.ERROR_VERSION_NOT_SUPPORTED:
raise IPPVersionNotSupportedError("IPP version not supported by server")

if parsed["status-code"] != IppStatus.OK:
raise IPPError(
"Unexpected printer status code",
{"status-code": parsed["status-code"]},
Expand Down
4 changes: 3 additions & 1 deletion pyipp/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import struct
from typing import Any, Dict, Tuple, cast

from .enums import IppDocumentState, IppJobState, IppPrinterState, IppTag
from .enums import IppDocumentState, IppJobState, IppPrinterState, IppStatus, IppTag

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -69,6 +69,8 @@ def parse_attribute(data: bytes, offset: int):
attribute["value"] = IppPrinterState(attribute["value"])
elif attribute["name"] == "document-state":
attribute["value"] = IppDocumentState(attribute["value"])
elif attribute["name"] == "status-code":
attribute["value"] = IppStatus(attribute["value"])

offset += 4
_LOGGER.debug("Attribute Value: %s", attribute["value"])
Expand Down
Binary file not shown.
28 changes: 28 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
IPPConnectionUpgradeRequired,
IPPError,
IPPParseError,
IPPVersionNotSupportedError,
)

from . import (
Expand Down Expand Up @@ -255,3 +256,30 @@ async def test_unexpected_response(aresponses):
},
},
)


@pytest.mark.asyncio
async def test_ipp_error_0x0503(aresponses):
"""Test IPP Error 0x0503 response handling."""
aresponses.add(
MATCH_DEFAULT_HOST,
DEFAULT_PRINTER_PATH,
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/ipp"},
body=load_fixture_binary("get-printer-attributes-error-0x0503.bin"),
),
)

async with ClientSession() as session:
ipp = IPP(DEFAULT_PRINTER_URI, session=session)
with pytest.raises(IPPVersionNotSupportedError):
assert await ipp.execute(
IppOperation.GET_PRINTER_ATTRIBUTES,
{
"operation-attributes-tag": {
"requested-attributes": DEFAULT_PRINTER_ATTRIBUTES,
},
},
)