Commit 76b3089b authored by Vadim Zeitlin's avatar Vadim Zeitlin

Add soci_error::get_error_message() method.

This will only become useful after the upcoming commits which will extend
what() to return more information about the exception, but commit this
separately as there are only trivial changes in this commit: just use
std::string returned by get_error_message() instead of char* returned by
what() in the tests.
parent e8e59781
......@@ -30,6 +30,12 @@ int main()
}
</pre>
<p>
The only public method of <code>soci_error</code> is <code>std::string get_error_message() const</code>,
which returns just the brief error message, without any additional information
that can be present in the full error message returned by <code>what()</code>.
</p>
<div class="note">
<p><span class="note">Portability note:</span></p>
<p>The Oracle backend can also throw the instances of the <code>oracle_soci_error</code>,
......
//
// Copyright (C) 2004-2008 Maciej Sobczak
// Copyright (C) 2015 Vadim Zeitlin
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
......@@ -20,6 +21,9 @@ class SOCI_DECL soci_error : public std::runtime_error
{
public:
explicit soci_error(std::string const & msg);
// Returns just the error message itself, without the context.
std::string get_error_message() const;
};
} // namespace soci
......
//
// Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
// Copyright (C) 2015 Vadim Zeitlin
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
......@@ -15,3 +16,8 @@ soci_error::soci_error(std::string const & msg)
: std::runtime_error(msg)
{
}
std::string soci_error::get_error_message() const
{
return std::runtime_error::what();
}
......@@ -593,8 +593,7 @@ TEST_CASE_METHOD(common_tests, "Use and into", "[core][into]")
}
catch (soci_error const &e)
{
std::string error = e.what();
CHECK(error ==
CHECK(e.get_error_message() ==
"Null value fetched and no indicator defined.");
}
......@@ -676,8 +675,8 @@ TEST_CASE_METHOD(common_tests, "Repeated and bulk fetch", "[core][bulk]")
}
catch (soci_error const &e)
{
std::string msg = e.what();
CHECK(msg == "Vectors of size 0 are not allowed.");
CHECK(e.get_error_message() ==
"Vectors of size 0 are not allowed.");
}
}
......@@ -1751,8 +1750,7 @@ TEST_CASE_METHOD(common_tests, "Named parameters", "[core][use][named-params]")
}
catch (soci_error const& e)
{
std::string what(e.what());
CHECK(what ==
CHECK(e.get_error_message() ==
"Binding for use elements must be either by position "
"or by name.");
}
......@@ -1869,8 +1867,7 @@ TEST_CASE_METHOD(common_tests, "Transactions", "[core][transaction]")
}
catch (soci_error const &e)
{
std::string msg = e.what();
CHECK(msg ==
CHECK(e.get_error_message() ==
"The transaction object cannot be handled twice.");
}
}
......@@ -3207,8 +3204,8 @@ TEST_CASE_METHOD(common_tests, "Connection and reconnection", "[core][connect]")
}
catch (soci_error const &e)
{
CHECK(e.what() == std::string(
"Cannot reconnect without previous connection."));
CHECK(e.get_error_message() ==
"Cannot reconnect without previous connection.");
}
// open from empty session
......@@ -3226,8 +3223,8 @@ TEST_CASE_METHOD(common_tests, "Connection and reconnection", "[core][connect]")
}
catch (soci_error const &e)
{
CHECK(e.what() == std::string(
"Cannot open already connected session."));
CHECK(e.get_error_message() ==
"Cannot open already connected session.");
}
sql.close();
......@@ -3249,7 +3246,8 @@ TEST_CASE_METHOD(common_tests, "Connection and reconnection", "[core][connect]")
}
catch (soci_error const &e)
{
CHECK(e.what() == std::string("Session is not connected."));
CHECK(e.get_error_message() ==
"Session is not connected.");
}
}
......@@ -3585,7 +3583,8 @@ TEST_CASE_METHOD(common_tests, "Boost date", "[core][boost][datetime]")
}
catch (soci_error const & e)
{
CHECK(e.what() == std::string("Null value not allowed for this type"));
CHECK(e.get_error_message() ==
"Null value not allowed for this type");
}
}
......
......@@ -277,8 +277,7 @@ TEST_CASE("Firebird floating point", "[firebird][float]")
}
catch (soci_error const &e)
{
std::string error = e.what();
CHECK(error ==
CHECK(e.get_error_message() ==
"Can't convert value with scale 2 to integral type");
}
......@@ -293,8 +292,7 @@ TEST_CASE("Firebird floating point", "[firebird][float]")
}
catch (soci_error const &e)
{
std::string error = e.what();
CHECK(error ==
CHECK(e.get_error_message() ==
"Can't convert non-integral value to integral column type");
}
......@@ -352,8 +350,7 @@ TEST_CASE("Firebird integers", "[firebird][int]")
}
catch (soci_error const &e)
{
std::string error = e.what();
CHECK(error ==
CHECK(e.get_error_message() ==
"Null value fetched and no indicator defined.");
}
......@@ -438,8 +435,8 @@ TEST_CASE("Firebird bulk operations", "[firebird][bulk]")
}
catch (soci_error const &e)
{
std::string msg = e.what();
CHECK(msg == "Vectors of size 0 are not allowed.");
CHECK(e.get_error_message() ==
"Vectors of size 0 are not allowed.");
}
}
......
......@@ -623,7 +623,7 @@ TEST_CASE("MySQL special floating point values", "[mysql][float]")
try {
st.execute(true);
} catch (soci_error const &e) {
CHECK(e.what() == expectedError);
CHECK(e.get_error_message() == expectedError);
}
}
{
......@@ -634,7 +634,7 @@ TEST_CASE("MySQL special floating point values", "[mysql][float]")
try {
st.execute(true);
} catch (soci_error const &e) {
CHECK(e.what() == expectedError);
CHECK(e.get_error_message() == expectedError);
}
}
{
......@@ -645,7 +645,7 @@ TEST_CASE("MySQL special floating point values", "[mysql][float]")
try {
sql << "insert into soci_test (val) values (:val)", use(v);
} catch (soci_error const &e) {
CHECK(e.what() == expectedError);
CHECK(e.get_error_message() == expectedError);
}
}
{
......@@ -656,7 +656,7 @@ TEST_CASE("MySQL special floating point values", "[mysql][float]")
try {
sql << "insert into soci_test (val) values (:val)", use(v);
} catch (soci_error const &e) {
CHECK(e.what() == expectedError);
CHECK(e.get_error_message() == expectedError);
}
}
}
......
......@@ -400,32 +400,33 @@ TEST_CASE("Oracle bulk insert", "[oracle][insert][bulk]")
ids.push_back(2);
std::vector<int> codes;
codes.push_back(1);
std::string error;
try
{
sql << "insert into soci_test(id,code) values(:id,:code)",
use(ids), use(codes);
FAIL("expected exception not thrown");
}
catch (soci_error const &e)
{
error = e.what();
std::string const error = e.what();
CAPTURE(error);
CHECK(error.find("Bind variable size mismatch")
!= std::string::npos);
}
CAPTURE(error);
CHECK(error.find("Bind variable size mismatch")
!= std::string::npos);
try
{
sql << "select from soci_test", into(ids), into(codes);
FAIL("expected exception not thrown");
}
catch (std::exception const &e)
{
error = e.what();
std::string const error = e.what();
CAPTURE(error);
CHECK(error.find("Bind variable size mismatch")
!= std::string::npos);
}
CAPTURE(error);
CHECK(error.find("Bind variable size mismatch")
!= std::string::npos);
}
// verify partial insert occurs when one of the records is bad
......@@ -434,19 +435,19 @@ TEST_CASE("Oracle bulk insert", "[oracle][insert][bulk]")
ids.push_back(100);
ids.push_back(1000000); // too big for column
std::string error;
try
{
sql << "insert into soci_test (id) values(:id)", use(ids, "id");
FAIL("expected exception not thrown");
}
catch (soci_error const &e)
{
error = e.what();
std::string const error = e.what();
//TODO e could be made to tell which row(s) failed
CAPTURE(error);
CHECK(error.find("ORA-01438") != std::string::npos);
}
sql.commit();
CAPTURE(error);
CHECK(error.find("ORA-01438") != std::string::npos);
int count(7);
sql << "select count(*) from soci_test", into(count);
CHECK(count == 1);
......@@ -534,12 +535,13 @@ TEST_CASE("Oracle bulk insert", "[oracle][insert][bulk]")
try
{
sql << "select code from soci_test", into(intos);
FAIL("expected exception not thrown");
}
catch (soci_error const &e)
{
msg = e.what();
CHECK(e.get_error_message() ==
"Null value fetched and no indicator defined." );
}
CHECK(msg == "Null value fetched and no indicator defined." );
}
// test basic select
......@@ -666,7 +668,7 @@ TEST_CASE("Oracle bulk insert", "[oracle][fetch][bulk]")
}
catch (soci_error const &e)
{
CHECK(std::string(e.what()) ==
CHECK(e.get_error_message() ==
"Increasing the size of the output vector is not supported.");
}
}
......@@ -851,19 +853,19 @@ TEST_CASE("Oracle ORM", "[oracle][orm]")
{
returns_null_procedure_creator procedureCreator(sql);
std::string msg;
person p;
try
{
procedure proc = (sql.prepare << "soci_test(:FIRST_NAME)",
use(p));
proc.execute(1);
FAIL("expected exception not thrown");
}
catch (soci_error& e)
{
msg = e.what();
CHECK(e.get_error_message() ==
"Null value not allowed for this type");
}
CHECK(msg == "Null value not allowed for this type");
procedure proc = (sql.prepare << "soci_test(:GENDER)",
use(p));
......@@ -1002,8 +1004,8 @@ TEST_CASE("Oracle const and modifiable parameters", "[oracle][use]")
}
catch (soci_error const & e)
{
const std::string msg = e.what();
CHECK(msg == "Attempted modification of const use element");
CHECK(e.get_error_message() ==
"Attempted modification of const use element");
}
}
......
......@@ -339,7 +339,8 @@ TEST_CASE("PostgreSQL dynamic backend", "[postgresql][backend][.]")
}
catch (soci_error const & e)
{
CHECK(e.what() == std::string("Failed to open: libsoci_nosuchbackend.so"));
CHECK(e.get_error_message() ==
"Failed to open: libsoci_nosuchbackend.so");
}
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment