parser: return a proper JSON reply on parse errors
This commit is contained in:
parent
7c6f2dbfc6
commit
a415d56048
|
@ -35,6 +35,8 @@ struct context {
|
||||||
char *line_copy;
|
char *line_copy;
|
||||||
const char *filename;
|
const char *filename;
|
||||||
|
|
||||||
|
const char *compact_error;
|
||||||
|
|
||||||
/* These are the same as in YYLTYPE */
|
/* These are the same as in YYLTYPE */
|
||||||
int first_column;
|
int first_column;
|
||||||
int last_column;
|
int last_column;
|
||||||
|
|
|
@ -58,6 +58,7 @@ void cmdyyerror(const char *error_message) {
|
||||||
else printf(" ");
|
else printf(" ");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
ELOG("\n");
|
ELOG("\n");
|
||||||
|
context->compact_error = sstrdup(error_message);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmdyywrap() {
|
int cmdyywrap() {
|
||||||
|
@ -73,13 +74,17 @@ char *parse_cmd(const char *new) {
|
||||||
FREE(json_output);
|
FREE(json_output);
|
||||||
if (cmdyyparse() != 0) {
|
if (cmdyyparse() != 0) {
|
||||||
fprintf(stderr, "Could not parse command\n");
|
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->line_copy);
|
||||||
|
FREE(context->compact_error);
|
||||||
free(context);
|
free(context);
|
||||||
return;
|
return json_output;
|
||||||
}
|
}
|
||||||
printf("done, json output = %s\n", json_output);
|
printf("done, json output = %s\n", json_output);
|
||||||
|
|
||||||
FREE(context->line_copy);
|
FREE(context->line_copy);
|
||||||
|
FREE(context->compact_error);
|
||||||
free(context);
|
free(context);
|
||||||
return json_output;
|
return json_output;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue