mirror of
https://github.com/cranci1/Sora.git
synced 2026-05-03 08:49:05 +00:00
fixed fetchV2 crash
This commit is contained in:
parent
0cd3be5401
commit
c7e7672bf3
1 changed files with 30 additions and 10 deletions
|
|
@ -111,19 +111,33 @@ extension JSContext {
|
||||||
let task = URLSession.fetchData(allowRedirects: redirect.boolValue).downloadTask(with: request) { tempFileURL, response, error in
|
let task = URLSession.fetchData(allowRedirects: redirect.boolValue).downloadTask(with: request) { tempFileURL, response, error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
Logger.shared.log("Network error in fetchV2NativeFunction: \(error.localizedDescription)", type: "Error")
|
Logger.shared.log("Network error in fetchV2NativeFunction: \(error.localizedDescription)", type: "Error")
|
||||||
reject.call(withArguments: [error.localizedDescription])
|
DispatchQueue.main.async {
|
||||||
|
reject.call(withArguments: [error.localizedDescription])
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let tempFileURL = tempFileURL else {
|
guard let tempFileURL = tempFileURL else {
|
||||||
Logger.shared.log("No data in response", type: "Error")
|
Logger.shared.log("No data in response", type: "Error")
|
||||||
reject.call(withArguments: ["No data"])
|
DispatchQueue.main.async {
|
||||||
|
reject.call(withArguments: ["No data"])
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// initialise return Object
|
|
||||||
|
var safeHeaders: [String: String] = [:]
|
||||||
|
if let httpResponse = response as? HTTPURLResponse {
|
||||||
|
for (key, value) in httpResponse.allHeaderFields {
|
||||||
|
if let keyString = key as? String,
|
||||||
|
let valueString = value as? String {
|
||||||
|
safeHeaders[keyString] = valueString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var responseDict: [String: Any] = [
|
var responseDict: [String: Any] = [
|
||||||
"status": (response as? HTTPURLResponse)?.statusCode ?? 0,
|
"status": (response as? HTTPURLResponse)?.statusCode ?? 0,
|
||||||
"headers": (response as? HTTPURLResponse)?.allHeaderFields ?? [:],
|
"headers": safeHeaders,
|
||||||
"body": ""
|
"body": ""
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -132,23 +146,29 @@ extension JSContext {
|
||||||
|
|
||||||
if data.count > 10_000_000 {
|
if data.count > 10_000_000 {
|
||||||
Logger.shared.log("Response exceeds maximum size", type: "Error")
|
Logger.shared.log("Response exceeds maximum size", type: "Error")
|
||||||
reject.call(withArguments: ["Response exceeds maximum size"])
|
DispatchQueue.main.async {
|
||||||
|
reject.call(withArguments: ["Response exceeds maximum size"])
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if let text = String(data: data, encoding: .utf8) {
|
if let text = String(data: data, encoding: .utf8) {
|
||||||
|
|
||||||
responseDict["body"] = text
|
responseDict["body"] = text
|
||||||
resolve.call(withArguments: [responseDict])
|
DispatchQueue.main.async {
|
||||||
|
resolve.call(withArguments: [responseDict])
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// rather than reject -> resolve with empty body as user can utilise reponse headers.
|
|
||||||
Logger.shared.log("Unable to decode data to text", type: "Error")
|
Logger.shared.log("Unable to decode data to text", type: "Error")
|
||||||
resolve.call(withArguments: [responseDict])
|
DispatchQueue.main.async {
|
||||||
|
resolve.call(withArguments: [responseDict])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch {
|
} catch {
|
||||||
Logger.shared.log("Error reading downloaded file: \(error.localizedDescription)", type: "Error")
|
Logger.shared.log("Error reading downloaded file: \(error.localizedDescription)", type: "Error")
|
||||||
reject.call(withArguments: ["Error reading downloaded file"])
|
DispatchQueue.main.async {
|
||||||
|
reject.call(withArguments: ["Error reading downloaded file"])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
task.resume()
|
task.resume()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue