Search code examples
swifterror-handlingvapor

Errors throws from Vapor server come as String, not as Error in response


I have Vapor 3 server api and iOS app. When I do requests to server and throw an error for example like this (I throw it on server):

throw Abort(.badRequest)

in iOS app in response error == nil, and if I transform the response data to a string, I can see:

{"error":true,"reason":"Bad Request"}

I tried to add ErrorMiddleware to services. That did not work for me.

// Request on iOS side:
func saveComment(post: Post, text: String, completion: @escaping (Result<Comment, Error>) -> Void) {

    var cmps = urlComps
    cmps.path = "/api/save-post"

    let url = cmps.url!

    var request = URLRequest(url: url)
    request.httpMethod = HTTPMethod.post
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    struct NewComment: Codable {
        let postId: String
        let text: String
    }

    let newComment = NewComment(postId: post.id.uuidString, text: text)
    let body = try! JSONEncoder().encode(newComment)

    request.httpBody = body

    let session = URLSession.shared.dataTask(with: request) { data, response, err in
        // this error comes as nil
        if let err = err {
            completion(.failure(err))
            return
        }

        // if I do String(data: data!, encoding: .utf8)!
        // it becomes: {"error":true,"reason":"Bad Request"}

        do {
            let comment = try JSONDecoder().decode(Comment.self, from: data!)
            completion(.success(comment))

        } catch {
            completion(.failure(error))
        }
    }

    session.resume()
}
// Response on Vapor server side:
func addComment(req: Request) throws -> Future<Comment.FormattedComment> {

    let user = try req.requireAuthenticated(User.self)

    guard user.canAddComments > 0 else { throw Abort(.badRequest) }
// here I throw error 

    return try req.content.decode(Comment.NewComment.self).flatMap(to: Comment.FormattedComment.self) { comment in

        let newComment = Comment(id: nil, userId: user.id!, postId: comment.postId, text: comment.text, date: Date(), isGiven: false)
        return newComment.save(on: req).map {
            return $0.formatted()
        }

    }
}

I would like to get error as an error from request. Not in data.


Solution

  • You just have to check response code instead of error.