Skip to content

Commit

Permalink
Return data and URLResponse with deserialization errors (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hallee authored Feb 17, 2024
1 parent 26a053b commit 8bf8fb3
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Sources/EndpointBuilderURLSession/EndpointBuilderURLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,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 @@ -82,8 +86,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 @@ -103,3 +107,7 @@ extension URL {
}

}

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

0 comments on commit 8bf8fb3

Please sign in to comment.