diff --git a/Sources/Helpers/SharedModels/PostgrestError.swift b/Sources/Helpers/SharedModels/PostgrestError.swift index 7874b4200..82f352cc1 100644 --- a/Sources/Helpers/SharedModels/PostgrestError.swift +++ b/Sources/Helpers/SharedModels/PostgrestError.swift @@ -8,22 +8,36 @@ public import Foundation public struct PostgrestError: Error, Codable, Sendable { - public let detail: String? + /// Additional detail about the error, as returned by PostgREST in the `details` field. + public let details: String? public let hint: String? public let code: String? public let message: String + @available(*, deprecated, renamed: "details") + public var detail: String? { details } + public init( - detail: String? = nil, + details: String? = nil, hint: String? = nil, code: String? = nil, message: String ) { self.hint = hint - self.detail = detail + self.details = details self.code = code self.message = message } + + @available(*, deprecated, renamed: "init(details:hint:code:message:)") + public init( + detail: String?, + hint: String? = nil, + code: String? = nil, + message: String + ) { + self.init(details: detail, hint: hint, code: code, message: message) + } } extension PostgrestError: LocalizedError { diff --git a/Tests/HelpersTests/PostgrestErrorTests.swift b/Tests/HelpersTests/PostgrestErrorTests.swift index 51ba5def4..eb59a4984 100644 --- a/Tests/HelpersTests/PostgrestErrorTests.swift +++ b/Tests/HelpersTests/PostgrestErrorTests.swift @@ -18,4 +18,42 @@ struct PostgrestErrorTests { #expect(error.errorDescription == "test error message") } + @Test + func decodesDetailsFromWireKey() throws { + let json = """ + { + "code": "23505", + "details": "Key (id)=(1) already exists.", + "hint": "Use a different id.", + "message": "duplicate key value violates unique constraint \\"users_pkey\\"" + } + """ + + let error = try JSONDecoder().decode(PostgrestError.self, from: Data(json.utf8)) + + #expect(error.details == "Key (id)=(1) already exists.") + #expect(error.hint == "Use a different id.") + #expect(error.code == "23505") + #expect(error.message == "duplicate key value violates unique constraint \"users_pkey\"") + } + + @Test + func encodesDetailsToWireKey() throws { + let error = PostgrestError(details: "a detail", message: "a message") + + let data = try JSONEncoder().encode(error) + let object = try #require( + try JSONSerialization.jsonObject(with: data) as? [String: Any] + ) + + #expect(object["details"] as? String == "a detail") + #expect(object["detail"] == nil) + } + + @Test + @available(*, deprecated) + func deprecatedDetailForwardsToDetails() { + let error = PostgrestError(details: "a detail", message: "a message") + #expect(error.detail == "a detail") + } } diff --git a/dictionary.txt b/dictionary.txt index 1252ab110..5e151e8b5 100644 --- a/dictionary.txt +++ b/dictionary.txt @@ -126,6 +126,7 @@ phraseto pkce PKCE pkcs +pkey plainto plfts postalcode diff --git a/sdk-compliance.yaml b/sdk-compliance.yaml index f1f041ec6..4f7cfdd19 100644 --- a/sdk-compliance.yaml +++ b/sdk-compliance.yaml @@ -337,7 +337,10 @@ features: # ── Database ─────────────────────────────────────────────────────────────── - database.query.from_table: implemented + database.query.from_table: + status: implemented + symbols: + - PostgrestError.details database.query.select: implemented database.query.schema_selection: implemented database.query.rpc: implemented