Permalink
Browse files

Create URLSession+BadResponseErrors.swift

  • Loading branch information...
mxcl committed Nov 3, 2017
1 parent 5c40f1e commit 1c8f706f4f87f1236bcfe35ba24cbaadb9a210a5
Showing with 20 additions and 0 deletions.
  1. +20 −0 Documentation/Examples/URLSession+BadResponseErrors.swift
@@ -0,0 +1,20 @@
Promise(.pending) { seal in
URLSession.shared.dataTask(with: rq, completionHandler: { data, rsp, error in
if let data = data {
seal.fulfill(data)
} else if let error = error {
if case URLError.badServerResponse = error, let rsp = rsp as? HTTPURLResponse {
seal.reject(Error.badResponse(rsp.statusCode))
} else {
seal.reject(error)
}
} else {
seal.reject(PMKError.invalidCallingConvention)
}
})
}
enum Error: Swift.Error {
case badUrl
case badResponse(Int)
}

0 comments on commit 1c8f706

Please sign in to comment.