Report JSON errors
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Thu, 23 Jun 2016 23:58:20 +0000 (16:58 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Thu, 23 Jun 2016 23:58:20 +0000 (16:58 -0700)
src/command.c
src/main.c
src/messages.def
src/status.c

index 6d4d2d3a79040bf5fd7ccadcd513683f864eaca6..37cf3be0a0200cf32e1f0bcea379a3e1cdbe68e7 100644 (file)
@@ -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;
 }
 
 
index 45581253f0a0d96ab54e5beb446196223746d753..0d3882bea1343e410dbee791b4553db4a6d801ef 100644 (file)
@@ -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;
index 31f3e4eca7eada7eb16e5b3252ebc06f9e7a7de6..ea0973b471a0365692f79f2bfec6044bf7f84202 100644 (file)
@@ -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")
index ed691cc8b6f8aefc37367c3aa78ec07e61bdb932..d3674f7675cb8b091d672a6795ff2a7754a592a8 100644 (file)
@@ -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);