Reputation: 302
This function is called when api hits, Looks like error is in return statement in second do block, it is working good on local host but after deploying on heroku and hitting this api, it says Could not cast value of type 'Vapor.Request' (0x55e629265b00) to 'Swift.Error' (0x7fd61a06b560).
I faced same error once in localhost and using try fixed that.
func updateInterestInJob(_ req: Request) throws -> Future<SeekerInterestInJob> {
var myInterest = SeekerInterestInJob(userId: 0, annotationId: 0, interest: false, status: 0)
let decoder = JSONDecoder()
guard let data = req.http.body.data else {
throw req as! Error
}
do {
myInterest = try decoder.decode(SeekerInterestInJob.self, from: data)
print("Testing print")
do {
return try SeekerInterestInJob.query(on: req).filter(\SeekerInterestInJob.userId, .equal,myInterest.userId).filter(\SeekerInterestInJob.providerId, .equal,myInterest.providerId).filter(\SeekerInterestInJob.annotationId, .equal,myInterest.annotationId).first().flatMap { (foundInterest) -> EventLoopFuture<SeekerInterestInJob> in
if foundInterest == nil {
print("interest not found")
return myInterest.save(on: req)
}
else {
print("interest found")
foundInterest?.userId = myInterest.userId
foundInterest?.providerId = myInterest.providerId
foundInterest?.annotationId = myInterest.annotationId
if myInterest.status != 0 {
foundInterest?.status = myInterest.status
foundInterest?.interest = foundInterest?.interest
}
else {
foundInterest?.interest = myInterest.interest
}
return (foundInterest?.update(on: req))!
}
}
}
catch {
print("in inner catch block")
return myInterest.update(on: req)
}
}
catch {
print("in outer catch block")
return myInterest.update(on: req)
}
}
Upvotes: 0
Views: 209
Reputation: 11494
Well, your issue is right here, at the top of your method:
guard let data = req.http.body.data else {
throw req as! Error
}
Vapor's Request
type doesn't conform to the Error
protocol, so this typecast will crash 100% percent of the time. Instead, you should be using Vapor's built-in Abort
error type, which can be used to represent any HTTP error.
guard let data = req.http.body.data else {
throw Abort(.badRequest, reason: "Missing request body")
}
Upvotes: 4