Skip to content

Commit

Permalink
mapIfDuplicatedKeyError
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriomazzeo committed Jan 30, 2019
1 parent 3c71d2a commit 4b48a4e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Sources/FluentMongo/Model+Index.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ extension Model where Database == MongoDatabase {
return IndexBuilder(on: conn.databaseConnection(to: Self.defaultDatabase))
}
}

extension Future where T: Model, T.Database == MongoDatabase {

@discardableResult
public func mapIfDuplicatedKeyError(_ callback: @escaping (MongoConnection.Error) throws -> T) -> Future<T> {
return self.catchMap { error in
guard
let mongoConnectionError = error as? MongoConnection.Error,
case .duplicatedKey = mongoConnectionError
else {
throw error
}

return try callback(mongoConnectionError)
}
}
}

0 comments on commit 4b48a4e

Please sign in to comment.