Skip to content

Commit 392a429

Browse files
committed
boxart: replace invalid utf-8 chars instead of failing when saving
Open (and truncate) the file only after json serialization.
1 parent 0491f2e commit 392a429

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

core/ui/boxart/boxart.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void Boxart::fetchBoxart()
7777
{
7878
try {
7979
fetching.get();
80-
} catch (const std::runtime_error& e) {
80+
} catch (const std::exception& e) {
8181
ERROR_LOG(COMMON, "Boxart scraper thread exception: %s", e.what());
8282
} catch (...) {
8383
ERROR_LOG(COMMON, "Boxart scraper thread unknown exception");
@@ -175,12 +175,6 @@ void Boxart::saveDatabase()
175175
return;
176176
std::string basePath = getSaveDirectory();
177177
std::string db_name = basePath + DB_NAME;
178-
FILE *file = nowide::fopen(db_name.c_str(), "wt");
179-
if (file == nullptr)
180-
{
181-
WARN_LOG(COMMON, "Can't save boxart database to %s: error %d", db_name.c_str(), errno);
182-
return;
183-
}
184178
DEBUG_LOG(COMMON, "Saving boxart database to %s", db_name.c_str());
185179

186180
json array;
@@ -190,7 +184,13 @@ void Boxart::saveDatabase()
190184
if (game.second.scraped || game.second.parsed)
191185
array.push_back(game.second.to_json(basePath));
192186
}
193-
std::string serialized = array.dump(4);
187+
std::string serialized = array.dump(4, ' ', false, json::error_handler_t::replace);
188+
189+
FILE *file = nowide::fopen(db_name.c_str(), "wt");
190+
if (file == nullptr) {
191+
WARN_LOG(COMMON, "Can't save boxart database to %s: error %d", db_name.c_str(), errno);
192+
return;
193+
}
194194
fwrite(serialized.c_str(), 1, serialized.size(), file);
195195
fclose(file);
196196
databaseDirty = false;

0 commit comments

Comments
 (0)