Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: More explicative error messages #42

Merged
merged 8 commits into from
Jan 21, 2024
28 changes: 23 additions & 5 deletions src/cli/action_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace opts
} // namespace opts

static bool get_version_from_str(const std::string& str, int& major, int& minor);
static const vlChar* get_last_vtflib_error();

std::string ActionConvert::get_help() const {
return "Convert a generic image file to VTF";
Expand Down Expand Up @@ -287,6 +288,11 @@ bool ActionConvert::process_file(
m_width = opts.get<int>(opts::width);
m_height = opts.get<int>(opts::height);

if (!std::filesystem::exists(srcFile)) {
std::cerr << "Could not open " << srcFile << ": file does not exist\n";
return false;
}

bool isvtf = srcFile.filename().extension() == ".vtf";

// If an out file name is not provided, we need to build our own
Expand Down Expand Up @@ -367,7 +373,7 @@ bool ActionConvert::process_file(

// Generate thumbnail
if (thumbnail && !vtfFile->GenerateThumbnail(srgb)) {
std::cerr << fmt::format("Could not generate thumbnail: {}\n", vlGetLastError());
std::cerr << fmt::format("Could not generate thumbnail: {}\n", get_last_vtflib_error());
return false;
}

Expand All @@ -379,13 +385,13 @@ bool ActionConvert::process_file(

// Convert to desired image format
if (vtfFile->GetFormat() != format && !vtfFile->ConvertInPlace(format)) {
std::cerr << fmt::format("Could not convert image data to {}\n", formatStr);
std::cerr << fmt::format("Could not convert image data to {}: {}\n", formatStr, get_last_vtflib_error());
return false;
}

// Save to disk finally
if (!vtfFile->Save(outFile.string().c_str())) {
std::cerr << fmt::format("Could not save file {}: {}\n", outFile.string(), vlGetLastError());
std::cerr << fmt::format("Could not save file {}: {}\n", outFile.string(), get_last_vtflib_error());
return false;
}

Expand Down Expand Up @@ -570,7 +576,8 @@ bool ActionConvert::add_image_data_raw(

if (!CVTFFile::Convert((vlByte*)data, dest, w, h, dataFormat, format)) {
std::cerr << fmt::format(
"Could not convert from {} to {}!\n", NAMEOF_ENUM(dataFormat), NAMEOF_ENUM(format));
"Could not convert from {} to {}: {}\n", NAMEOF_ENUM(dataFormat), NAMEOF_ENUM(format),
get_last_vtflib_error());
free(dest);
return false;
}
Expand All @@ -583,7 +590,7 @@ bool ActionConvert::add_image_data_raw(
// This is done here because we don't actually know w/h until now
if (create) {
if (!file->Init(w, h, 1, 1, 1, format, vlTrue, m_mips)) {
std::cerr << "Could not create VTF.\n";
std::cerr << "Could not create VTF: " << get_last_vtflib_error() << "\n";
free(dest);
return false;
}
Expand All @@ -602,3 +609,14 @@ static bool get_version_from_str(const std::string& str, int& major, int& minor)
auto minorVer = str.substr(pos + 1);
return util::strtoint(majorVer, major) && util::strtoint(minorVer, minor);
}

// Get the last error which occurred in VTFLib
static const vlChar* get_last_vtflib_error() {
auto error = vlGetLastError();
if (error) {
// +7 so we do not print `Error:\n`, which destroys the formatting
return error + 7;
}

return "Unknown error";
}
Loading