Uploaded image for project: 'Spring Roo'
  1. Spring Roo
  2. ROO-3486

JSON methods may fail and yet still return a success (2xx) status code

    Details

      Description

      I have created a number of entities and generated a web mvc layer including JSON controllers.

      If, for any reason, code that the JSON controllers depends on fails then 2xx status code is still returned. This can be as simple as a mis-configured database resulting in a NullPointerException in generated ActiveRecord aspects or a more obscure bug in pushed-in business logic.

      I propose that we catch all Exceptions and handle them with a 500 status code and brief message in the JSON payload. Returning the stack trace would IMO reveal information about the system and so therefore should not be done but at the same time something to help the client application author would be helpful. The proposal below allows the server side author to reveal a (hopefully helpful) message or none.

      Existing generated example:

       
      public ResponseEntity<String> AccountController.createFromJsonArray(@RequestBody String json) {
              for (Account account: Account.fromJsonArrayToAccounts(json)) {
                  account.persist();
              }
              HttpHeaders headers = new HttpHeaders();
              headers.add("Content-Type", "application/json");
              return new ResponseEntity<String>(headers, HttpStatus.CREATED);
          }
      

      Proposed generated example:

      @RequestMapping(value = "/jsonArray", method = RequestMethod.POST, headers = "Accept=application/json")
          public ResponseEntity<String> AccountController.createFromJsonArray(@RequestBody String json) {
              for (Account account: Account.fromJsonArrayToAccounts(json)) {
                  account.persist();
              }
              HttpHeaders headers = new HttpHeaders();
              headers.add("Content-Type", "application/json");
              try {
                  return new ResponseEntity<String>(headers, HttpStatus.CREATED);
              } catch (Exception e) {
                  return new ResponseEntity<String>("{\"ERROR\":"+e.getMessage()+"\"}", headers, HttpStatus.INTERNAL_SERVER_ERROR);
              }
          }
      

      In some cases there are already returned 404 codes and these should of course remain.

        Attachments

          Activity

            People

            • Assignee:
              eruiz Enrique Ruiz (DiSiD)
              Reporter:
              tstephenson Tim Stephenson
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: