Permalink
Browse files

highlevel: unexpose ElektraKDBError

  • Loading branch information...
kodebach committed Jan 4, 2019
1 parent a070cd7 commit a25be1b40ff94baa1bb97804595053d2cd8c44d4
@@ -11,14 +11,14 @@

#include <elektra/errorcodes.h>
#include <kdb.h>
#include <stdbool.h>

#ifdef __cplusplus
#define Key ckdb::Key
extern "C" {
#endif

typedef struct _ElektraError ElektraError;
typedef struct _ElektraKDBError ElektraKDBError;

typedef enum
{
@@ -38,17 +38,16 @@ typedef void (*ElektraErrorHandler) (ElektraError * error);
ElektraErrorCode elektraErrorCode (const ElektraError * error);
const char * elektraErrorDescription (const ElektraError * error);
ElektraErrorSeverity elektraErrorSeverity (const ElektraError * error);
ElektraKDBError * elektraErrorLowLevelError (const ElektraError * error);

int elektraKDBErrorCode (const ElektraKDBError * error);
const char * elektraKDBErrorDescription (const ElektraKDBError * error);
ElektraErrorSeverity elektraKDBErrorSeverity (const ElektraKDBError * error);
ElektraKDBErrorGroup elektraKDBErrorGroup (const ElektraKDBError * error);
ElektraKDBErrorModule elektraKDBErrorModule (const ElektraKDBError * error);
const char * elektraKDBErrorReason (const ElektraKDBError * error);
int elektraKDBErrorWarningCount (const ElektraKDBError * error);
const ElektraKDBError ** elektraKDBErrorWarnings (const ElektraKDBError * error);
Key * elektraKDBErrorKey (const ElektraKDBError * error);

int elektraKDBErrorCode (const ElektraError * error);
const char * elektraKDBErrorDescription (const ElektraError * error);
ElektraErrorSeverity elektraKDBErrorSeverity (const ElektraError * error);
ElektraKDBErrorGroup elektraKDBErrorGroup (const ElektraError * error);
ElektraKDBErrorModule elektraKDBErrorModule (const ElektraError * error);
const char * elektraKDBErrorReason (const ElektraError * error);
int elektraKDBErrorWarningCount (const ElektraError * error);
ElektraError * elektraKDBErrorGetWarning (const ElektraError * error, int index);
Key * elektraKDBErrorKey (const ElektraError * error);

void elektraErrorReset (ElektraError ** error);

@@ -622,7 +622,7 @@ struct _ElektraError
ElektraErrorCode code;
char * description;
ElektraErrorSeverity severity;
ElektraKDBError * lowLevelError;
struct _ElektraKDBError * lowLevelError;
};

struct _ElektraKDBError
@@ -634,7 +634,7 @@ struct _ElektraKDBError
ElektraKDBErrorModule module;
const char * reason;
int warningCount;
ElektraKDBError ** warnings;
struct _ElektraKDBError ** warnings;
Key * errorKey;
};

@@ -27,7 +27,7 @@ static void defaultFatalErrorHandler (ElektraError * error)
exit (code);
}

static ElektraKDBError * elektraKDBErrorFromKey (Key * key);
static struct _ElektraKDBError * elektraKDBErrorFromKey (Key * key);
static ElektraError * elektraErrorCreateFromKey (Key * key);

/**
@@ -219,8 +219,7 @@ void elektraSaveKey (Elektra * elektra, Key * key, ElektraError ** error)
return;
}

ElektraKDBError * kdbError = elektraErrorLowLevelError (kdbSetError);
if (elektraKDBErrorCode (kdbError) != 30) // ELEKTRA_ERROR_CONFLICT = 30
if (elektraKDBErrorCode (kdbSetError) != 30) // ELEKTRA_ERROR_CONFLICT = 30
{
*error = kdbSetError;
return;
@@ -282,7 +281,7 @@ static ElektraError * elektraErrorCreateFromKey (Key * key)
return error;
}

static ElektraKDBError * elektraKDBErrorFromKey (Key * key)
static struct _ElektraKDBError * elektraKDBErrorFromKey (Key * key)
{
if (key == NULL)
{
@@ -308,7 +307,7 @@ static ElektraKDBError * elektraKDBErrorFromKey (Key * key)
const char * reason = keyString (keyGetMeta (key, "error/reason"));
const char * description = keyString (keyGetMeta (key, "error/description"));

ElektraKDBError * const error = elektraCalloc (sizeof (struct _ElektraKDBError));
struct _ElektraKDBError * const error = elektraCalloc (sizeof (struct _ElektraKDBError));
error->code = code;
error->description = description;
error->severity = severity;
@@ -327,11 +326,11 @@ static ElektraKDBError * elektraKDBErrorFromKey (Key * key)
error->warningCount = warningCount;
if (warningCount > 0)
{
ElektraKDBError ** warnings = elektraCalloc (warningCount * sizeof (ElektraKDBError *));
struct _ElektraKDBError ** warnings = elektraCalloc (warningCount * sizeof (struct _ElektraKDBError *));

for (int i = 0; i < warningCount; ++i)
{
ElektraKDBError * const warning = elektraCalloc (sizeof (struct _ElektraKDBError));
struct _ElektraKDBError * const warning = elektraCalloc (sizeof (struct _ElektraKDBError));
warning->severity = ELEKTRA_ERROR_SEVERITY_WARNING;

char * name = elektraFormat ("warnings/#%02d/number", i);
@@ -73,84 +73,140 @@ ElektraErrorSeverity elektraErrorSeverity (const ElektraError * error)
}

/**
* @return the Key from which this error was extracted,
* or NULL if the error was created by the high-level API
* @return the error code of the attached low-level error,
* or -1 if no low-level error is attached
*/
ElektraKDBError * elektraErrorLowLevelError (const ElektraError * error)
int elektraKDBErrorCode (const ElektraError * error)
{
return error->lowLevelError;
}
if (error->lowLevelError == NULL)
{
return -1;
}

/**
* @return the error code of the given kdb error
*/
int elektraKDBErrorCode (const ElektraKDBError * error)
{
return error->code;
return error->lowLevelError->code;
}

/**
* @return the description for the given kdb error
* @return the description for the attached low-level error,
* or -1 if no low-level error is attached
*/
const char * elektraKDBErrorDescription (const ElektraKDBError * error)
const char * elektraKDBErrorDescription (const ElektraError * error)
{
return error->description;
if (error->lowLevelError == NULL)
{
return NULL;
}

return error->lowLevelError->description;
}

/**
* @return the severity of the given kdb error
* @return the severity of the attached low-level error,
* or #ELEKTRA_ERROR_SEVERITY_FATAL if no low-level error is attached
*/
ElektraErrorSeverity elektraKDBErrorSeverity (const ElektraKDBError * error)
ElektraErrorSeverity elektraKDBErrorSeverity (const ElektraError * error)
{
return error->severity;
if (error->lowLevelError == NULL)
{
return ELEKTRA_ERROR_SEVERITY_FATAL;
}

return error->lowLevelError->severity;
}

/**
* @return the group from which the given kdb error originated
* @return the group from which the attached low-level error originated,
* or NULL if no low-level error is attached
*/
ElektraKDBErrorGroup elektraKDBErrorGroup (const ElektraKDBError * error)
ElektraKDBErrorGroup elektraKDBErrorGroup (const ElektraError * error)
{
return error->group;
if (error->lowLevelError == NULL)
{
return NULL;
}

return error->lowLevelError->group;
}

/**
* @return the module from which the given kdb error originated
* @return the module from which the attached low-level error originated,
* or NULL if no low-level error is attached
*/
ElektraKDBErrorModule elektraKDBErrorModule (const ElektraKDBError * error)
ElektraKDBErrorModule elektraKDBErrorModule (const ElektraError * error)
{
return error->module;
if (error->lowLevelError == NULL)
{
return NULL;
}

return error->lowLevelError->module;
}

/**
* @return the reason for the given kdb error
* @return the reason for the attached low-level error,
* or NULL if no low-level error is attached
*/
const char * elektraKDBErrorReason (const ElektraKDBError * error)
const char * elektraKDBErrorReason (const ElektraError * error)
{
return error->reason;
if (error->lowLevelError == NULL)
{
return NULL;
}

return error->lowLevelError->reason;
}

/**
* @return the number of warnings associated with the given kdb error
* @return the number of warnings associated with the attached low-level error,
* or -1 if no low-level error is attached
*/
int elektraKDBErrorWarningCount (const ElektraKDBError * error)
int elektraKDBErrorWarningCount (const ElektraError * error)
{
return error->warningCount;
if (error->lowLevelError == NULL)
{
return -1;
}

return error->lowLevelError->warningCount;
}

/**
* @return the array of warnings associated with the given kdb error
* @return a newly allocated ElektraError representing the warning at the given index,
* or NULL if the index is out of range
*
* the returned error will always have the following properties:
* - error code is #ELEKTRA_ERROR_CODE_LOW_LEVEL
* - description is ""
* - severity is #ELEKTRA_ERROR_SEVERITY_WARNING
* - elektraErrorHasKDBError() returns true
* - the attached low-level error represents the warning in question
*
* NOTE: you have to free the memory allocated by this function using elektraFree()
*/
const ElektraKDBError ** elektraKDBErrorWarnings (const ElektraKDBError * error)
ElektraError * elektraKDBErrorGetWarning (const ElektraError * error, int index)
{
return (const ElektraKDBError **) error->warnings;
if (index < 0 || index > error->lowLevelError->warningCount)
{
return NULL;
}

ElektraError * warning = elektraErrorCreate (ELEKTRA_ERROR_CODE_LOW_LEVEL, "", ELEKTRA_ERROR_SEVERITY_WARNING);
warning->lowLevelError = error->lowLevelError->warnings[index];
return warning;
}

/**
* @return the Key from which the given kdb error was extracted
* @return the Key from which the given kdb error was extracted,
* or NULL if no low-level error is attached
*/
Key * elektraKDBErrorKey (const ElektraKDBError * error)
Key * elektraKDBErrorKey (const ElektraError * error)
{
return error->errorKey;
if (error->lowLevelError == NULL)
{
return NULL;
}

return error->lowLevelError->errorKey;
}

/**
@@ -171,7 +227,7 @@ void elektraErrorReset (ElektraError ** error)
elektraFree (actualError->description);
}

ElektraKDBError * kdbError = actualError->lowLevelError;
struct _ElektraKDBError * kdbError = actualError->lowLevelError;
if (kdbError != NULL)
{
if (kdbError->warnings != NULL)
@@ -23,51 +23,47 @@ static inline const char * severityString (ElektraError * error)
return severityString (elektraErrorSeverity (error));
}

static std::ostream & operator<< (std::ostream & os, ElektraKDBError * kdbError)
{
if (kdbError != nullptr)
{
os << "\tKDB";
switch (elektraKDBErrorSeverity (kdbError))
{
case ELEKTRA_ERROR_SEVERITY_ERROR:
os << "ERROR";
break;
case ELEKTRA_ERROR_SEVERITY_WARNING:
os << "WARNING";
break;
default:
case ELEKTRA_ERROR_SEVERITY_FATAL:
os << "FATAL";
break;
}

os << " " << elektraKDBErrorCode (kdbError) << " [" << elektraKDBErrorGroup (kdbError) << "/"
<< elektraKDBErrorModule (kdbError) << "]"
<< ": " << elektraKDBErrorDescription (kdbError) << std::endl;
os << "\t\tReason: " << elektraKDBErrorReason (kdbError) << std::endl;

int warningCount = elektraKDBErrorWarningCount (kdbError);
os << "\t\t" << warningCount << " Warnings:" << std::endl;
const ElektraKDBError ** warnings = elektraKDBErrorWarnings (kdbError);
for (int i = 0; i < warningCount; ++i)
{
os << "\t\t - Warning " << elektraKDBErrorCode (warnings[i]) << " [" << elektraKDBErrorGroup (warnings[i]) << "/"
<< elektraKDBErrorModule (warnings[i]) << "]: " << elektraKDBErrorDescription (warnings[i]) << std::endl;
}
os << "\t\tFrom Key: " << ckdb::keyName (elektraKDBErrorKey (kdbError)) << std::endl;
}

return os;
}

static std::ostream & operator<< (std::ostream & os, ElektraError ** error)
{
if (*error != nullptr)
{
os << "[" << severityString (*error) << "] (" << elektraErrorCode (*error) << ") " << elektraErrorDescription (*error)
<< std::endl;
os << elektraErrorLowLevelError (*error);

int kdbCode = elektraKDBErrorCode (*error);
if (kdbCode > 0)
{
os << "\tKDB";
switch (elektraKDBErrorSeverity (*error))
{
case ELEKTRA_ERROR_SEVERITY_ERROR:
os << "ERROR";
break;
case ELEKTRA_ERROR_SEVERITY_WARNING:
os << "WARNING";
break;
default:
case ELEKTRA_ERROR_SEVERITY_FATAL:
os << "FATAL";
break;
}

os << " " << kdbCode << " [" << elektraKDBErrorGroup (*error) << "/" << elektraKDBErrorModule (*error) << "]"
<< ": " << elektraKDBErrorDescription (*error) << std::endl;
os << "\t\tReason: " << elektraKDBErrorReason (*error) << std::endl;

int warningCount = elektraKDBErrorWarningCount (*error);
os << "\t\t" << warningCount << " Warnings:" << std::endl;
for (int i = 0; i < warningCount; ++i)
{
ElektraError * warning = elektraKDBErrorGetWarning (*error, i);
os << "\t\t - Warning " << elektraKDBErrorCode (warning) << " [" << elektraKDBErrorGroup (warning) << "/"
<< elektraKDBErrorModule (warning) << "]: " << elektraKDBErrorDescription (warning) << std::endl;
ckdb::elektraFree (warning);
}
os << "\t\tFrom Key: " << ckdb::keyName (elektraKDBErrorKey (*error)) << std::endl;
}

elektraErrorReset (error);
}
return os;

0 comments on commit a25be1b

Please sign in to comment.