Skip to content

Commit 95556b1

Browse files
Improve errors (#237)
1 parent 11cab4c commit 95556b1

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

Applications/StableDiffusionExample/ContentView.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,18 @@ actor ModelFactory {
105105
case loaded(ModelContainer<TextToImageGenerator>)
106106
}
107107

108-
enum SDError: Error {
108+
enum SDError: LocalizedError {
109109
case unableToLoad
110+
111+
var errorDescription: String? {
112+
switch self {
113+
case .unableToLoad:
114+
return String(
115+
localized:
116+
"Unable to load the Stable Diffusion model. Please check your internet connection or available storage space."
117+
)
118+
}
119+
}
110120
}
111121

112122
public nonisolated let configuration = StableDiffusionConfiguration.presetSDXLTurbo

Libraries/MLXLLM/Lora+Data.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
import Foundation
44

5-
enum LoRADataError: Error {
5+
enum LoRADataError: LocalizedError {
66
case fileNotFound(URL, String)
7+
8+
var errorDescription: String? {
9+
switch self {
10+
case .fileNotFound(let directory, let name):
11+
return String(
12+
localized: "Could not find data file '\(name)' in directory '\(directory.path())'.")
13+
}
14+
}
715
}
816

917
/// Load a LoRA data file.

Libraries/MLXLMCommon/ModelFactory.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ import Foundation
44
import Hub
55
import Tokenizers
66

7-
public enum ModelFactoryError: Error {
7+
public enum ModelFactoryError: LocalizedError {
88
case unsupportedModelType(String)
99
case unsupportedProcessorType(String)
10+
11+
public var errorDescription: String? {
12+
switch self {
13+
case .unsupportedModelType(let type): "Unsupported model type: \(type)"
14+
case .unsupportedProcessorType(let type): "Unsupported processor type: \(type)"
15+
}
16+
}
1017
}
1118

1219
/// Context of types that work together to provide a ``LanguageModel``.

Libraries/MLXLMCommon/UserInput.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,21 @@ public protocol UserInputProcessor {
175175
func prepare(input: UserInput) async throws -> LMInput
176176
}
177177

178-
private enum UserInputError: Error {
178+
private enum UserInputError: LocalizedError {
179179
case notImplemented
180180
case unableToLoad(URL)
181181
case arrayError(String)
182+
183+
var errorDescription: String? {
184+
switch self {
185+
case .notImplemented:
186+
return String(localized: "This functionality is not implemented.")
187+
case .unableToLoad(let url):
188+
return String(localized: "Unable to load image from URL: \(url.path).")
189+
case .arrayError(let message):
190+
return String(localized: "Error processing image array: \(message).")
191+
}
192+
}
182193
}
183194

184195
/// A do-nothing ``UserInputProcessor``.

Libraries/MLXVLM/VLMModelFactory.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@ import MLX
66
import MLXLMCommon
77
import Tokenizers
88

9-
public enum VLMError: Error {
9+
public enum VLMError: LocalizedError {
1010
case imageRequired
1111
case maskRequired
1212
case singleImageAllowed
1313
case imageProcessingFailure(String)
1414
case processing(String)
15+
16+
public var errorDescription: String? {
17+
switch self {
18+
case .imageRequired:
19+
return String(localized: "An image is required for this operation.")
20+
case .maskRequired:
21+
return String(localized: "An image mask is required for this operation.")
22+
case .singleImageAllowed:
23+
return String(localized: "Only a single image is allowed for this operation.")
24+
case .imageProcessingFailure(let details):
25+
return String(localized: "Failed to process the image: \(details)")
26+
case .processing(let details):
27+
return String(localized: "Processing error: \(details)")
28+
}
29+
}
1530
}
1631

1732
public struct BaseProcessorConfiguration: Codable, Sendable {

Libraries/StableDiffusion/Image.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ import ImageIO
77
import MLX
88
import UniformTypeIdentifiers
99

10-
enum ImageError: Error {
10+
enum ImageError: LocalizedError {
1111
case failedToSave
1212
case unableToOpen
13+
14+
var errorDescription: String? {
15+
switch self {
16+
case .failedToSave:
17+
return String(localized: "Failed to save the image to the specified location.")
18+
case .unableToOpen:
19+
return String(localized: "Unable to open the image file.")
20+
}
21+
}
1322
}
1423

1524
/// Conversion utilities for moving between `MLXArray`, `CGImage` and files.

Libraries/StableDiffusion/StableDiffusion.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,26 @@ public protocol ImageToImageGenerator: ImageGenerator {
100100
-> DenoiseIterator
101101
}
102102

103-
enum ModelContainerError: Error {
104-
/// unable to create the particular type of model, e.g. it doesn't support image to image
103+
enum ModelContainerError: LocalizedError {
104+
/// Unable to create the particular type of model, e.g. it doesn't support image to image
105105
case unableToCreate(String, String)
106-
107-
/// when operating in conserveMemory mode it tried to use a model that had been discarded
106+
/// When operating in conserveMemory mode, it tried to use a model that had been discarded
108107
case modelDiscarded
108+
109+
var errorDescription: String? {
110+
switch self {
111+
case .unableToCreate(let modelId, let generatorType):
112+
return String(
113+
localized:
114+
"Unable to create a \(generatorType) with model ID '\(modelId)'. The model may not support this operation type."
115+
)
116+
case .modelDiscarded:
117+
return String(
118+
localized:
119+
"The model has been discarded to conserve memory and is no longer available. Please recreate the model container."
120+
)
121+
}
122+
}
109123
}
110124

111125
/// Container for models that guarantees single threaded access.

0 commit comments

Comments
 (0)