Merge pull request #2175 from apreiml/fix-swaymsg-error-output

fix swaymsg: errors are displayed again
This commit is contained in:
emersion 2018-06-30 12:28:17 +01:00 committed by GitHub
commit 09fdcfe7df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,30 +19,40 @@ void sway_terminate(int exit_code) {
exit(exit_code); exit(exit_code);
} }
static bool success_object(json_object *result) {
json_object *success;
if (!json_object_object_get_ex(result, "success", &success)) {
return false;
}
return json_object_get_boolean(success);
}
// Iterate results array and return false if any of them failed // Iterate results array and return false if any of them failed
static bool success(json_object *r, bool fallback) { static bool success(json_object *r, bool fallback) {
if (!json_object_is_type(r, json_type_array)) { if (!json_object_is_type(r, json_type_array)) {
return fallback; return fallback;
} }
size_t results_len = json_object_array_length(r); size_t results_len = json_object_array_length(r);
if (!results_len) { if (!results_len) {
return fallback; return fallback;
} }
for (size_t i = 0; i < results_len; ++i) { for (size_t i = 0; i < results_len; ++i) {
json_object *result = json_object_array_get_idx(r, i); json_object *result = json_object_array_get_idx(r, i);
json_object *success;
if (!json_object_object_get_ex(result, "success", &success)) { if (!success_object(result)) {
return false;
}
if (!json_object_get_boolean(success)) {
return false; return false;
} }
} }
return true; return true;
} }
static void pretty_print_cmd(json_object *r) { static void pretty_print_cmd(json_object *r) {
if (!success(r, true)) { if (!success_object(r)) {
json_object *error; json_object *error;
if (!json_object_object_get_ex(r, "error", &error)) { if (!json_object_object_get_ex(r, "error", &error)) {
printf("An unknkown error occurred"); printf("An unknkown error occurred");
@ -402,6 +412,7 @@ int main(int argc, char **argv) {
} else { } else {
sway_abort("Unknown message type %s", cmdtype); sway_abort("Unknown message type %s", cmdtype);
} }
free(cmdtype); free(cmdtype);
char *command = NULL; char *command = NULL;