Skip to content

Commit 8ab493c

Browse files
author
Luke Robison
committed
opal/util/json: complain less about errors
For smaller things like type mismatch, errors should be handled by caller rather than show_help Signed-off-by: Luke Robison <lrbison@amazon.com>
1 parent 24889ce commit 8ab493c

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

opal/util/json/opal_json.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#define CHECK_OBJ_TYPE(obj, expected) \
2020
do { \
2121
if ((obj)->type != (expected)) { \
22-
opal_show_help("help-json.txt", "Invalid argument type", true, __func__, #expected, \
23-
expected, (obj)->type); \
2422
return OPAL_ERROR; \
2523
} \
2624
} while (0)
@@ -97,7 +95,6 @@ int opal_json_load(const char *str, const size_t len, const opal_json_t **json)
9795

9896
json_value *value = json_parse(str, len);
9997
if (!value) {
100-
opal_show_help("help-json.txt", "Invalid JSON string", true);
10198
ret = OPAL_ERROR;
10299
goto out;
103100
}
@@ -114,7 +111,7 @@ int opal_json_load(const char *str, const size_t len, const opal_json_t **json)
114111
return ret;
115112
}
116113

117-
int opal_json_load_file(const char *filename, const opal_json_t **json)
114+
int opal_json_load_file(const char *filename, const opal_json_t **json, int show_errors)
118115
{
119116
FILE *fp = NULL;
120117
size_t file_size;
@@ -123,7 +120,9 @@ int opal_json_load_file(const char *filename, const opal_json_t **json)
123120

124121
fp = fopen(filename, "r");
125122
if (fp == NULL) {
126-
opal_show_help("help-json.txt", "Unable to open file", true, filename);
123+
if (show_errors) {
124+
opal_show_help("help-json.txt", "Unable to open file", true, filename);
125+
}
127126
ret = OPAL_ERROR;
128127
goto out;
129128
}
@@ -134,18 +133,25 @@ int opal_json_load_file(const char *filename, const opal_json_t **json)
134133

135134
file_contents = (char *) malloc(file_size);
136135
if (!file_contents) {
137-
opal_show_help("help-json.txt", "Memory allocation failure", true);
136+
if (show_errors) {
137+
opal_show_help("help-json.txt", "Memory allocation failure", true);
138+
}
138139
ret = OPAL_ERROR;
139140
goto out;
140141
}
141142

142143
if (file_size > fread(file_contents, 1, file_size, fp)) {
143-
opal_show_help("help-json.txt", "Unable to read file", true, filename);
144+
if (show_errors) {
145+
opal_show_help("help-json.txt", "Unable to read file", true, filename);
146+
}
144147
ret = OPAL_ERROR;
145148
goto out;
146149
}
147150

148151
ret = opal_json_load(file_contents, file_size, json);
152+
if (ret != OPAL_SUCCESS && show_errors) {
153+
opal_show_help("help-json.txt", "Invalid JSON string", true);
154+
}
149155

150156
out:
151157
if (fp) {

opal/util/json/opal_json.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const opal_json_t *json = NULL, *item = NULL;
2121
char *val;
2222
size_t len;
2323
24-
ret = opal_json_load_file("my_file.json", &json); // Parse the file content into json
24+
ret = opal_json_load_file("my_file.json", &json, 1); // Parse the file content into json
2525
if (OPAL_SUCCESS != ret) {
2626
goto out;
2727
}
@@ -88,7 +88,7 @@ OPAL_DECLSPEC int opal_json_load(const char *str, const size_t len, const opal_j
8888
* @returns OPAL_SUCCESS if the file is read and parsed successfully
8989
* OPAL_ERROR otherwise
9090
*/
91-
OPAL_DECLSPEC int opal_json_load_file(const char *filename, const opal_json_t **json);
91+
OPAL_DECLSPEC int opal_json_load_file(const char *filename, const opal_json_t **json, int show_errors);
9292

9393
/**
9494
* Free JSON resources

test/util/opal_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static int load_json(const char *string, enum INPUT_TYPE input_type, const opal_
5656
fclose(fp);
5757
close(fd);
5858
/* Load the input string from the temporary file */
59-
ret = opal_json_load_file(filename, json);
59+
ret = opal_json_load_file(filename, json, 1);
6060
/* Remember to delete the file */
6161
(void) remove(filename);
6262
break;

0 commit comments

Comments
 (0)