Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ When no token is passed, the clients read `PROOFLINE_TOKEN` from the environment

## Errors and refusals

A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. The token is redacted from every text field, and `repr` and `str` show only the status.
A refused request raises `ProoflineError` with the server problem's `status`, `type`, `title` and `detail`. When a review request creates nothing, the error also carries `refusal`, whose `reason` names the setting to change, such as `NO_LINKED_ENVIRONMENT` or `ENVIRONMENT_NOT_READY`, and `repository_name`. `RunNowRefusal` names the type of `refusal`. A refusal that does not match the public contract keeps its status, `title` and `detail` with the type `urn:proofline:sdk:invalid-response` and no `refusal`. The token is redacted from every text field, and `repr` and `str` show only the status. `ProoflineError` survives `pickle` and `copy` with all of these fields, so an error raised in a `ProcessPoolExecutor` or `multiprocessing` worker reaches the caller typed.

```python
from proofline import ProoflineError
Expand Down
6 changes: 6 additions & 0 deletions proofline/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def __init__(
def __repr__(self) -> str:
return f"ProoflineError(status={self.status})"

def __reduce__(self) -> tuple[Any, ...]:
# BaseException rebuilds an error as type(self)(*self.args), and args holds
# only the status message. Rebuild from the fields and restore the rest, so
# pickle, copy and process pools keep the typed problem and its refusal.
return type(self), (self.status, self.type, self.detail), self.__dict__.copy()


def token_value(token: str | SecretStr | None) -> SecretStr:
secret = (
Expand Down
86 changes: 86 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import annotations

import asyncio
import copy
import gzip
import inspect
import json
import logging
import multiprocessing
import pickle
import socket
import subprocess
import sys
Expand All @@ -13,6 +16,7 @@
import tomllib
import traceback
from collections.abc import AsyncIterator
from concurrent.futures import ProcessPoolExecutor
from contextlib import asynccontextmanager
from datetime import UTC, datetime, timedelta
from pathlib import Path
Expand Down Expand Up @@ -606,6 +610,7 @@ async def test_review_requests_return_each_documented_refusal(
assert decoded["environment"]["display_name"] == f"{PRIVATE} [redacted]"
diagnostic = repr(error) + str(error) + "".join(traceback.format_exception(error))
assert PRIVATE not in diagnostic and TOKEN not in diagnostic
assert_round_trips(error)
assert len(server.requests) == len(REVIEW_REQUESTS)


Expand Down Expand Up @@ -684,6 +689,87 @@ async def test_problem_without_detail_keeps_status_and_title(mode: str) -> None:
assert len(server.requests) == 1


def problem_fields(error: ProoflineError) -> tuple[Any, ...]:
refusal = None if error.refusal is None else error.refusal.to_dict()
return (
type(error),
error.status,
error.type,
error.detail,
error.title,
refusal,
error.repository_name,
str(error),
repr(error),
)


def assert_round_trips(error: ProoflineError) -> None:
copies = [copy.copy(error), copy.deepcopy(error)]
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
data = pickle.dumps(error, protocol=protocol)
assert TOKEN.encode() not in data
copies.append(pickle.loads(data))
for restored in copies:
assert restored is not error
assert problem_fields(restored) == problem_fields(error)


@pytest.mark.parametrize(
"error",
[
_transport.invalid_response(),
_transport.timeout_error(),
_transport.redirect_error(307),
ProoflineError(409, "urn:proofline:conflict", "busy", title="Conflict"),
],
ids=["invalid-response", "timeout", "redirect", "keyword-fields"],
)
def test_sdk_problems_round_trip_through_pickle_and_copy(error: ProoflineError) -> None:
error.add_note("kept with the error state")
assert_round_trips(error)
assert pickle.loads(pickle.dumps(error)).__notes__ == ["kept with the error state"]


def finding_in_worker(url: str) -> CustomerFinding:
with ProoflineClient(SecretStr(TOKEN), base_url=url) as client:
return client.get_finding(ORG, FINDING)


def test_problem_raised_in_a_worker_process_reaches_the_caller_typed() -> None:
with Server(
lambda request, index: Reply(
403,
{
"type": "urn:proofline:forbidden",
"title": "Forbidden",
"detail": f"{PRIVATE} {TOKEN}",
},
)
) as server:
context = multiprocessing.get_context("spawn")
with ProcessPoolExecutor(max_workers=1, mp_context=context) as pool:
with pytest.raises(ProoflineError) as raised:
pool.submit(finding_in_worker, server.url).result(timeout=60)
# The pool survives the refusal and keeps serving later work.
with pytest.raises(ProoflineError) as again:
pool.submit(finding_in_worker, server.url).result(timeout=60)
for error in (raised.value, again.value):
assert (error.status, error.type, error.title) == (
403,
"urn:proofline:forbidden",
"Forbidden",
)
assert error.detail == f"{PRIVATE} [redacted]"
assert repr(error) == "ProoflineError(status=403)"
# The pool chains the worker's formatted traceback as __cause__; it stays static.
assert error.__cause__ is not None
diagnostic = "".join(traceback.format_exception(error))
assert "Proofline request failed (HTTP 403)." in diagnostic
assert PRIVATE not in diagnostic and TOKEN not in diagnostic
assert len(server.requests) == 2


@MODES
@pytest.mark.parametrize("status", [401, 403, 404])
async def test_authority_problems_do_not_retry_or_disclose_secrets(
Expand Down