LCOV - code coverage report
Current view: top level - src/libs/tools/include - toolexcept.hpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 47 171 27.5 %
Date: 2019-09-12 12:28:41 Functions: 22 111 19.8 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Implementation of all exceptions elektratools library might throw
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #ifndef TOOLS_EXCEPTION_HPP
      11             : #define TOOLS_EXCEPTION_HPP
      12             : 
      13             : #include <memory>
      14             : #include <stdexcept>
      15             : 
      16             : #include <kdbio.hpp>
      17             : 
      18             : /* SWIG cpp preprocessor is not aware of the override statement */
      19             : #ifndef SWIG_WITHOUT_OVERRIDE
      20             : #define ELEKTRA_OVERRIDE override
      21             : #else
      22             : #define ELEKTRA_OVERRIDE
      23             : #endif
      24             : 
      25             : namespace kdb
      26             : {
      27             : 
      28             : /**
      29             :  * @brief This namespace is for the libtool library.
      30             :  *
      31             :  * @note You have to link against libelektratools if you want to
      32             :  * use functionality from it. Contrary to classes in namespace kdb it
      33             :  * is not header-only.
      34             :  *
      35             :  * @see Backend for an entry point
      36             :  */
      37             : namespace tools
      38             : {
      39             : 
      40             : /**
      41             :  * @brief All exceptions from the elektratools library are derived from
      42             :  * this exception
      43             :  */
      44        1269 : struct ToolException : public std::runtime_error
      45             : {
      46             :         ToolException ()
      47        1261 :         : runtime_error (
      48             :                   "When you read this, that means there was something wrong with Elektra Tools.\n"
      49        1261 :                   "Seems like a wrong exception was thrown."){};
      50           4 :         explicit ToolException (std::string message) : runtime_error (message){};
      51             : };
      52             : 
      53           3 : struct ParseException : public ToolException
      54             : {
      55         117 :         explicit ParseException (std::string str) : m_str (std::move (str))
      56             :         {
      57          39 :         }
      58             : 
      59          40 :         virtual ~ParseException () throw ()
      60         117 :         {
      61          40 :         }
      62             : 
      63           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
      64             :         {
      65           0 :                 return m_str.c_str ();
      66             :         }
      67             : 
      68             :         std::string m_str;
      69             : };
      70             : 
      71        4446 : struct PluginCheckException : public ToolException
      72             : {
      73           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
      74             :         {
      75             :                 return "When you read this, that means there was something wrong with the plugin.\n"
      76           0 :                        "Seems like a check could not specify the error any further";
      77             :         }
      78             : };
      79             : 
      80         448 : struct BackendCheckException : public ToolException
      81             : {
      82           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
      83             :         {
      84             :                 return "When you read this, that means there was something wrong with the backend.\n"
      85           0 :                        "Seems like a check could not specify the error any further";
      86             :         }
      87             : };
      88             : 
      89           0 : struct FileNotValidException : public BackendCheckException
      90             : {
      91           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
      92             :         {
      93             :                 return "The path you entered is invalid.\n"
      94             :                        "Try to add another path instead.\n"
      95             :                        "\n"
      96             :                        "Filenames are typically (depending on\n"
      97             :                        "resolver) not allowed to contain '..'.\n"
      98             :                        "\n"
      99             :                        "For more information see:\n"
     100           0 :                        " kdb info <your resolver>";
     101             :         }
     102             : };
     103             : 
     104           0 : struct MountpointInvalidException : public BackendCheckException
     105             : {
     106           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     107             :         {
     108             :                 return "Given mountpoint is not a valid keyname, will abort\n"
     109           0 :                        "Examples: system/hosts or user/sw/app";
     110             :         }
     111             : };
     112             : 
     113           0 : struct MountpointAlreadyInUseException : public BackendCheckException
     114             : {
     115         336 :         explicit MountpointAlreadyInUseException (std::string str) : m_str (std::move (str))
     116             :         {
     117         112 :         }
     118             : 
     119         112 :         virtual ~MountpointAlreadyInUseException () throw ()
     120         336 :         {
     121         112 :         }
     122             : 
     123           4 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     124             :         {
     125           8 :                 return m_str.c_str ();
     126             :         }
     127             : 
     128             :         std::string m_str;
     129             : };
     130             : 
     131           0 : struct NoSuchBackend : public BackendCheckException
     132             : {
     133           0 :         explicit NoSuchBackend (std::string const & message) : m_str (message)
     134             :         {
     135           0 :         }
     136             : 
     137           0 :         virtual ~NoSuchBackend () throw ()
     138           0 :         {
     139           0 :         }
     140             : 
     141           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     142             :         {
     143           0 :                 return m_str.c_str ();
     144             :         }
     145             : 
     146             : private:
     147             :         std::string m_str;
     148             : };
     149             : 
     150           0 : struct PluginAlreadyInserted : public PluginCheckException
     151             : {
     152           0 :         explicit PluginAlreadyInserted (std::string name)
     153           0 :         : m_str ("It is not allowed to insert the same plugin (" + name +
     154             :                  ") again!\n"
     155           0 :                  "Try to add other plugins or other refnames (part after #) instead.")
     156             :         {
     157           0 :         }
     158             : 
     159           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     160             :         {
     161           0 :                 return m_str.c_str ();
     162             :         }
     163             : 
     164             :         std::string m_str;
     165             : };
     166             : 
     167           0 : struct PluginConfigInvalid : public PluginCheckException
     168             : {
     169           8 :         explicit PluginConfigInvalid (Key key) : m_key (key), m_str ()
     170             :         {
     171           2 :         }
     172             : 
     173           0 :         explicit PluginConfigInvalid (std::string const & message) : m_str (message)
     174             :         {
     175           0 :         }
     176             : 
     177           2 :         virtual ~PluginConfigInvalid () throw ()
     178           8 :         {
     179           2 :         }
     180             : 
     181           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     182             :         {
     183           0 :                 if (m_str.empty ())
     184             :                 {
     185           0 :                         std::stringstream ss;
     186           0 :                         ss << "The provided plugin configuration is not valid!\n";
     187           0 :                         ss << "Errors/Warnings during the check were:\n";
     188           0 :                         printError (ss, m_key, true, true);
     189           0 :                         printWarnings (ss, m_key, true, true);
     190           0 :                         m_str = ss.str ();
     191             :                 }
     192           0 :                 return m_str.c_str ();
     193             :         }
     194             : 
     195             : private:
     196             :         Key m_key;
     197             :         mutable std::string m_str;
     198             : };
     199             : 
     200         188 : struct BadPluginName : public PluginCheckException
     201             : {
     202          60 :         explicit BadPluginName (std::string name)
     203         180 :         : m_str ("You entered a bad name (" + name +
     204             :                  ") for a plugin!\n"
     205             :                  "A valid name of a plugin is either\n"
     206             :                  "modulename or modulename#refname\n"
     207             :                  "where both modulename and refname must start with a-z\n"
     208         180 :                  "and then a-z, 0-9 and underscore (_) only")
     209             :         {
     210          60 :         }
     211             : 
     212           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     213             :         {
     214           0 :                 return m_str.c_str ();
     215             :         }
     216             : 
     217             :         std::string m_str;
     218             : };
     219             : 
     220           0 : struct TooManyPlugins : public PluginCheckException
     221             : {
     222           3 :         explicit TooManyPlugins (std::string str) : m_str (std::move (str))
     223             :         {
     224           1 :         }
     225             : 
     226           1 :         virtual ~TooManyPlugins () throw ()
     227           3 :         {
     228           1 :         }
     229             : 
     230           1 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     231             :         {
     232           2 :                 return m_str.c_str ();
     233             :         }
     234             : 
     235             :         std::string m_str;
     236             : };
     237             : 
     238           0 : struct OrderingViolation : public PluginCheckException
     239             : {
     240           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     241             :         {
     242             :                 return "Ordering Violation!\n"
     243             :                        "You tried to add a plugin which requests another plugin to be positioned first.\n"
     244           0 :                        "Please position the other plugin first and try again.";
     245             :         }
     246             : };
     247             : 
     248           0 : struct CyclicOrderingViolation : public OrderingViolation
     249             : {
     250           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     251             :         {
     252             :                 return "Ordering Violation!\n"
     253             :                        "Could not order plugins by their dependency because of cycle.\n"
     254           0 :                        "Either fix plugins or try with other plugins.";
     255             :         }
     256             : };
     257             : 
     258             : 
     259           0 : struct ConflictViolation : public PluginCheckException
     260             : {
     261           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     262             :         {
     263             :                 return "Conflict Violation!\n"
     264             :                        "You tried to add a plugin which conflicts with another.\n"
     265           0 :                        "Please don't add a plugin which conflicts.";
     266             :         }
     267             : };
     268             : 
     269             : 
     270           4 : struct NoPlugin : public PluginCheckException
     271             : {
     272          36 :         explicit NoPlugin (Key key) : m_key (key), m_str ()
     273             :         {
     274           9 :         }
     275             : 
     276           0 :         explicit NoPlugin (std::string const & message) : m_str (message)
     277             :         {
     278           0 :         }
     279             : 
     280          10 :         virtual ~NoPlugin () throw ()
     281          36 :         {
     282          10 :         }
     283             : 
     284           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     285             :         {
     286           0 :                 if (m_str.empty ())
     287             :                 {
     288             :                         // note that the code will be re-evaluated
     289             :                         // if it prints nothing, but an expensive
     290             :                         // function not printing anything seems
     291             :                         // to be unlikely.
     292             :                         //
     293             :                         // note that printError/printWarning will be
     294             :                         // used either from namespace kdb or global
     295             :                         // namespace.
     296           0 :                         std::stringstream ss;
     297           0 :                         ss << "Was not able to load such a plugin!\n\n";
     298           0 :                         ss << "Maybe you misspelled it, there is no such plugin or the loader has problems.\n";
     299           0 :                         ss << "You might want to try to set LD_LIBRARY_PATH, use kdb-full or kdb-static.\n";
     300           0 :                         ss << "Errors/Warnings during loading were:\n";
     301           0 :                         printError (ss, m_key, true, true);
     302           0 :                         printWarnings (ss, m_key, true, true);
     303           0 :                         m_str = ss.str ();
     304             :                 }
     305           0 :                 return m_str.c_str ();
     306             :         }
     307             : 
     308             : private:
     309             :         Key m_key;
     310             :         mutable std::string m_str;
     311             : };
     312             : 
     313           0 : struct ReferenceNotFound : public PluginCheckException
     314             : {
     315           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     316             :         {
     317             :                 return "Could not find a reference!\n"
     318             :                        "Seems you forgot to create the reference before using it.\n"
     319           0 :                        "Use #modulename#label# before you #ref to it.";
     320             :         }
     321             : };
     322             : 
     323             : struct MissingNeeded : public PluginCheckException
     324             : {
     325             :         std::string msg;
     326           0 :         explicit MissingNeeded (std::string need) : msg ("The plugin " + need + " is needed by this plugin but it is not provided.")
     327             :         {
     328           0 :         }
     329           0 :         ~MissingNeeded () throw ()
     330           0 :         {
     331           0 :         }
     332           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     333             :         {
     334           0 :                 return msg.c_str ();
     335             :         }
     336             : };
     337             : 
     338           0 : struct MissingSymbol : public PluginCheckException
     339             : {
     340             :         std::string msg;
     341        3114 :         explicit MissingSymbol (std::string symbol) : msg ("The necessary symbol \"" + symbol + "\" is missing in that plugin!")
     342             :         {
     343        1038 :         }
     344        1038 :         ~MissingSymbol () throw ()
     345        3114 :         {
     346        1038 :         }
     347           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     348             :         {
     349           0 :                 return msg.c_str ();
     350             :         }
     351             : };
     352             : 
     353           0 : struct WrongStatus : public PluginCheckException
     354             : {
     355             :         std::string msg;
     356           0 :         explicit WrongStatus (std::string status) : msg ("The status \"" + status + "\" is neither a valid enum value nor an integer!")
     357             :         {
     358           0 :         }
     359           0 :         ~WrongStatus () throw ()
     360           0 :         {
     361           0 :         }
     362           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     363             :         {
     364           0 :                 return msg.c_str ();
     365             :         }
     366             : };
     367             : 
     368             : 
     369           0 : struct SymbolMismatch : public PluginCheckException
     370             : {
     371             :         std::string msg;
     372           0 :         explicit SymbolMismatch (std::string symbol) : msg ("The symbol \"" + symbol + "\" does not match with other exported information!")
     373             :         {
     374           0 :         }
     375           0 :         ~SymbolMismatch () throw ()
     376           0 :         {
     377           0 :         }
     378           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     379             :         {
     380           0 :                 return msg.c_str ();
     381             :         }
     382             : };
     383             : 
     384             : struct NoGlobalPlugin : public PluginCheckException
     385             : {
     386             :         std::string msg;
     387           0 :         explicit NoGlobalPlugin (std::string plugin) : msg ("The plugin \"" + plugin + "\" is not suitable to be mounted as global plugin!")
     388             :         {
     389           0 :         }
     390           0 :         ~NoGlobalPlugin () throw ()
     391           0 :         {
     392           0 :         }
     393           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     394             :         {
     395           0 :                 return msg.c_str ();
     396             :         }
     397             : };
     398             : 
     399             : 
     400           0 : struct SymbolDuplicate : public PluginCheckException
     401             : {
     402             :         std::string msg;
     403           0 :         explicit SymbolDuplicate (std::string symbol) : msg ("The symbol \"" + symbol + "\" has the same value as another symbol!")
     404             :         {
     405           0 :         }
     406           0 :         ~SymbolDuplicate () throw ()
     407           0 :         {
     408           0 :         }
     409           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     410             :         {
     411           0 :                 return msg.c_str ();
     412             :         }
     413             : };
     414             : 
     415           0 : struct StoragePlugin : public PluginCheckException
     416             : {
     417           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     418             :         {
     419           0 :                 return "There need to be exactly one storage plugin!";
     420             :         }
     421             : };
     422             : 
     423             : 
     424           0 : struct ResolverPlugin : public PluginCheckException
     425             : {
     426           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     427             :         {
     428           0 :                 return "There need to be exactly one resolver plugin!";
     429             :         }
     430             : };
     431             : 
     432           0 : struct PluginNoContract : public PluginCheckException
     433             : {
     434           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     435             :         {
     436             :                 return "No contract found for that plugin!\n"
     437           0 :                        "Make sure you export kdbGet correctly!";
     438             :         }
     439             : };
     440             : 
     441           0 : struct PluginNoInfo : public PluginCheckException
     442             : {
     443           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     444             :         {
     445           0 :                 return "No info found for that plugin within contract!";
     446             :         }
     447             : };
     448             : 
     449           0 : struct VersionInfoMismatch : public PluginCheckException
     450             : {
     451           0 :         virtual const char * what () const throw () ELEKTRA_OVERRIDE
     452             :         {
     453           0 :                 return "Version info does not match with library!";
     454             :         }
     455             : };
     456             : } // namespace tools
     457             : } // namespace kdb
     458             : 
     459             : #endif

Generated by: LCOV version 1.13