Skip to content

Commit

Permalink
Tweak executor error messages (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth authored Oct 24, 2023
1 parent 825ce3c commit 51d8610
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
7 changes: 4 additions & 3 deletions opvious/executors/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from .common import (
CONTENT_TYPE_HEADER,
Executor,
ExecutorError,
ExecutorResult,
JsonExecutorResult,
JsonSeqExecutorResult,
PlainTextExecutorResult,
Headers,
TRACE_HEADER,
unexpected_response_error,
unsupported_content_type_error,
)

Expand Down Expand Up @@ -101,6 +101,7 @@ async def _send(
trace = None
if isinstance(err.headers, list):
trace = next(
(v for (k, v) in err.headers if k == TRACE_HEADER), None
(v for (k, v) in err.headers if k == TRACE_HEADER),
None,
)
raise unexpected_response_error(message=err.message, trace=trace)
raise ExecutorError(status=err.status, trace=trace) from err
26 changes: 7 additions & 19 deletions opvious/executors/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@
class ExecutorError(Exception):
"""Local representation of an error during an executor's request"""

status: int
status: Optional[int]
trace: Optional[str]
reason: Optional[str]

def __init__(
self,
status: int,
status: Optional[int] = None,
trace: Optional[str] = None,
reason: Optional[Any] = None,
) -> None:
message = f"Request failed with status {status}"
message = "Request errored"
if status and status >= 400:
message += f" with status {status}"
if trace:
message += f" ({trace})"
if reason:
Expand All @@ -56,25 +58,11 @@ def __init__(
self.reason = reason


def unexpected_response_error(
message: str, trace: Optional[str] = None
) -> Exception:
return Exception(
"Unexpected response"
+ (f" ({trace})" if trace else "")
+ (f": {message}" if message else "")
)


def unsupported_content_type_error(
text: str, content_type: Optional[str], trace: Optional[str] = None
) -> Exception:
return unexpected_response_error(
message=(
f"unsupported content-type ({content_type or '<none>'}): {text}"
),
trace=trace,
)
reason = f"unsupported content-type ({content_type or '<none>'}): {text}"
return ExecutorError(trace=trace, reason=reason)


@dataclasses.dataclass
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "opvious"
version = "0.18.3rc1"
version = "0.18.4rc1"
description = "Opvious Python SDK"
authors = ["Opvious Engineering <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 51d8610

Please sign in to comment.