Skip to content

Commit 8cb0e35

Browse files
authored
Merge pull request #942 from TTOzzi/configuration_decoding_error
Make the error message for an invalid version more user-friendly
2 parents eeb2850 + 5773d64 commit 8cb0e35

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,9 @@ public struct Configuration: Codable, Equatable {
285285
// If the version number is not present, assume it is 1.
286286
self.version = try container.decodeIfPresent(Int.self, forKey: .version) ?? 1
287287
guard version <= highestSupportedConfigurationVersion else {
288-
throw DecodingError.dataCorruptedError(
289-
forKey: .version,
290-
in: container,
291-
debugDescription:
292-
"This version of the formatter does not support configuration version \(version)."
288+
throw SwiftFormatError.unsupportedConfigurationVersion(
289+
version,
290+
highestSupported: highestSupportedConfigurationVersion
293291
)
294292
}
295293

Sources/SwiftFormat/API/SwiftFormatError.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public enum SwiftFormatError: LocalizedError {
3131
/// An error happened while dumping the tool's configuration.
3232
case configurationDumpFailed(String)
3333

34+
/// The provided configuration version is not supported by this version of the formatter.
35+
case unsupportedConfigurationVersion(Int, highestSupported: Int)
36+
3437
public var errorDescription: String? {
3538
switch self {
3639
case .fileNotReadable:
@@ -43,6 +46,9 @@ public enum SwiftFormatError: LocalizedError {
4346
return "experimental feature '\(name)' was not recognized by the Swift parser"
4447
case .configurationDumpFailed(let message):
4548
return "dumping configuration failed: \(message)"
49+
case .unsupportedConfigurationVersion(let version, let highestSupported):
50+
return
51+
"This version of the formatter does not support configuration version \(version). The highest supported version is \(highestSupported)."
4652
}
4753
}
4854
}

Sources/swift-format/Frontend/Frontend.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Frontend {
9999
// Fall through to the default return at the end of the function.
100100
} catch {
101101
diagnosticsEngine.emitError(
102-
"Unable to read configuration for \(swiftFileURL.path): \(error.localizedDescription)"
102+
"Unable to read configuration for \(swiftFileURL.relativePath): \(error.localizedDescription)"
103103
)
104104
return nil
105105
}
@@ -116,7 +116,7 @@ class Frontend {
116116
}
117117
} catch {
118118
diagnosticsEngine.emitError(
119-
"Unable to read configuration for \(cwd): \(error.localizedDescription)"
119+
"Unable to read configuration for \(cwd.relativePath): \(error.localizedDescription)"
120120
)
121121
return nil
122122
}

api-breakages.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
API breakage: constructor FileIterator.init(urls:followSymlinks:) has been removed
55
API breakage: enumelement SwiftFormatError.configurationDumpFailed has been added as a new enum case
6+
API breakage: enumelement SwiftFormatError.unsupportedConfigurationVersion has been added as a new enum case

0 commit comments

Comments
 (0)