Skip to content

Commit 8b570ad

Browse files
committed
Error handling
1 parent 931739f commit 8b570ad

File tree

4 files changed

+55
-36
lines changed

4 files changed

+55
-36
lines changed

CommandLine/CommandLine.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,19 @@ extension SwiftDraw.CommandLine {
4242
printHelp()
4343
return .error
4444
}
45-
46-
guard
47-
let data = try? process(with: config) else {
48-
print("Failure", to: &.standardError)
45+
46+
let data: Data
47+
do {
48+
data = try process(with: config)
49+
} catch Error.fileNotFound {
50+
print("Failure: File does not exist.", to: &.standardError)
51+
return .error
52+
} catch {
53+
print("Failure:", error.localizedDescription, to: &.standardError)
4954
printHelp()
5055
return .error
5156
}
52-
57+
5358
do {
5459
try data.write(to: config.output)
5560
print("Created: \(config.output.path)")
@@ -61,24 +66,32 @@ extension SwiftDraw.CommandLine {
6166
}
6267

6368
static func process(with config: Configuration) throws -> Data {
64-
guard let data = processImage(config: config) else {
69+
guard let data = try processImage(config: config) else {
6570
throw Error.invalid
6671
}
6772

6873
return data
6974
}
7075

71-
static func processImage(config: Configuration) -> Data? {
76+
static func processImage(config: Configuration) throws -> Data? {
77+
guard FileManager.default.fileExists(atPath: config.input.path) else {
78+
throw Error.fileNotFound
79+
}
80+
7281
switch config.format {
7382
case .swift:
74-
let code = CGTextRenderer.render(fileURL: config.input, size: config.size.renderSize, options: config.options)
75-
return code?.data(using: .utf8)
83+
let code = try CGTextRenderer.render(fileURL: config.input, size: config.size.renderSize, options: config.options)
84+
return code.data(using: .utf8)
7685
case .sfsymbol:
7786
print("[--format sfsymbol] is an experimental feature.")
78-
let svg = try? SFSymbolRenderer.render(fileURL: config.input, options: config.options)
79-
return svg?.data(using: .utf8)
87+
let svg = try SFSymbolRenderer.render(fileURL: config.input, options: config.options)
88+
return svg.data(using: .utf8)
8089
case .jpeg, .pdf, .png:
81-
return SwiftDraw.Image(fileURL: config.input, options: config.options).flatMap { processImage($0, with: config) }
90+
guard let image = SwiftDraw.Image(fileURL: config.input, options: config.options),
91+
let data = processImage(image, with: config) else {
92+
throw Error.invalid
93+
}
94+
return data
8295
}
8396
}
8497

SwiftDraw/CGTextRenderer+Code.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,16 @@ public extension CGTextRenderer {
3535

3636
typealias Size = (width: Int, height: Int)
3737

38-
static func render(named name: String, in bundle: Bundle = Bundle.main, size: Size? = nil, options: Image.Options) -> String? {
39-
guard let url = bundle.url(forResource: name, withExtension: nil) else { return nil }
40-
return render(fileURL: url, size: size, options: options)
41-
}
42-
43-
static func render(fileURL: URL, size: Size? = nil, options: Image.Options) -> String? {
44-
guard let svg = try? DOM.SVG.parse(fileURL: fileURL) else {
45-
return nil
38+
static func render(named name: String, in bundle: Bundle = Bundle.main, size: Size? = nil, options: Image.Options) throws -> String {
39+
guard let url = bundle.url(forResource: name, withExtension: nil) else {
40+
throw Error("File not found.")
4641
}
42+
return try render(fileURL: url, size: size, options: options)
43+
}
4744

45+
static func render(fileURL: URL, size: Size? = nil, options: Image.Options) throws -> String {
46+
let svg = try DOM.SVG.parse(fileURL: fileURL)
4847
let size = makeSize(svg: svg, size: size)
49-
5048
let identifier = fileURL.lastPathComponent
5149
.replacingOccurrences(of: ".\(fileURL.pathExtension)", with: "")
5250
.replacingOccurrences(of: "_", with: " ")
@@ -93,4 +91,12 @@ public extension CGTextRenderer {
9391

9492
return renderer.makeText()
9593
}
94+
95+
private struct Error: LocalizedError {
96+
var errorDescription: String?
97+
98+
init(_ message: String) {
99+
self.errorDescription = message
100+
}
101+
}
96102
}

SwiftDraw/CommandLine.Arguments.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extension CommandLine {
6565

6666
public enum Error: Swift.Error {
6767
case invalid
68+
case fileNotFound
6869
}
6970
}
7071

SwiftDraw/Renderer.SFSymbol.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@ public final class SFSymbolRenderer { }
3535

3636
extension SFSymbolRenderer {
3737

38-
func makeDOM(for layer: LayerTree.Layer) throws -> DOM.SVG {
39-
throw Error.invalid
40-
}
41-
42-
static func firstPath(for layer: LayerTree.Layer) throws -> LayerTree.Path {
43-
guard let path = getPaths(for: layer).first else {
44-
throw Error.invalid
45-
}
46-
return path
47-
}
48-
4938
static func getPaths(for layer: LayerTree.Layer) -> [LayerTree.Path] {
5039
guard layer.opacity > 0,
5140
layer.clip.isEmpty,
@@ -78,11 +67,14 @@ extension SFSymbolRenderer {
7867
fill: LayerTree.FillAttributes) -> LayerTree.Path? {
7968
guard case .path(let p) = shape else { return nil }
8069

81-
#if canImport(CoreGraphics)
8270
if stoke.color != .none && stoke.width > 0 {
71+
#if canImport(CoreGraphics)
8372
return expandOutlines(for: p, stroke: stoke)
84-
}
73+
#else
74+
print("Warning:", "expanding stroke outlines requires macOS.", to: &.standardError)
75+
return nil
8576
#endif
77+
}
8678

8779
if fill.fill != .none && fill.opacity > 0 {
8880
return p
@@ -149,8 +141,12 @@ extension SFSymbolRenderer {
149141
return dom
150142
}
151143

152-
enum Error: Swift.Error {
153-
case invalid
144+
struct Error: LocalizedError {
145+
var errorDescription: String?
146+
147+
init(_ message: String) {
148+
self.errorDescription = message
149+
}
154150
}
155151
}
156152

@@ -167,6 +163,9 @@ public extension SFSymbolRenderer {
167163
let black = try svg.group(id: "Symbols").group(id: "Black-S")
168164

169165
let sourcePaths = getPaths(for: layer)
166+
guard !sourcePaths.isEmpty else {
167+
throw Error("No valid content found.")
168+
}
170169
regular.childElements.append(
171170
contentsOf: convertPaths(sourcePaths, into: .regular)
172171
)

0 commit comments

Comments
 (0)