From: Michael Stapelberg Date: Sun, 14 Nov 2010 00:45:05 +0000 (+0100) Subject: parser: return a proper JSON reply on parse errors X-Git-Tag: tree-pr1~108 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=a415d56048a18720bfdab8144652b87cb39ce626;p=i3%2Fi3 parser: return a proper JSON reply on parse errors --- diff --git a/include/config.h b/include/config.h index a435cb5b..ab2dac87 100644 --- a/include/config.h +++ b/include/config.h @@ -35,6 +35,8 @@ struct context { char *line_copy; const char *filename; + const char *compact_error; + /* These are the same as in YYLTYPE */ int first_column; int last_column; diff --git a/src/cmdparse.y b/src/cmdparse.y index d79698e4..e5c9aa5c 100644 --- a/src/cmdparse.y +++ b/src/cmdparse.y @@ -58,6 +58,7 @@ void cmdyyerror(const char *error_message) { else printf(" "); printf("\n"); ELOG("\n"); + context->compact_error = sstrdup(error_message); } int cmdyywrap() { @@ -73,13 +74,17 @@ char *parse_cmd(const char *new) { FREE(json_output); if (cmdyyparse() != 0) { fprintf(stderr, "Could not parse command\n"); + asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}", + context->compact_error, context->first_column); FREE(context->line_copy); + FREE(context->compact_error); free(context); - return; + return json_output; } printf("done, json output = %s\n", json_output); FREE(context->line_copy); + FREE(context->compact_error); free(context); return json_output; }