From 594911b7a29977d6c05ae44c928ec8730e70806d Mon Sep 17 00:00:00 2001 From: An Tao Date: Thu, 29 Oct 2020 20:09:16 +0800 Subject: [PATCH] Fix a bug in the Mapper::insertFuture() (#620) --- orm_lib/inc/drogon/orm/Mapper.h | 11 +++++++++-- orm_lib/tests/db_test.cc | 21 +++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/orm_lib/inc/drogon/orm/Mapper.h b/orm_lib/inc/drogon/orm/Mapper.h index 94db85dc..7ece802c 100644 --- a/orm_lib/inc/drogon/orm/Mapper.h +++ b/orm_lib/inc/drogon/orm/Mapper.h @@ -1243,8 +1243,15 @@ inline std::future Mapper::insertFuture(const T &obj) noexcept tmp.findByPrimaryKey( newObj.getPrimaryKey(), [prom](T selObj) { prom->set_value(selObj); }, - [prom](const std::exception_ptr &e) { - prom->set_exception(e); + [prom](const DrogonDbException &e) { + try + { + throw e; + } + catch (...) + { + prom->set_exception(std::current_exception()); + } }); } else diff --git a/orm_lib/tests/db_test.cc b/orm_lib/tests/db_test.cc index 522b2eaf..f51449bb 100644 --- a/orm_lib/tests/db_test.cc +++ b/orm_lib/tests/db_test.cc @@ -31,7 +31,7 @@ using namespace drogon::orm; #define RED "\033[31m" /* Red */ #define GREEN "\033[32m" /* Green */ -constexpr int postgre_tests = 43; +constexpr int postgre_tests = 44; constexpr int mysql_tests = 45; constexpr int sqlite_tests = 47; @@ -650,13 +650,30 @@ void doPostgreTest(const drogon::orm::DbClientPtr &clientPtr) testOutput(false, "postgresql - ORM mapper asynchronous interface(0)"); }); + + /// 6.1.5 insert future + user.setUserId("pg_future"); + auto fu = mapper.insertFuture(user); + try + { + auto u = fu.get(); + testOutput(true, + "postgresql - ORM mapper asynchronous future interface(0)"); + } + catch (const DrogonDbException &e) + { + std::cerr << e.base().what() << std::endl; + testOutput(false, + "postgresql - ORM mapper asynchronous future interface(0)"); + } + /// 6.2 insert user.setUserId("pg1"); user.setUserName("postgres1"); mapper.insert( user, [](Users ret) { - testOutput(ret.getPrimaryKey() == 2, + testOutput(ret.getPrimaryKey() == 3, "postgresql - ORM mapper asynchronous interface(1)"); }, [](const DrogonDbException &e) {