Skip to content

Commit

Permalink
Merge pull request #942 from TTOzzi/configuration_decoding_error
Browse files Browse the repository at this point in the history
Make the error message for an invalid version more user-friendly
  • Loading branch information
allevato authored Feb 24, 2025
2 parents eeb2850 + 5773d64 commit 8cb0e35
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
8 changes: 3 additions & 5 deletions Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,9 @@ public struct Configuration: Codable, Equatable {
// If the version number is not present, assume it is 1.
self.version = try container.decodeIfPresent(Int.self, forKey: .version) ?? 1
guard version <= highestSupportedConfigurationVersion else {
throw DecodingError.dataCorruptedError(
forKey: .version,
in: container,
debugDescription:
"This version of the formatter does not support configuration version \(version)."
throw SwiftFormatError.unsupportedConfigurationVersion(
version,
highestSupported: highestSupportedConfigurationVersion
)
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/SwiftFormat/API/SwiftFormatError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public enum SwiftFormatError: LocalizedError {
/// An error happened while dumping the tool's configuration.
case configurationDumpFailed(String)

/// The provided configuration version is not supported by this version of the formatter.
case unsupportedConfigurationVersion(Int, highestSupported: Int)

public var errorDescription: String? {
switch self {
case .fileNotReadable:
Expand All @@ -43,6 +46,9 @@ public enum SwiftFormatError: LocalizedError {
return "experimental feature '\(name)' was not recognized by the Swift parser"
case .configurationDumpFailed(let message):
return "dumping configuration failed: \(message)"
case .unsupportedConfigurationVersion(let version, let highestSupported):
return
"This version of the formatter does not support configuration version \(version). The highest supported version is \(highestSupported)."
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/swift-format/Frontend/Frontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Frontend {
// Fall through to the default return at the end of the function.
} catch {
diagnosticsEngine.emitError(
"Unable to read configuration for \(swiftFileURL.path): \(error.localizedDescription)"
"Unable to read configuration for \(swiftFileURL.relativePath): \(error.localizedDescription)"
)
return nil
}
Expand All @@ -116,7 +116,7 @@ class Frontend {
}
} catch {
diagnosticsEngine.emitError(
"Unable to read configuration for \(cwd): \(error.localizedDescription)"
"Unable to read configuration for \(cwd.relativePath): \(error.localizedDescription)"
)
return nil
}
Expand Down
1 change: 1 addition & 0 deletions api-breakages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

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

0 comments on commit 8cb0e35

Please sign in to comment.