Replace transformation of warnings to errors in elektra.c with elektra_error.c
#include "kdbhelper.h"
#include "kdblogger.h"
#include "kdbprivate.h"
-#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
@@ -119,36 +118,20 @@ Elektra * elektraOpen (const char * application, KeySet * defaults, KeySet * con
const int kdbGetResult = kdbGet (kdb, config, parentKey);
- // Look for warnings in the parentKey after kdbGet().
- // If there are warnings, create an error, set the param "error" and return NULL.
- KeySet * parentKeyMeta = keyMeta(parentKey);
- for (elektraCursor metaIt = 0; metaIt < ksGetSize (parentKeyMeta); metaIt++) {
- // All warnings are available as array items of array "warnings" in parentKey.
- const Key * currentMetaKey = ksAtCursor(parentKeyMeta, metaIt);
- // If the meta key name starts with "warnings", it is a warning.
- bool isWarning = strncmp(keyName(currentMetaKey), "meta:/warnings", strlen("meta:/warnings")) == 0;
-
- size_t baseNameSize = keyGetBaseNameSize (currentMetaKey);
- char * kBaseName = elektraMalloc (baseNameSize * sizeof (char));
- keyGetBaseName (currentMetaKey, kBaseName, baseNameSize);
- // If the baseName is "reason", this is the reason we are interested in
- // (e.g. "Value '50' of key 'proc:/sw/jonls/redshift/#0/current/temp/day' not within range 1000-2500")
- bool isWarningReason = isWarning && strcmp(kBaseName, "reason") == 0;
- if (isWarningReason) {
- // We found a warning reason.
- const char * warningReason = keyString(currentMetaKey);
- // Create an error that we can return to the calling application via the "error" param.
- ELEKTRA_SET_INTERNAL_ERROR (parentKey, warningReason);
- *error = elektraErrorFromKey (parentKey);
- elektraFree (kBaseName);
+ // Applications using the HL API should treat warnings as errors. Therefore, if a warning occurred, set the error param and return NULL.
+ ElektraError * myError = elektraErrorFromKey (parentKey);
+
+ if (myError->warningCount > 0) {
+ // If there are warnings, pick the first warning, set it to param "error" and return NULL.
+ // We can only report 1 error at a time. Once the user has fixed that error, they will be informed about the next one on the next execution of the application.
+
+ *error = myError->warnings[0];
ksDel(config);
kdbClose(kdb, parentKey);
keyDel(parentKey);
return NULL;
}
- elektraFree (kBaseName);
- }
if (kdbGetResult == -1)
{
@@ -170,7 +153,7 @@ Elektra * elektraOpen (const char * application, KeySet * defaults, KeySet * con
}
else
{
- *error = elektraErrorFromKey (parentKey);
+ *error = myError;
ksDel (config);
kdbClose (kdb, parentKey);
