diff options
author | Geoff Greer <geoff@greer.fm> | 2017-10-22 18:03:45 -0700 |
---|---|---|
committer | Geoff Greer <geoff@greer.fm> | 2017-10-22 18:09:36 -0700 |
commit | 29f27c7cdc9e11aa560a5d49d110fc705dbc21cb (patch) | |
tree | 06c66f34523546b3a21dae6a6edcadc6f7decc6d /swaygrab | |
parent | Merge pull request #1424 from ggreer/swaygrab-json (diff) | |
download | sway-29f27c7cdc9e11aa560a5d49d110fc705dbc21cb.tar.gz sway-29f27c7cdc9e11aa560a5d49d110fc705dbc21cb.tar.zst sway-29f27c7cdc9e11aa560a5d49d110fc705dbc21cb.zip |
swaygrab: Add some error handling.
- If IPC response contains `success: false`, abort and print error message.
- If tree has no nodes, abort with error msg instead of segfaulting.
Diffstat (limited to 'swaygrab')
-rw-r--r-- | swaygrab/json.c | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/swaygrab/json.c b/swaygrab/json.c index 32b68612..286085c3 100644 --- a/swaygrab/json.c +++ b/swaygrab/json.c | |||
@@ -21,6 +21,14 @@ void init_json_tree(int socketfd) { | |||
21 | if (!tree || tok->err != json_tokener_success) { | 21 | if (!tree || tok->err != json_tokener_success) { |
22 | sway_abort("Unable to parse IPC response as JSON: %s", json_tokener_error_desc(tok->err)); | 22 | sway_abort("Unable to parse IPC response as JSON: %s", json_tokener_error_desc(tok->err)); |
23 | } | 23 | } |
24 | json_object *success; | ||
25 | json_object_object_get_ex(tree, "success", &success); | ||
26 | if (success && !json_object_get_boolean(success)) { | ||
27 | json_object *error; | ||
28 | json_object_object_get_ex(tree, "error", &error); | ||
29 | sway_abort("IPC request failed: %s", json_object_get_string(error)); | ||
30 | } | ||
31 | json_object_put(success); | ||
24 | json_tokener_free(tok); | 32 | json_tokener_free(tok); |
25 | } | 33 | } |
26 | 34 | ||
@@ -72,7 +80,9 @@ json_object *get_focused_container() { | |||
72 | char *get_focused_output() { | 80 | char *get_focused_output() { |
73 | json_object *outputs, *output, *name; | 81 | json_object *outputs, *output, *name; |
74 | json_object_object_get_ex(tree, "nodes", &outputs); | 82 | json_object_object_get_ex(tree, "nodes", &outputs); |
75 | 83 | if (!outputs) { | |
84 | sway_abort("Unabled to get focused output. No nodes in tree."); | ||
85 | } | ||
76 | for (int i = 0; i < json_object_array_length(outputs); i++) { | 86 | for (int i = 0; i < json_object_array_length(outputs); i++) { |
77 | output = json_object_array_get_idx(outputs, i); | 87 | output = json_object_array_get_idx(outputs, i); |
78 | 88 | ||