Skip to content

Commit 786bb9f

Browse files
authored
Merge pull request #1868 from joto/add-fmt-error-func
Add fmt_error() function that uses fmt lib to format error message
2 parents e3cd251 + 21a1e73 commit 786bb9f

20 files changed

+191
-230
lines changed

src/expire-tiles.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ quadkey_list_t expire_tiles::get_tiles()
251251
void expire_tiles::merge_and_destroy(expire_tiles *other)
252252
{
253253
if (m_map_width != other->m_map_width) {
254-
throw std::runtime_error{"Unable to merge tile expiry sets when "
255-
"map_width does not match: {} != {}."_format(
256-
m_map_width, other->m_map_width)};
254+
throw fmt_error("Unable to merge tile expiry sets when "
255+
"map_width does not match: {} != {}.",
256+
m_map_width, other->m_map_width);
257257
}
258258

259259
if (m_dirty_tiles.empty()) {

src/flex-lua-index.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ static void check_and_add_column(flex_table_t const &table,
2222
{
2323
auto const *column = util::find_by_name(table, column_name);
2424
if (!column) {
25-
throw std::runtime_error{"Unknown column '{}' in table '{}'."_format(
26-
column_name, table.name())};
25+
throw fmt_error("Unknown column '{}' in table '{}'.", column_name,
26+
table.name());
2727
}
2828
columns->push_back(column_name);
2929
}
@@ -53,7 +53,7 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
5353
char const *const method =
5454
luaX_get_table_string(lua_state, "method", -1, "Index definition");
5555
if (!has_index_method(method)) {
56-
throw std::runtime_error{"Unknown index method '{}'."_format(method)};
56+
throw fmt_error("Unknown index method '{}'.", method);
5757
}
5858
lua_pop(lua_state, 1);
5959
auto &index = table->add_index(method);
@@ -105,9 +105,9 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
105105
}
106106
index.set_include_columns(include_columns);
107107
} else if (!lua_isnil(lua_state, -1)) {
108-
throw std::runtime_error{
109-
"Database version ({}) doesn't support"
110-
" include columns in indexes."_format(get_database_version())};
108+
throw fmt_error("Database version ({}) doesn't support"
109+
" include columns in indexes.",
110+
get_database_version());
111111
}
112112
lua_pop(lua_state, 1);
113113

@@ -117,7 +117,7 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
117117
lua_pop(lua_state, 1);
118118
check_identifier(tablespace, "tablespace");
119119
if (!has_tablespace(tablespace)) {
120-
throw std::runtime_error{"Unknown tablespace '{}'."_format(tablespace)};
120+
throw fmt_error("Unknown tablespace '{}'.", tablespace);
121121
}
122122
index.set_tablespace(tablespace.empty() ? table->index_tablespace()
123123
: tablespace);

src/flex-table-column.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static table_column_type get_column_type_from_string(std::string const &type)
5858
{
5959
auto const *column_type = util::find_by_name(column_types, type);
6060
if (!column_type) {
61-
throw std::runtime_error{"Unknown column type '{}'."_format(type)};
61+
throw fmt_error("Unknown column type '{}'.", type);
6262
}
6363

6464
return column_type->type;
@@ -106,8 +106,7 @@ void flex_table_column_t::set_projection(char const *projection)
106106
m_srid = static_cast<int>(std::strtoul(projection, &end, 10));
107107

108108
if (*end != '\0') {
109-
throw std::runtime_error{
110-
"Unknown projection: '{}'."_format(projection)};
109+
throw fmt_error("Unknown projection: '{}'.", projection);
111110
}
112111
}
113112

src/flex-table.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,19 +262,18 @@ void table_connection_t::start(bool append)
262262
assert(m_db_connection);
263263

264264
if (!has_schema(table().schema())) {
265-
throw std::runtime_error{
266-
"Schema '{0}' not available. "
267-
"Use 'CREATE SCHEMA \"{0}\";' to create it."_format(
268-
table().schema())};
265+
throw fmt_error("Schema '{0}' not available."
266+
" Use 'CREATE SCHEMA \"{0}\";' to create it.",
267+
table().schema());
269268
}
270269

271270
for (auto const &ts :
272271
{table().data_tablespace(), table().index_tablespace()}) {
273272
if (!has_tablespace(ts)) {
274-
throw std::runtime_error{
275-
"Tablespace '{0}' not available. "
276-
"Use 'CREATE TABLESPACE \"{0}\" ...;' to create it."_format(
277-
ts)};
273+
throw fmt_error(
274+
"Tablespace '{0}' not available."
275+
" Use 'CREATE TABLESPACE \"{0}\" ...;' to create it.",
276+
ts);
278277
}
279278
}
280279

src/flex-write.cpp

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,8 @@ static void write_json_table(json_writer_t *writer, lua_State *lua_state,
201201
while (lua_next(lua_state, -2) != 0) {
202202
int const ltype_key = lua_type(lua_state, -2);
203203
if (ltype_key != LUA_TSTRING) {
204-
throw std::runtime_error{
205-
"Incorrect data type '{}' as key."_format(
206-
lua_typename(lua_state, ltype_key))};
204+
throw fmt_error("Incorrect data type '{}' as key.",
205+
lua_typename(lua_state, ltype_key));
207206
}
208207
char const *const key = lua_tostring(lua_state, -2);
209208
writer->key(key);
@@ -260,9 +259,8 @@ static void write_json(json_writer_t *writer, lua_State *lua_state,
260259
write_json_table(writer, lua_state, tables);
261260
break;
262261
default:
263-
throw std::runtime_error{
264-
"Invalid type '{}' for json/jsonb column."_format(
265-
lua_typename(lua_state, ltype))};
262+
throw fmt_error("Invalid type '{}' for json/jsonb column.",
263+
lua_typename(lua_state, ltype));
266264
}
267265
}
268266

@@ -323,9 +321,8 @@ void flex_write_column(lua_State *lua_state,
323321
if (column.type() == table_column_type::text) {
324322
auto const *const str = lua_tolstring(lua_state, -1, nullptr);
325323
if (!str) {
326-
throw std::runtime_error{
327-
"Invalid type '{}' for text column."_format(
328-
lua_typename(lua_state, ltype))};
324+
throw fmt_error("Invalid type '{}' for text column.",
325+
lua_typename(lua_state, ltype));
329326
}
330327
copy_mgr->add_column(str);
331328
} else if (column.type() == table_column_type::boolean) {
@@ -341,9 +338,8 @@ void flex_write_column(lua_State *lua_state,
341338
lua_tolstring(lua_state, -1, nullptr));
342339
break;
343340
default:
344-
throw std::runtime_error{
345-
"Invalid type '{}' for boolean column."_format(
346-
lua_typename(lua_state, ltype))};
341+
throw fmt_error("Invalid type '{}' for boolean column.",
342+
lua_typename(lua_state, ltype));
347343
}
348344
} else if (column.type() == table_column_type::int2) {
349345
if (ltype == LUA_TNUMBER) {
@@ -360,9 +356,8 @@ void flex_write_column(lua_State *lua_state,
360356
} else if (ltype == LUA_TBOOLEAN) {
361357
copy_mgr->add_column(lua_toboolean(lua_state, -1));
362358
} else {
363-
throw std::runtime_error{
364-
"Invalid type '{}' for int2 column."_format(
365-
lua_typename(lua_state, ltype))};
359+
throw fmt_error("Invalid type '{}' for int2 column.",
360+
lua_typename(lua_state, ltype));
366361
}
367362
} else if (column.type() == table_column_type::int4) {
368363
if (ltype == LUA_TNUMBER) {
@@ -379,9 +374,8 @@ void flex_write_column(lua_State *lua_state,
379374
} else if (ltype == LUA_TBOOLEAN) {
380375
copy_mgr->add_column(lua_toboolean(lua_state, -1));
381376
} else {
382-
throw std::runtime_error{
383-
"Invalid type '{}' for int4 column."_format(
384-
lua_typename(lua_state, ltype))};
377+
throw fmt_error("Invalid type '{}' for int4 column.",
378+
lua_typename(lua_state, ltype));
385379
}
386380
} else if (column.type() == table_column_type::int8) {
387381
if (ltype == LUA_TNUMBER) {
@@ -392,9 +386,8 @@ void flex_write_column(lua_State *lua_state,
392386
} else if (ltype == LUA_TBOOLEAN) {
393387
copy_mgr->add_column(lua_toboolean(lua_state, -1));
394388
} else {
395-
throw std::runtime_error{
396-
"Invalid type '{}' for int8 column."_format(
397-
lua_typename(lua_state, ltype))};
389+
throw fmt_error("Invalid type '{}' for int8 column.",
390+
lua_typename(lua_state, ltype));
398391
}
399392
} else if (column.type() == table_column_type::real) {
400393
if (ltype == LUA_TNUMBER) {
@@ -403,9 +396,8 @@ void flex_write_column(lua_State *lua_state,
403396
write_double(copy_mgr, column,
404397
lua_tolstring(lua_state, -1, nullptr));
405398
} else {
406-
throw std::runtime_error{
407-
"Invalid type '{}' for real column."_format(
408-
lua_typename(lua_state, ltype))};
399+
throw fmt_error("Invalid type '{}' for real column.",
400+
lua_typename(lua_state, ltype));
409401
}
410402
} else if (column.type() == table_column_type::hstore) {
411403
if (ltype == LUA_TTABLE) {
@@ -417,27 +409,26 @@ void flex_write_column(lua_State *lua_state,
417409
char const *const val = lua_tostring(lua_state, -1);
418410
if (key == nullptr) {
419411
int const ltype_key = lua_type(lua_state, -2);
420-
throw std::runtime_error{
412+
throw fmt_error(
421413
"NULL key for hstore. Possibly this is due to"
422-
" an incorrect data type '{}' as key."_format(
423-
lua_typename(lua_state, ltype_key))};
414+
" an incorrect data type '{}' as key.",
415+
lua_typename(lua_state, ltype_key));
424416
}
425417
if (val == nullptr) {
426418
int const ltype_value = lua_type(lua_state, -1);
427-
throw std::runtime_error{
419+
throw fmt_error(
428420
"NULL value for hstore. Possibly this is due to"
429-
" an incorrect data type '{}' for key '{}'."_format(
430-
lua_typename(lua_state, ltype_value), key)};
421+
" an incorrect data type '{}' for key '{}'.",
422+
lua_typename(lua_state, ltype_value), key);
431423
}
432424
copy_mgr->add_hash_elem(key, val);
433425
lua_pop(lua_state, 1);
434426
}
435427

436428
copy_mgr->finish_hash();
437429
} else {
438-
throw std::runtime_error{
439-
"Invalid type '{}' for hstore column."_format(
440-
lua_typename(lua_state, ltype))};
430+
throw fmt_error("Invalid type '{}' for hstore column.",
431+
lua_typename(lua_state, ltype));
441432
}
442433
} else if ((column.type() == table_column_type::json) ||
443434
(column.type() == table_column_type::jsonb)) {
@@ -458,9 +449,8 @@ void flex_write_column(lua_State *lua_state,
458449
lua_tolstring(lua_state, -1, nullptr));
459450
break;
460451
default:
461-
throw std::runtime_error{
462-
"Invalid type '{}' for direction column."_format(
463-
lua_typename(lua_state, ltype))};
452+
throw fmt_error("Invalid type '{}' for direction column.",
453+
lua_typename(lua_state, ltype));
464454
}
465455
} else if (column.is_geometry_column()) {
466456
// If this is a geometry column, the Lua function 'insert()' was
@@ -471,10 +461,9 @@ void flex_write_column(lua_State *lua_state,
471461
if (geom && !geom->is_null()) {
472462
auto const type = column.type();
473463
if (!is_compatible(*geom, type)) {
474-
throw std::runtime_error{
475-
"Geometry data for geometry column '{}'"
476-
" has the wrong type ({})."_format(
477-
column.name(), geometry_type(*geom))};
464+
throw fmt_error("Geometry data for geometry column '{}'"
465+
" has the wrong type ({}).",
466+
column.name(), geometry_type(*geom));
478467
}
479468
bool const wrap_multi =
480469
(type == table_column_type::multipoint ||
@@ -494,9 +483,8 @@ void flex_write_column(lua_State *lua_state,
494483
write_null(copy_mgr, column);
495484
}
496485
} else {
497-
throw std::runtime_error{
498-
"Need geometry data for geometry column '{}'."_format(
499-
column.name())};
486+
throw fmt_error("Need geometry data for geometry column '{}'.",
487+
column.name());
500488
}
501489
} else if (column.type() == table_column_type::area) {
502490
// If this is an area column, the Lua function 'insert()' was
@@ -505,8 +493,8 @@ void flex_write_column(lua_State *lua_state,
505493
throw std::runtime_error{"Column type 'area' not allowed with "
506494
"'insert()'. Maybe use 'real'?"};
507495
} else {
508-
throw std::runtime_error{"Column type {} not implemented."_format(
509-
static_cast<uint8_t>(column.type()))};
496+
throw fmt_error("Column type {} not implemented.",
497+
static_cast<uint8_t>(column.type()));
510498
}
511499

512500
lua_pop(lua_state, 1);

src/format.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313
#define FMT_HEADER_ONLY
1414
#include <fmt/format.h>
1515

16+
#include <stdexcept>
17+
1618
// NOLINTNEXTLINE(google-global-names-in-headers,google-build-using-namespace)
1719
using namespace fmt::literals;
1820

21+
template <typename S, typename... TArgs>
22+
std::runtime_error fmt_error(S const &format_str, TArgs &&...args)
23+
{
24+
return std::runtime_error{
25+
fmt::format(format_str, std::forward<TArgs>(args)...)};
26+
}
27+
1928
#endif // OSM2PGSQL_FORMAT_HPP

src/geom-transform.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ bool geom_transform_area_t::set_param(char const *name, lua_State *lua_state)
102102
return true;
103103
}
104104

105-
throw std::runtime_error{"Unknown value for 'split_at' field in a geometry"
106-
" transformation: '{}'"_format(val)};
105+
throw fmt_error("Unknown value for 'split_at' field in a geometry"
106+
" transformation: '{}'",
107+
val);
107108
}
108109

109110
bool geom_transform_area_t::is_compatible_with(
@@ -142,8 +143,7 @@ std::unique_ptr<geom_transform_t> create_geom_transform(char const *type)
142143
return std::make_unique<geom_transform_area_t>();
143144
}
144145

145-
throw std::runtime_error{
146-
"Unknown geometry transformation '{}'."_format(type)};
146+
throw fmt_error("Unknown geometry transformation '{}'.", type);
147147
}
148148

149149
void init_geom_transform(geom_transform_t *transform, lua_State *lua_state)
@@ -194,26 +194,24 @@ get_transform(lua_State *lua_state, flex_table_column_t const &column)
194194
// Field set to anything but a Lua table is not allowed
195195
if (ltype != LUA_TTABLE) {
196196
lua_pop(lua_state, 1); // geom field
197-
throw std::runtime_error{
198-
"Invalid geometry transformation for column '{}'."_format(
199-
column.name())};
197+
throw fmt_error("Invalid geometry transformation for column '{}'.",
198+
column.name());
200199
}
201200

202201
lua_getfield(lua_state, -1, "create");
203202
char const *create_type = lua_tostring(lua_state, -1);
204203
if (create_type == nullptr) {
205-
throw std::runtime_error{
206-
"Missing geometry transformation for column '{}'."_format(
207-
column.name())};
204+
throw fmt_error("Missing geometry transformation for column '{}'.",
205+
column.name());
208206
}
209207

210208
transform = create_geom_transform(create_type);
211209
lua_pop(lua_state, 1); // 'create' field
212210
init_geom_transform(transform.get(), lua_state);
213211
if (!transform->is_compatible_with(column.type())) {
214-
throw std::runtime_error{
215-
"Geometry transformation is not compatible "
216-
"with column type '{}'."_format(column.type_name())};
212+
throw fmt_error("Geometry transformation is not compatible"
213+
" with column type '{}'.",
214+
column.type_name());
217215
}
218216

219217
lua_pop(lua_state, 1); // geom field
@@ -246,7 +244,6 @@ geom_transform_t const *get_default_transform(flex_table_column_t const &column,
246244
break;
247245
}
248246

249-
throw std::runtime_error{
250-
"Missing geometry transformation for column '{}'."_format(
251-
column.name())};
247+
throw fmt_error("Missing geometry transformation for column '{}'.",
248+
column.name());
252249
}

0 commit comments

Comments
 (0)