Nick Schaefer
Nick Schaefer
CCConvex Community
Created by Nick Schaefer on 12/23/2024 in #support-community
Request for better Swift Error Handling
As I've been setting up my app in Swift, I've been encountering this error:
Thread 3: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode String but found a dictionary instead.", underlyingError: nil))
Thread 3: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode String but found a dictionary instead.", underlyingError: nil))
In this internal Convex code:
func callForResult<T: Decodable>(
name: String, args: [String: ConvexEncodable?]? = nil, remoteCall: RemoteCall
)
async throws -> T
{
let rawResult = try await remoteCall(
name,
args?.mapValues({ v in
try v?.convexEncode() ?? "null"
}) ?? [:])
return try! JSONDecoder().decode(T.self, from: Data(rawResult.utf8)) // here
}
func callForResult<T: Decodable>(
name: String, args: [String: ConvexEncodable?]? = nil, remoteCall: RemoteCall
)
async throws -> T
{
let rawResult = try await remoteCall(
name,
args?.mapValues({ v in
try v?.convexEncode() ?? "null"
}) ?? [:])
return try! JSONDecoder().decode(T.self, from: Data(rawResult.utf8)) // here
}
At first I discovered it was because I was naming my Convex mutation name with a dot instead of a colon try await convex.mutation("~~items.create~~item:create", with: ["name": name]). But now I'm experiencing the same error for another completely different reason. Wrapping the mutation/action in a do/try/catch does not catch this error either - it crashes the app every time. I may follow this up with my latest issue if I don't solve it soon, but I would appreciate some better error handling here if possible. Thanks 🙂
8 replies