DatabaseError.isConstraintFailure not being set
gargs opened this issue · 0 comments
gargs commented
I wrote a simple User schema with a constraint of a unique email address in the database. In my 'newuser' post method, I am checking the create operation for any database constraint failures. The isConstraintFailure
property is set to false, even though the error is SQLiteNIO.SQLiteError.Reason.constraint
with a message of UNIQUE constraint failed: users.email
.
My function is:
try User.validate(req)
let user = try req.content.decode(User.self)
return user.create(on: req.db)
.map { user }
.flatMapError { (error) -> EventLoopFuture<User> in
if let dbError = error as? DatabaseError, dbError.isConstraintFailure {
return req.eventLoop.makeFailedFuture(Abort(.internalServerError, reason: "User Already exists"))
} else {
return User.query(on: req.db)
.filter(\.$email, .equal, user.email)
.first()
.unwrap(or: Abort(.internalServerError))
}
}
}