From 29a1d74c93ff3936efa905ce8c8b0e87222d0bee Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Tue, 15 Oct 2024 17:48:45 -0400 Subject: [PATCH] Add expected ranges to year, month, day decoding errors --- .../Sources/Types/Internal/ProtoDate.swift | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/FirebaseVertexAI/Sources/Types/Internal/ProtoDate.swift b/FirebaseVertexAI/Sources/Types/Internal/ProtoDate.swift index d605d47d08d..d2d02583617 100644 --- a/FirebaseVertexAI/Sources/Types/Internal/ProtoDate.swift +++ b/FirebaseVertexAI/Sources/Types/Internal/ProtoDate.swift @@ -97,7 +97,12 @@ extension ProtoDate: Decodable { if let year = try container.decodeIfPresent(Int.self, forKey: .year), year != 0 { guard year >= 1 && year <= 9999 else { throw DecodingError.dataCorrupted( - .init(codingPath: [CodingKeys.year], debugDescription: "Invalid year: \(year)") + DecodingError.Context( + codingPath: [CodingKeys.year], + debugDescription: """ + Invalid year: \(year); must be from 1 to 9999, or 0 for a date without a specified year. + """ + ) ) } self.year = year @@ -108,7 +113,13 @@ extension ProtoDate: Decodable { if let month = try container.decodeIfPresent(Int.self, forKey: .month), month != 0 { guard month >= 1 && month <= 12 else { throw DecodingError.dataCorrupted( - .init(codingPath: [CodingKeys.month], debugDescription: "Invalid month: \(month)") + DecodingError.Context( + codingPath: [CodingKeys.month], + debugDescription: """ + Invalid month: \(month); must be from 1 to 12, or 0 for a year date without a \ + specified month and day. + """ + ) ) } self.month = month @@ -119,7 +130,12 @@ extension ProtoDate: Decodable { if let day = try container.decodeIfPresent(Int.self, forKey: .day), day != 0 { guard day >= 1 && day <= 31 else { throw DecodingError.dataCorrupted( - .init(codingPath: [CodingKeys.day], debugDescription: "Invalid day: \(day)") + DecodingError.Context( + codingPath: [CodingKeys.day], + debugDescription: """ + Invalid day: \(day); must be from 1 to 31, or 0 for a date without a specified day. + """ + ) ) } self.day = day