Skip to content

Commit 9ba8173

Browse files
committed
Throwing error when the configs fail JSON serialization (#114)
* Added error for JSON serialization errors * Fix merge commit --------- Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
1 parent 4b3f7f6 commit 9ba8173

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

Sources/Hub/Hub.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public extension Hub {
1414
case authorizationRequired
1515
case httpStatusCode(Int)
1616
case parse
17+
case jsonSerialization(fileURL: URL, message: String)
1718
case unexpectedError
1819
case downloadError(String)
1920
case fileNotFound(String)
@@ -31,6 +32,8 @@ public extension Hub {
3132
String(localized: "HTTP error with status code: \(code)")
3233
case .parse:
3334
String(localized: "Failed to parse server response.")
35+
case .jsonSerialization(_, let message):
36+
return message
3437
case .unexpectedError:
3538
String(localized: "An unexpected error occurred.")
3639
case let .downloadError(message):

Sources/Hub/HubApi.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ public extension HubApi {
268268
/// `fileURL` is a complete local file path for the given model
269269
func configuration(fileURL: URL) throws -> Config {
270270
let data = try Data(contentsOf: fileURL)
271-
let parsed = try JSONSerialization.bomPreservingJsonObject(with: data)
271+
guard let parsed = try? JSONSerialization.bomPreservingJsonObject(with: data) else {
272+
throw Hub.HubClientError.jsonSerialization(fileURL: fileURL, message: "JSON Serialization failed for \(fileURL). Please verify that you have set the HF_TOKEN environment variable.")
273+
}
272274
guard let dictionary = parsed as? [NSString: Any] else { throw Hub.HubClientError.parse }
273275
return Config(dictionary)
274276
}

0 commit comments

Comments
 (0)