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
6 changes: 3 additions & 3 deletions Sources/TypedNetwork/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Foundation
import FoundationNetworking
#endif

actor APIClient {
public actor APIClient {

private let session: NetworkSession
private let builder: RequestBuilder
private let mockRegistry: MockRegistry?
private let middlewares: [any Middleware]
private let decoder: ResponseDecoder

init(
public init(
baseURL: URL,
session: NetworkSession = URLSession.shared,
mockRegistry: MockRegistry? = nil,
Expand All @@ -26,7 +26,7 @@ actor APIClient {
self.decoder = decoder
}

func send<E: Endpoint>(_ endpoint: E) async throws -> E.Response {
public func send<E: Endpoint>(_ endpoint: E) async throws -> E.Response {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle non-2xx responses for Never endpoints

Making send public exposes the existing non-2xx path to downstream clients whose endpoints use the protocol default Failure == Never. In that common case, any 4xx/5xx response calls the default Endpoint.mapError, which unconditionally fatalErrors instead of throwing NetworkError, so a public API request can crash the app on an ordinary server error unless every consumer defines a custom failure mapper.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codex address that feedback

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

  • Updated APIClient.send(_:) to guard the non-2xx path for endpoints whose Failure is Never, so it no longer calls mapError (which previously triggered a fatalError via the default implementation). It now throws NetworkError.endpoint with an internal fallback error instead.
  • Added a regression test covering a Failure == Never endpoint receiving a 500 response, verifying that the client throws instead of crashing.
  • Committed changes on branch work with commit message: Handle non-2xx responses for Never-failure endpoints.

Testing

  • swift test
  • git status --short
  • git commit -m "Handle non-2xx responses for Never-failure endpoints"

Additionally, I created the follow-up PR message via make_pr summarizing the fix and validation.

View task →

if let mock = await mockRegistry?.response(for: endpoint) as? E.Response {
return mock
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/TypedNetwork/Middleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public protocol Middleware: Sendable {
) async throws -> (Data, HTTPURLResponse)
}

public struct MiddlewareChain: Sendable {
struct MiddlewareChain: Sendable {
private let middlewares: [any Middleware]

public init(_ middlewares: [any Middleware]) {
init(_ middlewares: [any Middleware]) {
self.middlewares = middlewares
}

Expand Down
Loading