Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Sources/Hub/Hub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@ import Foundation
public struct Hub {}

public extension Hub {
enum HubClientError: Error {
enum HubClientError: LocalizedError {
case parse
case jsonSerialization(fileURL: URL, message: String)
case authorizationRequired
case unexpectedError
case httpStatusCode(Int)

public var errorDescription: String? {
switch self {
case .parse:
return NSLocalizedString("Failed to parse the configuration.", comment: "Parse Error")
case .jsonSerialization(_, let message):
return message
case .authorizationRequired:
return NSLocalizedString("Authorization is required.", comment: "Authorization Error")
case .unexpectedError:
return NSLocalizedString("An unexpected error occurred.", comment: "Unexpected Error")
case .httpStatusCode(let statusCode):
return String(format: NSLocalizedString("HTTP error with status code: %d", comment: "HTTP Status Code Error"), statusCode)
}
}
}

enum RepoType: String {
case models
case datasets
Expand Down
10 changes: 7 additions & 3 deletions Sources/Hub/HubApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ public extension HubApi {
/// `fileURL` is a complete local file path for the given model
func configuration(fileURL: URL) throws -> Config {
let data = try Data(contentsOf: fileURL)
let parsed = try JSONSerialization.jsonObject(with: data, options: [])
guard let dictionary = parsed as? [NSString: Any] else { throw Hub.HubClientError.parse }
return Config(dictionary)
do {
let parsed = try JSONSerialization.jsonObject(with: data, options: [])
guard let dictionary = parsed as? [NSString: Any] else { throw Hub.HubClientError.parse }
return Config(dictionary)
} catch {
throw Hub.HubClientError.jsonSerialization(fileURL: fileURL, message: "JSON Serialization failed for \(fileURL). Please verify that you have set the HUGGING_FACE_HUB_TOKEN environment variable.")
}
}
}

Expand Down