From d2099d65afafd9465276d1c91d7a44b3c4592688 Mon Sep 17 00:00:00 2001 From: cranci1 <100066266+cranci1@users.noreply.github.com> Date: Fri, 21 Mar 2025 16:44:40 +0100 Subject: [PATCH] fixed crash --- Sora/Utils/JSLoader/JSController.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sora/Utils/JSLoader/JSController.swift b/Sora/Utils/JSLoader/JSController.swift index b3a0f92..a0a220b 100644 --- a/Sora/Utils/JSLoader/JSController.swift +++ b/Sora/Utils/JSLoader/JSController.swift @@ -23,6 +23,9 @@ class JSController: ObservableObject { context = JSContext() setupContext() context.evaluateScript(script) + if let exception = context.exception { + Logger.shared.log("Error loading script: \(exception)", type: "Error") + } } func fetchSearchResults(keyword: String, module: ScrapingModule, completion: @escaping ([SearchItem]) -> Void) { @@ -196,10 +199,13 @@ class JSController: ObservableObject { let data = jsonString.data(using: .utf8) { do { if let array = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] { - let resultItems = array.map { item -> SearchItem in - let title = item["title"] as? String ?? "" - let imageUrl = item["image"] as? String ?? "https://s4.anilist.co/file/anilistcdn/character/large/default.jpg" - let href = item["href"] as? String ?? "" + let resultItems = array.compactMap { item -> SearchItem? in + guard let title = item["title"] as? String, + let imageUrl = item["image"] as? String, + let href = item["href"] as? String else { + Logger.shared.log("Missing or invalid data in search result item: \(item)", type: "Error") + return nil + } return SearchItem(title: title, imageUrl: imageUrl, href: href) }