Skip to content

Commit

Permalink
Application: Improve thrown exceptions
Browse files Browse the repository at this point in the history
Exceptions should be more informative now. Invalid desktop files now
fall under invalid_error and are handled gracefully instead of aborting
j4-dmenu-desktop.

related to #176
  • Loading branch information
meator committed Aug 11, 2024
1 parent 83e1770 commit 06a8df3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
33 changes: 23 additions & 10 deletions src/AppManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <stdlib.h>
#include <system_error>

using std::in_place_t;

Expand Down Expand Up @@ -126,7 +127,7 @@ AppManager::AppManager(Desktop_file_list files, stringlist_t desktopenvs,
"taken! Not registering.",
newly_added.app->generic_name);
}
} catch (disabled_error &e) {
} catch (const disabled_error &e) {
SPDLOG_DEBUG("AppManager: Desktop file is disabled: {}",
e.what());
// Add an empty Application that only occupies desktop ID + rank
Expand All @@ -136,9 +137,13 @@ AppManager::AppManager(Desktop_file_list files, stringlist_t desktopenvs,
SPDLOG_DEBUG(
"AppManager: Collision detected, skipping!");
continue;
} catch (invalid_error &e) {
} catch (const std::system_error &e) {
SPDLOG_WARN("Couldn't open file '{}': {}", filename, e.what());
continue;
} catch (const invalid_error &e) {
SPDLOG_WARN("Desktop file '{}' is invalid: {}", filename,
e.what());
continue;
}
}
}
Expand Down Expand Up @@ -207,12 +212,16 @@ void AppManager::add(const string &filename, const string &base_path,
try {
new_app.emplace(filename.c_str(), this->liner, this->suffixes,
this->desktopenvs);
} catch (disabled_error &e) {
} catch (const disabled_error &e) {
SPDLOG_DEBUG("AppManager: App is disabled: {}", e.what());
is_disabled = true;
} catch (invalid_error &e) {
SPDLOG_WARN("Couldn't open newly added file '{}': {}", filename,
e.what());
} catch (const std::system_error &e) {
SPDLOG_WARN("Couldn't open newly added desktop file '{}': {}",
filename, e.what());
return;
} catch (const invalid_error &e) {
SPDLOG_WARN("Newly added desktop file '{}' is invalid: {}",
filename, e.what());
return;
}

Expand All @@ -239,13 +248,17 @@ void AppManager::add(const string &filename, const string &base_path,
filename.c_str(), this->liner,
this->suffixes, this->desktopenvs)
.first->second;
} catch (disabled_error &e) {
} catch (const disabled_error &e) {
SPDLOG_DEBUG("AppManager: App is disabled: {}", e.what());
this->applications.try_emplace(ID, rank);
return;
} catch (invalid_error &e) {
SPDLOG_WARN("Couldn't open newly added file '{}': {}", filename,
e.what());
} catch (const std::system_error &e) {
SPDLOG_WARN("Couldn't open newly added desktop file '{}': {}",
filename, e.what());
return;
} catch (const invalid_error &e) {
SPDLOG_WARN("Newly added desktop file '{}' is invalid: {}",
filename, e.what());
return;
}

Expand Down
27 changes: 19 additions & 8 deletions src/Application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdio.h>
#include <string_view>
#include <sys/types.h>
#include <system_error>
#include <utility>

#include "LineReader.hh"
Expand Down Expand Up @@ -53,9 +54,16 @@ Application::Application(const char *path, LineReader &liner,
ssize_t line_length;
std::unique_ptr<FILE, fclose_deleter> file(fopen(path, "r"));
if (!file)
throw invalid_error(strerror(errno));
throw std::system_error(errno, std::system_category());

// The choice of 'unsigned long' is arbitrary here. This variable isn't
// checked for integer overflow, but it is unlikely that a desktop file will
// have more than (unsigned long)-1 lines. This is used for diagnostics
// only, so it doesn't matter much.
unsigned long line_number = 0;

while ((line_length = liner.getline(file.get())) != -1) {
++line_number;
char *line = liner.get_lineptr();
line[--line_length] = 0; // Chop off \n

Expand All @@ -71,13 +79,17 @@ Application::Application(const char *path, LineReader &liner,
// Split that string in place
char *key = line, *value = strpbrk(line, " =");
if (!value || value == line)
throw std::runtime_error("Malformed file.");
throw invalid_error(
"Malformed file, invalid key=value pair (line " +
std::to_string(line_number) + ").");
// Cut spaces before equal sign
if (*value != '=') {
*value++ = '\0';
value = strchr(value, '=');
if (!value)
throw std::runtime_error("Malformed file.");
throw invalid_error(
"Malformed file, invalid key=value pair (line " +
std::to_string(line_number) + ").");
} else
*value = '\0';
value++;
Expand Down Expand Up @@ -126,17 +138,16 @@ Application::Application(const char *path, LineReader &liner,
}
} catch (const escape_error &e) {
SPDLOG_ERROR("{}: {}\n", location, e.what());
throw;
throw escape_error((std::string)e.what() + " (line " +
std::to_string(line_number) + ")");
}
} else if (!strcmp(line, "[Desktop Entry]"))
parse_key_values = true;
}
if (!parse_key_values)
throw invalid_error("Invalid desktop file! Desktop file doesn't "
"contain '[Desktop Entry]'.");
throw invalid_error("Desktop file doesn't contain '[Desktop Entry]'.");
if (this->name.empty())
throw invalid_error(
"Invalid desktop file! 'Name' key is missing or empty.");
throw invalid_error("'Name' key is missing or empty.");
}

char Application::convert(char escape) {
Expand Down
10 changes: 5 additions & 5 deletions src/Application.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ struct disabled_error final : public std::runtime_error
using std::runtime_error::runtime_error;
};

// Invalid escape sequences
struct escape_error final : public std::runtime_error
// Desktop file is invalid in some way
struct invalid_error : public std::runtime_error
{
using std::runtime_error::runtime_error;
};

// Error while opening a desktop file
struct invalid_error final : public std::runtime_error
// Invalid escape sequences
struct escape_error final : public invalid_error
{
using std::runtime_error::runtime_error;
using invalid_error::invalid_error;
};

class Application
Expand Down

0 comments on commit 06a8df3

Please sign in to comment.