Commit 57db6221 authored by Maciej Sobczak's avatar Maciej Sobczak

Added basic support for error categories.

parent a1d45503
......@@ -40,6 +40,21 @@ public:
// highest level context.
void add_context(std::string const& context);
// Basic error classes.
enum error_category
{
connection_error,
invalid_statement,
no_privilege,
no_data,
constraint_violation,
system_error,
unknown
};
// Basic error classification support
virtual error_category get_error_category() const { return unknown; }
private:
// Optional extra information (currently just the context data).
class soci_error_extra_info* info_;
......
......@@ -40,7 +40,10 @@ class SOCI_ORACLE_DECL oracle_soci_error : public soci_error
public:
oracle_soci_error(std::string const & msg, int errNum = 0);
virtual error_category get_error_category() const { return cat_; }
int err_num_;
error_category cat_;
};
......
......@@ -38,8 +38,11 @@ public:
std::string sqlstate() const;
virtual error_category get_error_category() const { return cat_; }
private:
char sqlstate_[ 5 ]; // not std::string to keep copy-constructor no-throw
error_category cat_;
};
namespace details
......
......@@ -20,8 +20,16 @@ using namespace soci::details;
using namespace soci::details::oracle;
oracle_soci_error::oracle_soci_error(std::string const & msg, int errNum)
: soci_error(msg), err_num_(errNum)
: soci_error(msg), err_num_(errNum), cat_(unknown)
{
if (errNum == 12162)
{
cat_ = connection_error;
}
else if (errNum == 1400)
{
cat_ = constraint_violation;
}
}
void soci::details::oracle::get_error_details(sword res, OCIError *errhp,
......
......@@ -14,9 +14,37 @@ using namespace soci::details;
postgresql_soci_error::postgresql_soci_error(
std::string const & msg, char const *sqlst)
: soci_error(msg)
: soci_error(msg), cat_(unknown)
{
std::memcpy(sqlstate_, sqlst, 5);
if (std::memcmp(sqlst, "08", 2) == 0)
{
cat_ = connection_error;
}
else if (std::memcmp(sqlst, "42501", 5) == 0)
{
cat_ = no_privilege;
}
else if (std::memcmp(sqlst, "42", 2) == 0)
{
cat_ = invalid_statement;
}
else if (std::memcmp(sqlst, "02", 2) == 0)
{
cat_ = no_data;
}
else if (std::memcmp(sqlst, "23", 2) == 0)
{
cat_ = constraint_violation;
}
else if ((std::memcmp(sqlst, "53", 2) == 0) ||
(std::memcmp(sqlst, "54", 2) == 0) ||
(std::memcmp(sqlst, "58", 2) == 0) ||
(std::memcmp(sqlst, "XX", 2) == 0))
{
cat_ = system_error;
}
}
std::string postgresql_soci_error::sqlstate() const
......
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