From 5773d64c2b6cb926369384b9c927ef022811cb6b Mon Sep 17 00:00:00 2001 From: TTOzzi Date: Sat, 22 Feb 2025 02:04:46 +0900 Subject: [PATCH] Make the error message for an invalid version more user-friendly --- Sources/SwiftFormat/API/Configuration.swift | 8 +++----- Sources/SwiftFormat/API/SwiftFormatError.swift | 6 ++++++ Sources/swift-format/Frontend/Frontend.swift | 4 ++-- api-breakages.txt | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Sources/SwiftFormat/API/Configuration.swift b/Sources/SwiftFormat/API/Configuration.swift index dd574580f..ac1c742d8 100644 --- a/Sources/SwiftFormat/API/Configuration.swift +++ b/Sources/SwiftFormat/API/Configuration.swift @@ -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 ) } diff --git a/Sources/SwiftFormat/API/SwiftFormatError.swift b/Sources/SwiftFormat/API/SwiftFormatError.swift index cb35a82d0..5dd2a6e69 100644 --- a/Sources/SwiftFormat/API/SwiftFormatError.swift +++ b/Sources/SwiftFormat/API/SwiftFormatError.swift @@ -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: @@ -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)." } } } diff --git a/Sources/swift-format/Frontend/Frontend.swift b/Sources/swift-format/Frontend/Frontend.swift index 6e24b7965..e1007414b 100644 --- a/Sources/swift-format/Frontend/Frontend.swift +++ b/Sources/swift-format/Frontend/Frontend.swift @@ -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 } @@ -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 } diff --git a/api-breakages.txt b/api-breakages.txt index 07f9a3bc2..0b1c3400a 100644 --- a/api-breakages.txt +++ b/api-breakages.txt @@ -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