diff --git a/Sources/EndpointBuilderURLSession/EndpointBuilderURLSession.swift b/Sources/EndpointBuilderURLSession/EndpointBuilderURLSession.swift index 5953949..f19c326 100644 --- a/Sources/EndpointBuilderURLSession/EndpointBuilderURLSession.swift +++ b/Sources/EndpointBuilderURLSession/EndpointBuilderURLSession.swift @@ -58,13 +58,17 @@ extension EndpointBuilderURLSession { /// A request with a response body @inlinable public func request(_ 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(_ endpoint: E) async throws -> Data { + func requestData(_ endpoint: E) async throws -> (Data, URLResponse) { // construct URL let url: URL = serverBaseURL.appendingPath(endpoint.path) @@ -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) } } @@ -103,3 +107,7 @@ extension URL { } } + +public enum EndpointBuilderError: Error { + case unableToDeserialize(data: Data, response: URLResponse) +}