Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    REST data source and HTTP response codes

    Hello,

    I have a pure RESTful server that's providing some API calls using GET, POST, PUT, and DELETE, in true REST fashion. Additionally, many of the calls set HTTP response codes to values that reflect the result of the request (using 2xx to indicate overall success, 4xx to indicate client-side error, 5xx to indicate server-side error)

    I want to be able to use the RestDataSource from SmartGWT to do databinding to grids and forms. I was able to extend RestDataSource to allow me to use the GET, POST, PUT, DELETE methods. The question I have is regarding how SmartClient deals with the HTTP response codes.

    For example, I have a client-side form that's databound to a REST data source. When the form is submitted (via POST) on the client-side, the server performs validation on the request, and if there's an error, returns a proper JSON response (that RestDataSource can understand), but ALSO sets the HTTP response code to a non-200 value. When this happens, no client-side error indication is given... it's like the response is dropped completely. If the HTTP response code is 200, then I do see a client-side WARNING icon being displayed, with the proper error message.

    The question is: is it possible to instruct SmartClient to process the response even if the HTTP response code is not 200? If so, how?

    Thank you,
    Boris

    #2
    If the response appears to be an error (anything other than 2xx) it goes to global error handling (see RPCManager.setHandleErrorCallback()) unless willHandleError has been set on the DSRequest. So if you wanted to treat an HTTP 4xx code as a normal error condition, you'd override transformRequest to setWillHandleError() true on an outbound requests, and transformResponse to set the dsResponse status to the constant for validation errors when you see a 4xx error code.

    Comment


      #3
      Thank you for the prompt response.

      Why is it necessary to set the dsResponse status manually in code? Isn't the status picked up from the JSON response body returned by the server?

      Code:
      {
         response: {
            status:-4,
            errors: {
               field1: [{errorMessage:"Validation error on field1"}]
            }
         }
      }

      Comment


        #4
        You're returning something that is treated as a transport-level error. Like a 500 server error, the content is not looked at because it is assumed not to be in the normal response format.

        Comment

        Working...
        X