Skip to content

Commit b3cc76c

Browse files
committed
Make the error message for an invalid version more user-friendly
1 parent eeb2850 commit b3cc76c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ import Foundation
2121
/// with the previous format.
2222
internal let highestSupportedConfigurationVersion = 1
2323

24+
private struct ConfigurationDecodingError: LocalizedError {
25+
var errorDescription: String?
26+
init(errorDescription: String) {
27+
self.errorDescription = errorDescription
28+
}
29+
}
30+
2431
/// Holds the complete set of configured values and defaults.
2532
public struct Configuration: Codable, Equatable {
2633

@@ -285,11 +292,9 @@ public struct Configuration: Codable, Equatable {
285292
// If the version number is not present, assume it is 1.
286293
self.version = try container.decodeIfPresent(Int.self, forKey: .version) ?? 1
287294
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)."
295+
throw ConfigurationDecodingError(
296+
errorDescription:
297+
"This version of the formatter does not support configuration version \(version). The highest supported version is \(highestSupportedConfigurationVersion)."
293298
)
294299
}
295300

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
}

0 commit comments

Comments
 (0)