Skip to content

Commit 5fbb3f1

Browse files
committed
Improved error messages
1 parent 208f446 commit 5fbb3f1

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

include/pgvector/pqxx.hpp

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -121,53 +121,47 @@ template <> struct string_traits<pgvector::SparseVector> {
121121
static constexpr bool converts_from_string{true};
122122

123123
static pgvector::SparseVector from_string(std::string_view text) {
124-
try {
125-
if (text.size() < 4 || text.front() != '{') {
126-
throw conversion_error("Malformed sparsevec literal");
127-
}
128-
129-
size_t n = text.find("}/", 1);
130-
if (n == std::string_view::npos) {
131-
throw conversion_error("Malformed sparsevec literal");
132-
}
124+
if (text.size() < 4 || text.front() != '{') {
125+
throw conversion_error("Malformed sparsevec literal");
126+
}
133127

134-
int dimensions = std::stoi(std::string(text.substr(n + 2)));
135-
if (dimensions < 0) {
136-
throw conversion_error("Dimensions cannot be negative");
137-
}
128+
size_t n = text.find("}/", 1);
129+
if (n == std::string_view::npos) {
130+
throw conversion_error("Malformed sparsevec literal");
131+
}
138132

139-
std::vector<int> indices;
140-
std::vector<float> values;
133+
int dimensions = pqxx::from_string<int>(text.substr(n + 2));
134+
if (dimensions < 0) {
135+
throw conversion_error("Dimensions cannot be negative");
136+
}
141137

142-
if (n > 1) {
143-
std::istringstream ss(std::string(text.substr(1, n)));
144-
while (ss.good()) {
145-
std::string substr;
146-
std::getline(ss, substr, ',');
138+
std::vector<int> indices;
139+
std::vector<float> values;
147140

148-
size_t ne = substr.find(":");
149-
if (ne == std::string::npos) {
150-
throw conversion_error("Malformed sparsevec literal");
151-
}
141+
if (n > 1) {
142+
std::istringstream ss(std::string(text.substr(1, n - 1)));
143+
while (ss.good()) {
144+
std::string substr;
145+
std::getline(ss, substr, ',');
152146

153-
int index = std::stoi(substr.substr(0, ne));
154-
float value = std::stof(substr.substr(ne + 1));
147+
size_t ne = substr.find(":");
148+
if (ne == std::string::npos) {
149+
throw conversion_error("Malformed sparsevec literal");
150+
}
155151

156-
if (index < 1) {
157-
throw conversion_error("Index out of bounds");
158-
}
152+
int index = pqxx::from_string<int>(substr.substr(0, ne));
153+
float value = pqxx::from_string<float>(substr.substr(ne + 1));
159154

160-
indices.push_back(index - 1);
161-
values.push_back(value);
155+
if (index < 1) {
156+
throw conversion_error("Index out of bounds");
162157
}
163-
}
164158

165-
return pgvector::SparseVector(dimensions, indices, values);
166-
} catch (const std::invalid_argument& e) {
167-
throw conversion_error("No conversion");
168-
} catch (const std::out_of_range& e) {
169-
throw conversion_error("Out of range");
159+
indices.push_back(index - 1);
160+
values.push_back(value);
161+
}
170162
}
163+
164+
return pgvector::SparseVector(dimensions, indices, values);
171165
}
172166

173167
static zview to_buf(char* begin, char* end, const pgvector::SparseVector& value) {

test/pqxx_test.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <optional>
33
#include <string>
44
#include <vector>
5+
#include <iostream>
56

67
#include <pqxx/pqxx>
78

@@ -245,7 +246,7 @@ void test_sparsevec_from_string() {
245246
auto unused = pqxx::from_string<pgvector::SparseVector>("{ }/");
246247
assert(false);
247248
} catch (const pqxx::conversion_error& e) {
248-
assert(std::string_view(e.what()) == "No conversion");
249+
assert(std::string_view(e.what()) == "Could not convert '' to int");
249250
}
250251

251252
try {
@@ -259,7 +260,7 @@ void test_sparsevec_from_string() {
259260
auto unused = pqxx::from_string<pgvector::SparseVector>("{:}/1");
260261
assert(false);
261262
} catch (const pqxx::conversion_error& e) {
262-
assert(std::string_view(e.what()) == "No conversion");
263+
assert(std::string_view(e.what()) == "Could not convert '' to int");
263264
}
264265

265266
try {
@@ -287,28 +288,28 @@ void test_sparsevec_from_string() {
287288
auto unused = pqxx::from_string<pgvector::SparseVector>("{1:4e38}/1");
288289
assert(false);
289290
} catch (const pqxx::conversion_error& e) {
290-
assert(std::string_view(e.what()) == "Out of range");
291+
assert(std::string_view(e.what()) == "Could not convert string to numeric value: '4e38'.");
291292
}
292293

293294
try {
294295
auto unused = pqxx::from_string<pgvector::SparseVector>("{a:1}/1");
295296
assert(false);
296297
} catch (const pqxx::conversion_error& e) {
297-
assert(std::string_view(e.what()) == "No conversion");
298+
assert(std::string_view(e.what()) == "Could not convert 'a' to int");
298299
}
299300

300301
try {
301302
auto unused = pqxx::from_string<pgvector::SparseVector>("{1:a}/1");
302303
assert(false);
303304
} catch (const pqxx::conversion_error& e) {
304-
assert(std::string_view(e.what()) == "No conversion");
305+
assert(std::string_view(e.what()) == "Could not convert string to numeric value: 'a'.");
305306
}
306307

307308
try {
308309
auto unused = pqxx::from_string<pgvector::SparseVector>("{}/a");
309310
assert(false);
310311
} catch (const pqxx::conversion_error& e) {
311-
assert(std::string_view(e.what()) == "No conversion");
312+
assert(std::string_view(e.what()) == "Could not convert 'a' to int");
312313
}
313314
}
314315

0 commit comments

Comments
 (0)