Skip to content

Return data and URLResponse with deserialization errors #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
18 changes: 13 additions & 5 deletions Sources/EndpointBuilderURLSession/EndpointBuilderURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ extension EndpointBuilderURLSession {
/// A request with a response body
@inlinable
public func request<E: Endpoint>(_ endpoint: E) async throws -> E.Response {
let data = try await requestData(endpoint)
return try decoder().decode(E.responseType, from: data)
let (data, response) = try await requestData(endpoint)
do {
return try decoder().decode(E.responseType, from: data)
} catch {
throw EndpointBuilderError.unableToDeserialize(data: data, response: response)
}
}

@discardableResult
@usableFromInline
func requestData<E: Endpoint>(_ endpoint: E) async throws -> Data {
func requestData<E: Endpoint>(_ endpoint: E) async throws -> (Data, URLResponse) {
// construct URL
let url: URL = serverBaseURL.appendingPath(endpoint.path)

Expand All @@ -74,8 +78,8 @@ extension EndpointBuilderURLSession {
}

// perform request
let (data, _) = try await urlSession().data(for: request)
return data
let (data, response) = try await urlSession().data(for: request)
return (data, response)
}

}
Expand All @@ -95,3 +99,7 @@ extension URL {
}

}

public enum EndpointBuilderError: Error {
case unableToDeserialize(data: Data, response: URLResponse)
}