#include <PMError.h>
Public Types | |
enum | ErrClass { C_Error = 0, C_InstSrcError = 10*_valrange, C_MediaError = 11*_valrange, C_InstTargetError = 12*_valrange, C_YouError = 14*_valrange, C_ModulePkgError = 15*_valrange } |
enum | Error { E_TBD = C_Error+1 } |
Public Member Functions | |
PMError (const unsigned e=E_ok) | |
PMError (const unsigned e, const std::string &details) | |
operator unsigned () const | |
std::string | errstr () const |
ErrClass | errClass () const |
void | addDetails (const std::string &) |
void | setDetails (const std::string &details) |
std::string | details () const |
std::string | asString () const |
Static Public Member Functions | |
std::string | errstr (const unsigned e) |
ErrClass | errClass (const unsigned e) |
Public Attributes | |
unsigned | _errval |
std::string | _errdetails |
Static Public Attributes | |
const unsigned | E_ok = 0 |
const unsigned | E_error = 1 |
const std::string | OKstring |
const std::string | ERRORstring |
Static Private Member Functions | |
std::string | defaulterrstr (const std::string &cl, const std::string &txt) |
Static Private Attributes | |
const unsigned | _valrange = 0x400 |
const unsigned | _valmask = _valrange-1 |
const unsigned | _repmask = ~_valmask |
const std::string | errstrPrefix |
Friends | |
std::ostream & | operator<< (std::ostream &str, const PMError &obj) |
Quite simple approach. Error values are unsigned. E_ok (== 0) means no error.
Conversion of error values to string, or writing to ostreams, per default prints the error number as ERROR(10242) ("ERROR" is translatable). For E_ok OK (also translatable) is printed.
Primitive support for error classes: Assume a class InstSrcError
providing nothing but an enum of common InstSrc error values. Returning those error values through PMError
, errors originating from this class will be reported as ERROR(InstSrc:short description of error), if an error text is provided, otherwise ERROR(InstSrc:10242).
An optional details string may be provided to give further explanation.
To accomplish this extend enum ErrClass
and define a new first error value for InstSrcError in multiples of _valrange
(currently 1024). Extend the switches in PMError::errClass
and PMError::errstr
by cases for the new enum value. By this _valrange
error numbers starting at C_InstSrcError
are assigned to class InstSrcError
.
enum ErrClass { ... C_InstSrcError = 10*_valrange };Now define
class InstSrcError
.
errclass
defines the string to be used as error class name.errtext
must return the text describing the error, or the numerical value as string. Error values will be printed as ERROR(errclass:errtext(err)).
#include <y2pm/PMError.h>
/////////////////////////////////////////////////////////////////// // // CLASS NAME : InstSrcError // class InstSrcError {
private:
friend class PMError;
static const std::string errclass;
static std::string errtext( const unsigned e );
public:
enum Error { E_ok = PMError::E_ok, // E_ok is always 0 E_error = PMError::C_InstSrcError, // C_InstSrcError first error value for InstSrcError E_no_media, ... }; };
///////////////////////////////////////////////////////////////////Implementation will look like this:#include <y2pm/InstSrcError.h>
using namespace std;
/////////////////////////////////////////////////////////////////// #ifndef N_ # define N_(STR) STR #endif ///////////////////////////////////////////////////////////////////
// define the error class name const std::string InstSrcError::errclass( "InstSrc" );
/////////////////////////////////////////////////////////////////// // // // METHOD NAME : InstSrcError::errtext // METHOD TYPE : std::string // // DESCRIPTION : Return textual description or numerical value // as string; // std::string InstSrcError::errtext( const unsigned e ) { switch ( (Error)e ) {
case E_ok: return PMError::OKstring; case E_error: return N_("error"); case E_no_media: return N_("no media"); ...
/////////////////////////////////////////////////////////////////// // no default: let compiler warn '... not handled in switch' /////////////////////////////////////////////////////////////////// };
return stringutil::numstring( e ); }
Member Enumeration Documentation
|
|
|
|
|
Default Constructor. Assign errorvalue, defaults to E_ok. |
|
Constructor. Assign error value and details. |
|
Add a string to the details string. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Conversion to unsigned error value. |
|
Assign a details string |
|
Stream output asString. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|