fb-cpp 0.0.1
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
Exception.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "Exception.h"
26#include "Client.h"
27#include <string>
28#include <cassert>
29
30using namespace fbcpp;
31using namespace fbcpp::impl;
32
33
34void StatusWrapper::checkException(StatusWrapper* status)
35{
36 if (status->dirty && (status->getState() & fb::IStatus::STATE_ERRORS))
37 throw DatabaseException{status->client, status->getErrors()};
38}
39
40void StatusWrapper::catchException(fb::IStatus* status) noexcept
41{
42 assert(false);
43}
44
45
46std::string DatabaseException::buildMessage(Client& client, const std::intptr_t* statusVector)
47{
48 constexpr char DEFAULT_MESSAGE[] = "Unknown database error";
49
50 if (!statusVector)
51 return DEFAULT_MESSAGE;
52
53 const auto util = client.getUtil();
54
55 const auto status = client.newStatus();
56 status->setErrors(statusVector);
57
58 constexpr unsigned MAX_BUFFER_SIZE = 32u * 1024u;
59 unsigned bufferSize = 256u;
60 std::string message;
61
62 while (bufferSize <= MAX_BUFFER_SIZE)
63 {
64 std::string buffer(bufferSize, '\0');
65 const auto written = util->formatStatus(buffer.data(), bufferSize, status.get());
66
67 if (written < bufferSize && buffer[0] != '\0')
68 {
69 message = written == 0 ? std::string{buffer.c_str()} : std::string{buffer.data(), written};
70 break;
71 }
72
73 if (bufferSize == MAX_BUFFER_SIZE)
74 {
75 message = buffer.c_str();
76 break;
77 }
78
79 bufferSize = (bufferSize > MAX_BUFFER_SIZE / 2u) ? MAX_BUFFER_SIZE : bufferSize * 2u;
80 }
81
82 if (message.empty())
83 message = DEFAULT_MESSAGE;
84
85 return message;
86}
Represents a Firebird client library instance.
Definition Client.h:53
fb::IUtil * getUtil()
Returns a Firebird IUtil interface.
Definition Client.h:142
FbUniquePtr< fb::IStatus > newStatus()
Creates and returns a Firebird IStatus instance.
Definition Client.h:183
fb-cpp namespace.
Definition Attachment.h:42