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 .release-notes/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use "json"

actor MyReceiver is req.LinkedResultReceiver
be success(json: JSONNav, link_header: String) => None
be failure(status: U16, response_body: String, message: String) => None
be failure(err: req.RequestError) => None
```

The same change applies to calls: `gh.LinkedJSONRequester(creds, url, receiver)` becomes `req.LinkedJSONRequester(creds, url, receiver)`.
Expand Down
55 changes: 55 additions & 0 deletions .release-notes/request-failure-reason.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Add a typed failure reason to RequestError

`RequestError` has a new `reason` field that says what failed, so code can decide what to do without comparing `message` strings. Before, telling a connection timeout from a TLS handshake failure meant matching `message` against `"Connection timed out"`, and a change to that wording silently changed the result.

`reason` is a `RequestFailureReason`: one of the `net` package's `ConnectionFailureReason` values (`ConnectionFailedDNS`, `ConnectionFailedTCP`, `ConnectionFailedSSL`, `ConnectionFailedTimeout`, `ConnectionFailedTimerError`) when the connection could not be opened, or one of `InvalidURL`, `RequestNotSent`, `ConnectionClosedEarly`, `InvalidHTTPResponse`, `UnexpectedStatus`, `RedirectRefused`, `InvalidJSON`, and `ConversionFailed`. The `RequestFailureReason` documentation, and the documentation of each reason this package defines, say whether retrying is safe. `status`, `response_body`, and `message` are unchanged.

Before:

```pony
| let e: RequestError =>
if e.message == "Connection timed out" then retry() end
```

After:

```pony
use "net"

| let e: RequestError =>
match e.reason
| ConnectionFailedTimeout => retry()
end
```

`RequestError` no longer has a `create` constructor. Code that builds a `RequestError`, such as a test fake for an operation, uses the constructor for the reason it needs, for example `RequestError.unexpected_status(404, body)` or `RequestError.connection_failed(ConnectionFailedTimeout)`.

Before:

```pony
let err = RequestError(404, body)
```

After:

```pony
let err = RequestError.unexpected_status(404, body)
```

The receiver interfaces that the request actors report to (`JSONRequesterResultReceiver`, `LinkedResultReceiver`, `DeleteResultReceiver`, and `CheckResultReceiver`) now receive a `RequestError` in `failure`:

Before:

```pony
be failure(status: U16, response_body: String, message: String) =>
...
```

After:

```pony
be failure(err: RequestError) =>
...
```

Code that calls `failure` on one of the library's own receivers, such as `ResultReceiver` or `PaginatedResultReceiver`, now passes a `RequestError` too, for example `receiver.failure(RequestError.unexpected_status(404, body))`.
2 changes: 1 addition & 1 deletion .release-notes/settle-promises-on-premature-close.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

Previously, request actors did not notify their receivers when a server closed the connection before completing a response, when an established connection reached the 60-second idle timeout, or when sending the HTTP request failed. High-level API operations left their `Promise` unsettled.

Now, every request actor reports a failure with status `0` to its receiver in each case. When a connection closes early, times out after the TLS handshake, or closes before the actor sends the request, the error message is `"Connection closed before response completed"`. High-level API operations fulfill their `Promise` with a `RequestError` that contains that status and message.
Now, every request actor reports a failure with status `0` to its receiver in each case. When a connection closes early or times out after the actor sends the request, the error's `reason` is `ConnectionClosedEarly` and its message is `"Connection closed before response completed"`. When the connection closes before the actor sends the request, or sending fails, the `reason` is `RequestNotSent` and the message is `"Failed to send request"`. High-level API operations fulfill their `Promise` with a `RequestError` that contains that reason, status, and message.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ primitive PrintRepository
| let repo: Repository =>
out.print(repo.full_name)
| let err: RequestError =>
out.print(err.message)
match err.reason
| ConnectionFailedTimeout => out.print("timed out; try again")
| UnexpectedStatus =>
out.print("GitHub returned status " + err.status.string())
else
out.print(err.message)
end
end
```

Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Each example has its own `Makefile`. Build with `make ssl=3.0.x` (or the SSL ver

| Example | Style | Description |
|---------|-------|-------------|
| `get-repository` | Functional | Fetch a repository by owner and name |
| `get-repository` | Functional | Fetch a repository by owner and name, and handle a failure by its reason |
| `get-repository-oo` | OO | Same as `get-repository`, using `GitHub.get_repo()` |
| `get-repository-labels` | Functional | List labels for a repository with pagination |

Expand Down
14 changes: 11 additions & 3 deletions examples/get-repository-oo/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ primitive PrintRepository
out.print(repo.html_url)
| let e: RequestError =>
out.print("Unable to retrieve repository")
out.print(e.status.string())
out.print(e.response_body)
out.print(e.message)
match e.reason
| ConnectionFailedTimeout =>
out.print("The connection timed out. Retrying is safe.")
| UnexpectedStatus if e.status == 404 =>
out.print("No such repository, or no access to it.")
| UnexpectedStatus =>
out.print("GitHub returned status " + e.status.string())
out.print(e.response_body)
else
out.print(e.message)
end
end
3 changes: 2 additions & 1 deletion examples/get-repository/get_repository.pony
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""
Demonstrates fetching information about a GitHub repository.
Demonstrates fetching information about a GitHub repository, and choosing
what to do about a failure by matching on its reason.
"""
14 changes: 11 additions & 3 deletions examples/get-repository/main.pony
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,15 @@ primitive PrintRepository
out.print(repo.html_url)
| let e: RequestError =>
out.print("Unable to retrieve repository")
out.print(e.status.string())
out.print(e.response_body)
out.print(e.message)
match e.reason
| ConnectionFailedTimeout =>
out.print("The connection timed out. Retrying is safe.")
| UnexpectedStatus if e.status == 404 =>
out.print("No such repository, or no access to it.")
| UnexpectedStatus =>
out.print("GitHub returned status " + e.status.string())
out.print(e.response_body)
else
out.print(e.message)
end
end
2 changes: 2 additions & 0 deletions github_rest_api/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ actor \nodoc\ Main is TestList
test(_TestLinkedWithLink)
test(_TestLinkedNoLink)
test(_TestLinkedFailure)
test(_TestLinkedRequesterInvalidJSON)
test(_TestBearerTokenSent)
test(_TestNoTokenNoAuthHeader)
test(_TestSearchConverterExtractsLinks)
Expand All @@ -115,6 +116,7 @@ actor \nodoc\ Main is TestList
test(_TestRepositoryGetIssuesSendsQuery)
test(_TestRepositoryGetIssuesDefaultQuery)
_OperationRequestLineTests.tests(test)
test(_TestModelTemplateInvalidURL)
test(_TestJSONRequesterSendsGitHubHeaders)
test(_TestNoContentRequesterSendsGitHubHeaders)
test(_TestCheckRequesterSendsGitHubHeaders)
Expand Down
30 changes: 30 additions & 0 deletions github_rest_api/_test_model_template_invalid_url.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use "json"
use "pony_test"
use req = "request"

class \nodoc\ _TestModelTemplateInvalidURL is UnitTest
"""
A URL template from GitHub data that does not parse gives a `RequestError`
with reason `InvalidURL`, without opening a connection.
"""
fun name(): String => "request-error/model-template-invalid-url"

fun ref apply(h: TestHelper) ? =>
let creds = _TestDummyCredentials(h)
let json =
_TestRepositoryJSON("x").update("labels_url", "https://example{.com")
let repo = RepositoryJSONConverter(JSONNav(json), creds)?
h.long_test(2_000_000_000)
repo.create_label("bug").next[None](
{(result: LabelOrError) =>
match \exhaustive\ result
| let _: Label =>
h.fail("expected a RequestError")
h.complete(false)
| let e: req.RequestError =>
h.assert_true(
e.reason is req.InvalidURL, "reason should be InvalidURL")
h.assert_eq[U16](0, e.status)
h.complete(true)
end
})
4 changes: 2 additions & 2 deletions github_rest_api/_test_operation_request_lines.pony
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ primitive \nodoc\ _OperationRequestLineTests
"2024-01-01T00:00:00Z",
per_page)
| let _: ct.ValidationFailure =>
// MakePerPage(50) cannot fail; this arm only satisfies the match.
Promise[(PaginatedList[Issue] | req.RequestError)]
.> apply(req.RequestError(
where message' = "MakePerPage(50) should succeed"))
.> apply(req.RequestError.invalid_url("MakePerPage(50) failed"))
end
} val))

Expand Down
Loading
Loading