Skip to content

Commit

Permalink
Add expected ranges to year, month, day decoding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard committed Oct 15, 2024
1 parent a388d9f commit 29a1d74
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions FirebaseVertexAI/Sources/Types/Internal/ProtoDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 29a1d74

Please sign in to comment.