Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
libtool: extend and refactor error/warning API, add some basic tests,
Browse files Browse the repository at this point in the history
add iterator functionality for Warnings to Error class
  • Loading branch information
flo91 committed Jan 16, 2022
1 parent 30291de commit 17480a0
Show file tree
Hide file tree
Showing 16 changed files with 711 additions and 300 deletions.
21 changes: 21 additions & 0 deletions src/libs/elektra/symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ libelektra_1.0 {
ELEKTRA_WARNING_VALIDATION_SEMANTIC;
ELEKTRA_WARNING_VALIDATION_SYNTACTIC;

# export also names of errors and warnings
ELEKTRA_ERROR_CONFLICTING_STATE_NAME;
ELEKTRA_ERROR_INSTALLATION_NAME;
ELEKTRA_ERROR_INTERFACE_NAME;
ELEKTRA_ERROR_INTERNAL_NAME;
ELEKTRA_ERROR_OUT_OF_MEMORY_NAME;
ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME;
ELEKTRA_ERROR_RESOURCE_NAME;
ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME;
ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME;

ELEKTRA_WARNING_CONFLICTING_STATE_NAME;
ELEKTRA_WARNING_INSTALLATION_NAME;
ELEKTRA_WARNING_INTERFACE_NAME;
ELEKTRA_WARNING_INTERNAL_NAME;
ELEKTRA_WARNING_OUT_OF_MEMORY_NAME;
ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME;
ELEKTRA_WARNING_RESOURCE_NAME;
ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME;
ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME;

# kdb.h
elektraGOptsContract;
elektraGOptsContractFromStrings;
Expand Down
10 changes: 0 additions & 10 deletions src/libs/highlevel/symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,6 @@ libelektra_0.9 {
elektraFindReference;
elektraFindReferenceArrayElement;
elektraHelpKey;

# elektra/error.h;
# added for C++ error api;
elektraErrorCode;
elektraErrorFromKey;
elektraErrorAddWarning;
elektraErrorCreate;
elektraErrorPureWarning;
elektraErrorAddWarning;

};

libelektraprivate_1.0 {
Expand Down
56 changes: 56 additions & 0 deletions src/libs/tools/include/errors/baseNotification.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef ELEKTRA_BASENOTIFICATION_HPP
#define ELEKTRA_BASENOTIFICATION_HPP

#include <string>
#include <key.hpp>
#include <utility>
#include <kdberrors.h> // for kdb-types, code and description constants

namespace kdb
{
namespace tools
{
namespace errors
{
/* common abstract class for warnings and errors */
/* Because warning and errors share the same data members, a method can accept a ErrBase argument and the caller
* can create an Error or a Warning based on the provided object. */
class BaseNotification
{
public:
/* setters */
void setData (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line);

/* get references (for getting and setting member values) */
std::string & reason();
std::string & module();
std::string & file();
kdb::long_t & line();

/* fixed values per Class, taken from C-makro definitions in /src/include/kdberrors.h */
virtual std::string code() const = 0;
virtual std::string description() const = 0;

/* string representation */
friend std::ostream& operator<< (std::ostream& outputStream, const BaseNotification& eb);
/* compare */
friend bool operator== (const BaseNotification& lhs, const BaseNotification& rhs);

protected:
BaseNotification () = default;
BaseNotification (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line);

/* Can be overwritten by subclasses to change the text representation */
std::ostream& toString (std::ostream& outputStream) const;

private:
std::string m_reason;
std::string m_module;
std::string m_file;
kdb::long_t m_line = 0;
};

} // namespace errors
} // namespace tools
} // namespace kdb
#endif // ELEKTRA_BASENOTIFICATION_HPP
50 changes: 0 additions & 50 deletions src/libs/tools/include/errors/errbase.hpp

This file was deleted.

48 changes: 18 additions & 30 deletions src/libs/tools/include/errors/error.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#ifndef ELEKTRA_ERROR_HPP
#define ELEKTRA_ERROR_HPP

#include <iterator>
#include <cstddef>
#include <errors/warning.hpp>
#include <vector>
#include "errors/warning.hpp"

namespace kdb
{
Expand All @@ -13,41 +12,30 @@ namespace tools
namespace errors
{

class Error : public ErrBase
class Error : public BaseNotification
{
public:
Error () : ErrBase{}
{
}
Error (const std::string & code, const std::string & description, const std::string & module, const std::string & file,
kdb::long_t line)
: ErrBase{ code, description, module, file, line }
{
}
explicit Error (kdb::Key & errorKey) : ErrBase{ errorKey }
{
}
explicit Error (ElektraError *err) : ErrBase { err } {}
~Error () override;

void addWarning (const std::string& code, const std::string& description, const std::string& module, const std::string& file,
kdb::long_t line);
void addWarning (Warning & warning);
void addWarning (ElektraError * warning);

/* inherit constructors */
using BaseNotification::BaseNotification;

/* An Error can contain 0 to n warnings */
void addWarning (Warning *warning);

/* getters */
kdb::long_t warningCount ();
Warning getWarning(kdb::long_t index);
Warning copyWarning(kdb::long_t index);


/* special types of errors and warnings */
void setSemanticValidationError (const std::string & description, const std::string & module, const std::string & file,
kdb::long_t line);
void setSyntacticValidationError (const std::string & description, const std::string & module, const std::string & file,
kdb::long_t line);
/* iterator functionality */
std::vector<Warning*>::iterator begin() { return warnings.begin(); }
std::vector<Warning*>::iterator end() { return warnings.end(); }
std::vector<Warning*>::const_iterator begin() const { return warnings.begin(); }
std::vector<Warning*>::const_iterator end() const { return warnings.begin(); }
std::vector<Warning*>::const_iterator cbegin() const { return warnings.cbegin(); }
std::vector<Warning*>::const_iterator cend() const { return warnings.cend(); }


private:
std::vector<Warning*> warnings;
};
} // namespace errors
} // namespace tools
Expand Down
111 changes: 111 additions & 0 deletions src/libs/tools/include/errors/errorTypes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@

#ifndef ELEKTRA_ERRORTYPES_HPP
#define ELEKTRA_ERRORTYPES_HPP

#include "error.hpp"

namespace kdb
{
namespace tools
{
namespace errors
{

/* This file contains all concrete classes for the different Errors, based on the constants defined in /src/include/kdberrors.h */

class ResourceError : public Error
{
public:
ResourceError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

class OutOfMemoryError : public Error
{
public:
OutOfMemoryError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

class InstallationError : public Error
{
public:
InstallationError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};


class InternalError: public Error
{
public:
InternalError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

class InterfaceError : public Error
{
public:
InterfaceError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

class PluginMisbehaviorError : public Error
{
public:
PluginMisbehaviorError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

class ConflictingStateError : public Error
{
public:
ConflictingStateError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

class ValidationSyntacticError : public Error
{
public:
ValidationSyntacticError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

class ValidationSemanticError : public Error
{
public:
ValidationSemanticError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line)
: Error { reason, module, file, line} {}

std::string code() const override;
std::string description() const override;
};

} // namespace errors
} // namespace tools
} // namespace kdb

#endif // ELEKTRA_ERRORTYPES_HPP
25 changes: 6 additions & 19 deletions src/libs/tools/include/errors/warning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,22 @@
#ifndef ELEKTRA_WARNING_HPP
#define ELEKTRA_WARNING_HPP

#include "errors/errbase.hpp"
#include "baseNotification.hpp"

namespace kdb
{


namespace tools
{

namespace errors
{

class Warning : public ErrBase
/* The warning class currently has no extra parts compared to the BaseNotification class,
* it's used for distiguishing Warnings and Errors by their type and allow to only add Warnings to Errors. */
class Warning : public BaseNotification
{
public:
Warning (const std::string & code, const std::string & description, const std::string & module, const std::string & file,
kdb::long_t line)
: ErrBase { code, description, module, file, line }
{
}
Warning () = default;
explicit Warning (ElektraError *err) : ErrBase { err } {}
~Warning () override = default;
void setSemanticValidationWarning (const std::string & description, const std::string & module, const std::string & file,
kdb::long_t line);
void setSyntacticValidationWarning (const std::string & description, const std::string & module, const std::string & file,
kdb::long_t line);
private:
friend class Error; // Error has access to ElektraError *err (internal errors) of warnings
/* inherit constructors */
using BaseNotification::BaseNotification;
};

} // namespace errors
Expand Down
Loading

0 comments on commit 17480a0

Please sign in to comment.