Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/fb-cpp/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Exception.h"
#include "Client.h"
#include <string>
#include <vector>
#include <cassert>

using namespace fbcpp;
Expand Down Expand Up @@ -84,3 +85,72 @@ std::string DatabaseException::buildMessage(Client& client, const std::intptr_t*

return message;
}


std::vector<std::intptr_t> DatabaseException::copyErrorVector(const std::intptr_t* statusVector)
{
std::vector<std::intptr_t> result;

if (!statusVector)
return result;

const auto* p = statusVector;

while (*p != isc_arg_end)
{
const auto argType = *p++;

// clang-format off
switch (argType)
{
case isc_arg_gds:
case isc_arg_number:
result.push_back(argType);
result.push_back(*p++);
break;

case isc_arg_string:
case isc_arg_interpreted:
case isc_arg_sql_state:
p++; // skip string pointer
break;

case isc_arg_cstring:
p += 2; // skip length + string pointer
break;

default:
p++; // skip unknown arg value
break;
}
// clang-format on
}

result.push_back(isc_arg_end);

return result;
}


std::string DatabaseException::extractSqlState(const std::intptr_t* statusVector)
{
if (!statusVector)
return {};

const auto* p = statusVector;

while (*p != isc_arg_end)
{
const auto argType = *p++;

if (argType == isc_arg_sql_state)
return reinterpret_cast<const char*>(*p);

if (argType == isc_arg_cstring)
p += 2;
else
p++;
}

return {};
}
43 changes: 40 additions & 3 deletions src/fb-cpp/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "fb-api.h"
#include <stdexcept>
#include <string>
#include <vector>
#include <cstdint>


Expand Down Expand Up @@ -204,13 +205,49 @@ namespace fbcpp
///
/// Constructs a DatabaseException from a Firebird status vector.
///
explicit DatabaseException(Client& client, const std::intptr_t* status)
: FbCppException{buildMessage(client, status)}
explicit DatabaseException(Client& client, const std::intptr_t* statusVector)
: FbCppException{buildMessage(client, statusVector)},
errorVector_{copyErrorVector(statusVector)},
sqlState_{extractSqlState(statusVector)}
{
}

///
/// Returns the Firebird error vector containing isc_arg_gds and isc_arg_number entries.
/// String arguments are excluded to avoid dangling pointers.
/// The vector is terminated by isc_arg_end.
///
const std::vector<std::intptr_t>& getErrors() const noexcept
{
return errorVector_;
}

///
/// Returns the primary ISC error code (first isc_arg_gds value), or 0 if none.
///
std::intptr_t getErrorCode() const noexcept
{
if (errorVector_.size() >= 2 && errorVector_[0] == isc_arg_gds)
return errorVector_[1];
return 0;
}

///
/// Returns the SQL state string (e.g. "42000") if present in the original status vector,
/// or empty otherwise.
///
const std::string& getSqlState() const noexcept
{
return sqlState_;
}

private:
static std::string buildMessage(Client& client, const std::intptr_t* status);
static std::string buildMessage(Client& client, const std::intptr_t* statusVector);
static std::vector<std::intptr_t> copyErrorVector(const std::intptr_t* statusVector);
static std::string extractSqlState(const std::intptr_t* statusVector);

std::vector<std::intptr_t> errorVector_;
std::string sqlState_;
};
} // namespace fbcpp

Expand Down
170 changes: 170 additions & 0 deletions src/test/Exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* MIT License
*
* Copyright (c) 2025 Adriano dos Santos Fernandes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "TestUtil.h"
#include "fb-cpp/Exception.h"
#include "fb-cpp/Statement.h"
#include "fb-cpp/Transaction.h"
#include <string>


BOOST_AUTO_TEST_SUITE(DatabaseExceptionSuite)

BOOST_AUTO_TEST_CASE(syntaxErrorHasNonZeroErrorCode)
{
const auto database = getTempFile("Exception-syntaxErrorHasNonZeroErrorCode.fdb");

Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

try
{
Statement stmt{attachment, transaction, "INVALID SQL STATEMENT !!!"};
BOOST_FAIL("Expected DatabaseException was not thrown");
}
catch (const DatabaseException& ex)
{
BOOST_CHECK_NE(ex.getErrorCode(), 0);
}
}

BOOST_AUTO_TEST_CASE(errorVectorContainsIscArgGds)
{
const auto database = getTempFile("Exception-errorVectorContainsIscArgGds.fdb");

Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

try
{
Statement stmt{attachment, transaction, "INVALID SQL"};
BOOST_FAIL("Expected DatabaseException was not thrown");
}
catch (const DatabaseException& ex)
{
const auto& errors = ex.getErrors();
BOOST_REQUIRE(!errors.empty());
BOOST_CHECK_EQUAL(errors[0], isc_arg_gds);
}
}

BOOST_AUTO_TEST_CASE(errorVectorIsTerminatedByIscArgEnd)
{
const auto database = getTempFile("Exception-errorVectorIsTerminatedByIscArgEnd.fdb");

Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

try
{
Statement stmt{attachment, transaction, "INVALID SQL"};
BOOST_FAIL("Expected DatabaseException was not thrown");
}
catch (const DatabaseException& ex)
{
const auto& errors = ex.getErrors();
BOOST_REQUIRE(!errors.empty());
BOOST_CHECK_EQUAL(errors.back(), isc_arg_end);
}
}

BOOST_AUTO_TEST_CASE(getErrorCodeReturnsFirstGdsCode)
{
const auto database = getTempFile("Exception-getErrorCodeReturnsFirstGdsCode.fdb");

Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

try
{
Statement stmt{attachment, transaction, "INVALID SQL"};
BOOST_FAIL("Expected DatabaseException was not thrown");
}
catch (const DatabaseException& ex)
{
const auto& errors = ex.getErrors();
BOOST_REQUIRE(errors.size() >= 2);
BOOST_CHECK_EQUAL(ex.getErrorCode(), errors[1]);
}
}

BOOST_AUTO_TEST_CASE(sqlStateIsExtractedForSyntaxError)
{
const auto database = getTempFile("Exception-sqlStateIsExtractedForSyntaxError.fdb");

Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

try
{
Statement stmt{attachment, transaction, "INVALID SQL"};
BOOST_FAIL("Expected DatabaseException was not thrown");
}
catch (const DatabaseException& ex)
{
// Firebird 3.0+ includes SQL state in the status vector.
BOOST_CHECK(!ex.getSqlState().empty());
}
}

BOOST_AUTO_TEST_CASE(defaultConstructedHasEmptyErrorVector)
{
DatabaseException ex{"test error message"};
BOOST_CHECK(ex.getErrors().empty());
BOOST_CHECK_EQUAL(ex.getErrorCode(), 0);
BOOST_CHECK(ex.getSqlState().empty());
}

BOOST_AUTO_TEST_CASE(whatPreservesFormattedMessage)
{
const auto database = getTempFile("Exception-whatPreservesFormattedMessage.fdb");

Attachment attachment{CLIENT, database, AttachmentOptions().setCreateDatabase(true)};
FbDropDatabase attachmentDrop{attachment};

Transaction transaction{attachment};

try
{
Statement stmt{attachment, transaction, "INVALID SQL"};
BOOST_FAIL("Expected DatabaseException was not thrown");
}
catch (const DatabaseException& ex)
{
std::string message = ex.what();
BOOST_CHECK(!message.empty());
}
}

BOOST_AUTO_TEST_SUITE_END()
Loading