Skip to content

DatabaseError.isConstraintFailure not being set #49

@gargs

Description

@gargs

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions