Skip to content

Commit

Permalink
fix: check for ParseError first after server response (#332)
Browse files Browse the repository at this point in the history
* fix: check for ParseError first after server response

* nits
  • Loading branch information
cbaker6 authored Jan 24, 2022
1 parent 96232f3 commit a9dd890
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Sources/ParseSwift/Extensions/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ internal extension URLSession {
return .failure(parseError)
}
if let responseData = responseData {
if let error = try? ParseCoding.jsonDecoder().decode(ParseError.self, from: responseData) {
return .failure(error)
}
if URLSession.parse.configuration.urlCache?.cachedResponse(for: request) == nil {
URLSession.parse.configuration.urlCache?
.storeCachedResponse(.init(response: response,
data: responseData),
for: request)
}
do {
if URLSession.parse.configuration.urlCache?.cachedResponse(for: request) == nil {
URLSession.parse.configuration.urlCache?.storeCachedResponse(.init(response: response,
data: responseData),
for: request)
}
return try .success(mapper(responseData))
} catch {
if let error = try? ParseCoding.jsonDecoder().decode(ParseError.self, from: responseData) {
return .failure(error)
}
guard let parseError = error as? ParseError else {
guard JSONSerialization.isValidJSONObject(responseData),
let json = try? JSONSerialization
Expand Down Expand Up @@ -255,7 +256,8 @@ internal extension URLSession {
completion: @escaping(Result<U, ParseError>) -> Void
) {
downloadTask(with: request) { (location, urlResponse, responseError) in
completion(self.makeResult(request: request, location: location,
completion(self.makeResult(request: request,
location: location,
urlResponse: urlResponse,
responseError: responseError,
mapper: mapper))
Expand Down

0 comments on commit a9dd890

Please sign in to comment.