Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

Vapor swift Code working on localhost but after deploying at heroku all apis working fine but this one crashes

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).

enter image description here

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)
    }
}
question from:https://stackoverflow.com/questions/65920872/vapor-swift-code-working-on-localhost-but-after-deploying-at-heroku-all-apis-wor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...