Skip to content

Commit

Permalink
vars: Throw ReadOnlyVariableError when setting read-only var
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-goode authored and j-mracek committed Aug 31, 2023
1 parent d6a0144 commit 54cdeab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions bindings/libdnf5/conf.i
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ wrap_unique_ptr(StringUniquePtr, std::string);
%ignore ConfigParserOptionNotFoundError;
%include "libdnf5/conf/config_parser.hpp"

%ignore libdnf5::ReadOnlyVariableError;
%include "libdnf5/conf/vars.hpp"

%include "libdnf5/conf/config.hpp"
Expand Down
10 changes: 10 additions & 0 deletions include/libdnf5/conf/vars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5 {

// Thrown when attempting to set a read-only variable
class ReadOnlyVariableError : public Error {
using Error::Error;

const char * get_domain_name() const noexcept override { return "libdnf5"; }
const char * get_name() const noexcept override { return "ReadOnlyVariableError"; }
};

/// @class Vars
///
/// @brief Class for reading and substituting DNF vars (arch, releasever, etc.).
Expand Down Expand Up @@ -68,6 +76,7 @@ struct Vars {
/// @param name Name of the variable
/// @param value Value to be stored in variable
/// @param prio Source/Priority of the value
/// @throw ReadOnlyVariableError if the variable is read-only
void set(const std::string & name, const std::string & value, Priority prio = Priority::RUNTIME);

/// @brief Checks whether a variable is read-only
Expand Down Expand Up @@ -132,6 +141,7 @@ struct Vars {
/// @param name Name of the variable
/// @param get_value Function that returns the (optional) value for the variable
/// @param prio Source/Priority of the value
/// @throw ReadOnlyVariableError if the variable is read-only
void set_lazy(
const std::string & name,
const std::function<const std::unique_ptr<const std::string>()> & get_value,
Expand Down
2 changes: 1 addition & 1 deletion libdnf5/conf/vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ bool Vars::is_read_only(const std::string & name) const {

void Vars::set(const std::string & name, const std::string & value, Priority prio) {
if (is_read_only(name)) {
throw RuntimeError(M_("Variable \"{}\" is read-only"), name);
throw ReadOnlyVariableError(M_("Variable \"{}\" is read-only"), name);
}

// set_unsafe sets the variable without checking whether it's read-only
Expand Down

0 comments on commit 54cdeab

Please sign in to comment.