From 1fb75b5a5326362f206744ced73d0bbd8f5305b6 Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Thu, 23 Jun 2016 16:58:20 -0700 Subject: [PATCH] Report JSON errors --- src/command.c | 35 ++++++++++------------------------- src/main.c | 2 +- src/messages.def | 4 +++- src/status.c | 2 +- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/command.c b/src/command.c index 6d4d2d3..37cf3be 100644 --- a/src/command.c +++ b/src/command.c @@ -94,8 +94,6 @@ int command_find(const char *match) { int command_exec(int argc, char *argv[]) { - stat_t status = STAT_INVALID_OR_MALFORMED_COMMAND; - putchar('\n'); int i = command_find(argv[0]); @@ -103,23 +101,15 @@ int command_exec(int argc, char *argv[]) { uint8_t minArgs = pgm_read_byte(&commands[i].minArgs); uint8_t maxArgs = pgm_read_byte(&commands[i].maxArgs); - if (argc <= minArgs) { - printf_P(PSTR("Too few arguments\n")); - return status; - - } else if (maxArgs < argc - 1) { - printf_P(PSTR("Too many arguments\n")); - return status; - - } else { + if (argc <= minArgs) return STAT_TOO_FEW_ARGUMENTS; + else if (maxArgs < argc - 1) return STAT_TOO_MANY_ARGUMENTS; + else { command_cb_t cb = pgm_read_word(&commands[i].cb); return cb(argc, argv); } - } else if (argc != 1) { - printf_P(PSTR("Unknown command '%s' or invalid arguments\n"), argv[0]); - return status; - } + } else if (argc != 1) + return STAT_INVALID_OR_MALFORMED_COMMAND; // Get or set variable char *value = strchr(argv[0], '='); @@ -132,9 +122,7 @@ int command_exec(int argc, char *argv[]) { return STAT_OK; } - printf_P(PSTR("Unknown command or variable '%s'\n"), argv[0]); - - return status; + return STAT_UNRECOGNIZED_NAME; } @@ -193,13 +181,10 @@ uint8_t command_help(int argc, char *argv[]) { if (argc == 2) { int i = command_find(argv[1]); - if (i == -1) { - printf_P(PSTR("Command not found\n")); - return STAT_INVALID_OR_MALFORMED_COMMAND; + if (i == -1) return STAT_UNRECOGNIZED_NAME; + else print_command_help(i); - } else print_command_help(i); - - return 0; + return STAT_OK; } puts_P(PSTR("\nCommands:")); @@ -213,7 +198,7 @@ uint8_t command_help(int argc, char *argv[]) { puts_P(PSTR("\nVariables:")); vars_print_help(); - return 0; + return STAT_OK; } diff --git a/src/main.c b/src/main.c index 4558125..0d3882b 100644 --- a/src/main.c +++ b/src/main.c @@ -81,7 +81,7 @@ static bool _dispatch(stat_t (*func)()) { switch (err) { case STAT_EAGAIN: return true; case STAT_OK: case STAT_NOOP: break; - default: printf_P(PSTR("%S\n"), status_to_pgmstr(err)); + default: status_error(err); break; } return false; diff --git a/src/messages.def b/src/messages.def index 31f3e4e..ea0973b 100644 --- a/src/messages.def +++ b/src/messages.def @@ -41,8 +41,10 @@ STAT_MSG(PREP_LINE_MOVE_TIME_IS_INFINITE, "Move time is infinite") STAT_MSG(PREP_LINE_MOVE_TIME_IS_NAN, "Move time is NAN") // Generic data input errors -STAT_MSG(UNRECOGNIZED_NAME, "Unrecognized command or config name") +STAT_MSG(UNRECOGNIZED_NAME, "Unrecognized command or variable name") STAT_MSG(INVALID_OR_MALFORMED_COMMAND, "Invalid or malformed command") +STAT_MSG(TOO_MANY_ARGUMENTS, "Too many arguments to command") +STAT_MSG(TOO_FEW_ARGUMENTS, "Too few arguments to command") STAT_MSG(BAD_NUMBER_FORMAT, "Bad number format") STAT_MSG(PARAMETER_IS_READ_ONLY, "Parameter is read-only") STAT_MSG(PARAMETER_CANNOT_BE_READ, "Parameter cannot be read") diff --git a/src/status.c b/src/status.c index ed691cc..d3674f7 100644 --- a/src/status.c +++ b/src/status.c @@ -53,7 +53,7 @@ stat_t status_error(stat_t status) { stat_t status_error_P(const char *location, const char *msg, stat_t status) { - printf_P(PSTR("\n{\"error\": %d, \"code\": %d"), + printf_P(PSTR("\n{\"error\": \"%S\", \"code\": %d"), status_to_pgmstr(status), status); if (msg) printf_P(PSTR(", \"msg\": %S"), msg); -- 2.27.0