From 46b5b0c1b9eaf18b7111d703caa6902eb7ae1d26 Mon Sep 17 00:00:00 2001 From: Arthur Liu Date: Sat, 29 Aug 2026 01:24:33 -0700 Subject: [PATCH] Give HTTPClientError meaningful localized descriptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Motivation: HTTPClientError.localizedDescription currently falls back to Foundation’s generic error 1 message, obscuring the underlying failure. Modifications: Conform HTTPClientError to LocalizedError using its existing description and add coverage for errors with and without associated values. Result: localizedDescription now preserves the concrete HTTP client error details. --- .../AsyncHTTPClient/FoundationExtensions.swift | 6 ++++++ .../FoundationExtensionTests.swift | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Sources/AsyncHTTPClient/FoundationExtensions.swift b/Sources/AsyncHTTPClient/FoundationExtensions.swift index eeff2ad7e..343470fb1 100644 --- a/Sources/AsyncHTTPClient/FoundationExtensions.swift +++ b/Sources/AsyncHTTPClient/FoundationExtensions.swift @@ -21,6 +21,12 @@ import FoundationEssentials import Foundation #endif +extension HTTPClientError: LocalizedError { + public var errorDescription: String? { + self.description + } +} + extension HTTPClient.Cookie { /// The cookie's expiration date. public var expires: Date? { diff --git a/Tests/AsyncHTTPClientTests/FoundationExtensionTests.swift b/Tests/AsyncHTTPClientTests/FoundationExtensionTests.swift index 4ff0d6c26..bfc0c8782 100644 --- a/Tests/AsyncHTTPClientTests/FoundationExtensionTests.swift +++ b/Tests/AsyncHTTPClientTests/FoundationExtensionTests.swift @@ -19,6 +19,21 @@ import Testing @Suite struct FoundationExtensionTests { + @Test + func httpClientErrorLocalizedDescriptionUsesDescription() { + let error = HTTPClientError.cancelled + + #expect(error.localizedDescription == error.description) + } + + @Test + func httpClientErrorLocalizedDescriptionPreservesAssociatedValues() { + let error = HTTPClientError.unsupportedScheme("custom") + + #expect(error.localizedDescription == error.description) + #expect(error.localizedDescription.contains("custom")) + } + @Test(arguments: [ // Format: (input, expected) ("localhost", "localhost"), // Alphanumerics (No encoding needed)