Skip to content

Commit 356d9ef

Browse files
committed
Updated exception types
1 parent 5f3aff7 commit 356d9ef

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

include/pgvector/pqxx.hpp

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,47 +121,53 @@ 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-
if (text.size() < 4 || text.front() != '{') {
125-
throw conversion_error("Malformed sparsevec literal");
126-
}
124+
try {
125+
if (text.size() < 4 || text.front() != '{') {
126+
throw conversion_error("Malformed sparsevec literal");
127+
}
127128

128-
size_t n = text.find("}/", 1);
129-
if (n == std::string_view::npos) {
130-
throw conversion_error("Malformed sparsevec literal");
131-
}
129+
size_t n = text.find("}/", 1);
130+
if (n == std::string_view::npos) {
131+
throw conversion_error("Malformed sparsevec literal");
132+
}
132133

133-
int dimensions = std::stoi(std::string(text.substr(n + 2)));
134-
if (dimensions < 0) {
135-
throw conversion_error("Malformed sparsevec literal");
136-
}
134+
int dimensions = std::stoi(std::string(text.substr(n + 2)));
135+
if (dimensions < 0) {
136+
throw conversion_error("Malformed sparsevec literal");
137+
}
137138

138-
std::vector<int> indices;
139-
std::vector<float> values;
139+
std::vector<int> indices;
140+
std::vector<float> values;
140141

141-
if (n > 1) {
142-
std::istringstream ss(std::string(text.substr(1, n)));
143-
while (ss.good()) {
144-
std::string substr;
145-
std::getline(ss, substr, ',');
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, ',');
146147

147-
size_t ne = substr.find(":");
148-
if (ne == std::string::npos) {
149-
throw conversion_error("Malformed sparsevec literal");
150-
}
148+
size_t ne = substr.find(":");
149+
if (ne == std::string::npos) {
150+
throw conversion_error("Malformed sparsevec literal");
151+
}
151152

152-
int index = std::stoi(substr.substr(0, ne));
153-
float value = std::stof(substr.substr(ne + 1));
153+
int index = std::stoi(substr.substr(0, ne));
154+
float value = std::stof(substr.substr(ne + 1));
154155

155-
if (index < 1) {
156-
throw conversion_error("Malformed sparsevec literal");
157-
}
156+
if (index < 1) {
157+
throw conversion_error("Malformed sparsevec literal");
158+
}
158159

159-
indices.push_back(index - 1);
160-
values.push_back(value);
160+
indices.push_back(index - 1);
161+
values.push_back(value);
162+
}
161163
}
162-
}
163164

164-
return pgvector::SparseVector(dimensions, indices, values);
165+
return pgvector::SparseVector(dimensions, indices, values);
166+
} catch (const std::invalid_argument& e) {
167+
throw conversion_error(e.what());
168+
} catch (const std::out_of_range& e) {
169+
throw conversion_error(e.what());
170+
}
165171
}
166172

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

test/pqxx_test.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,11 @@ void test_sparsevec_from_string() {
241241
assert(std::string_view(e.what()) == "Malformed sparsevec literal");
242242
}
243243

244-
// TODO change to pqxx::conversion_error
245244
try {
246245
auto unused = pqxx::from_string<pgvector::SparseVector>("{ }/");
247246
assert(false);
248-
} catch (const std::invalid_argument& e) {
249-
assert(true);
247+
} catch (const pqxx::conversion_error& e) {
248+
assert(std::string_view(e.what()) == "stoi: no conversion");
250249
}
251250

252251
try {
@@ -256,12 +255,11 @@ void test_sparsevec_from_string() {
256255
assert(std::string_view(e.what()) == "Malformed sparsevec literal");
257256
}
258257

259-
// TODO change to pqxx::conversion_error
260258
try {
261259
auto unused = pqxx::from_string<pgvector::SparseVector>("{:}/1");
262260
assert(false);
263-
} catch (const std::invalid_argument& e) {
264-
assert(true);
261+
} catch (const pqxx::conversion_error& e) {
262+
assert(std::string_view(e.what()) == "stoi: no conversion");
265263
}
266264

267265
try {
@@ -285,36 +283,32 @@ void test_sparsevec_from_string() {
285283
assert(std::string_view(e.what()) == "Malformed sparsevec literal");
286284
}
287285

288-
// TODO change to pqxx::conversion_error
289286
try {
290287
auto unused = pqxx::from_string<pgvector::SparseVector>("{1:4e38}/1");
291288
assert(false);
292-
} catch (const std::out_of_range& e) {
293-
assert(true);
289+
} catch (const pqxx::conversion_error& e) {
290+
assert(std::string_view(e.what()) == "stof: out of range");
294291
}
295292

296-
// TODO change to pqxx::conversion_error
297293
try {
298294
auto unused = pqxx::from_string<pgvector::SparseVector>("{a:1}/1");
299295
assert(false);
300-
} catch (const std::invalid_argument& e) {
301-
assert(true);
296+
} catch (const pqxx::conversion_error& e) {
297+
assert(std::string_view(e.what()) == "stoi: no conversion");
302298
}
303299

304-
// TODO change to pqxx::conversion_error
305300
try {
306301
auto unused = pqxx::from_string<pgvector::SparseVector>("{1:a}/1");
307302
assert(false);
308-
} catch (const std::invalid_argument& e) {
309-
assert(true);
303+
} catch (const pqxx::conversion_error& e) {
304+
assert(std::string_view(e.what()) == "stof: no conversion");
310305
}
311306

312-
// TODO change to pqxx::conversion_error
313307
try {
314308
auto unused = pqxx::from_string<pgvector::SparseVector>("{}/a");
315309
assert(false);
316-
} catch (const std::invalid_argument& e) {
317-
assert(true);
310+
} catch (const pqxx::conversion_error& e) {
311+
assert(std::string_view(e.what()) == "stoi: no conversion");
318312
}
319313
}
320314

0 commit comments

Comments
 (0)