Server provided errors handling

Server provided errors handling

ralfeusralfeus Posts: 24Questions: 6Answers: 1

Hi all,
how exactly server side provided errors are handled by the Editor? I read this article but it describes only format of the data returned, which I've implemented.

However I don't see those messages in my frontend. Should I also implement it in postSubmit handler? Or it should be done somehow automatically?

Replies

  • ralfeusralfeus Posts: 24Questions: 6Answers: 1

    I have found this article saying:

    The request to the server will be considered to be successful as long as valid JSON is returned (regardless of status code), while any response with invalid JSON will fall into Editor's error handler. This is particularly useful when interfacing Editor with an REST service where response codes can be used to convey useful information - for example a submission which contains invalid data could have a 400 response code and valid JSON noting which field was in error, and what the error was. Editor will correctly handle such a case

    Having that in mind I expected that this code would work fine (will show proper errors):

        g_editor = new $.fn.dataTable.Editor({
            ajax: (_method, _url, data, success, error_callback) => {
        ...
                $.ajax({
                    url: url,
                    method: method,
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(target),
                    success: data => { success(data); },
                    error: error_callback
        });
    

    In case of error (validation error) I return following (HTTP 400):

    {
      "error": "Couldn't save a payment", 
      "fieldErrors": [
        {
          "name": "user_id", 
          "status": " Is not a valid user"
        }, 
        {
          "name": "payment_method.id", 
          "status": " Is not a valid payment method"
        }
      ]
    }
    

    However no my error messages were shown on the edit form - just a standard "A system error has occurred (More information)." one.
    So I've changed an error handler to this:

                    error: (xhr, error, status) => {
                        if (xhr.responseJSON) {
                            var error_data = xhr.responseJSON
                            if (error_data.error) {
                                g_editor.error(error_data.error);
                            }
                            error_data.fieldErrors.forEach(e => {
                                if (g_editor.fields().includes(e.name)) {
                                    g_editor.error(e.name, e.status);
                                }
                            });
                        }
                        error_callback(xhr, error, status);
    

    Then field error messages were shown. However the general error message (Couldn't save a payment) wasn't shown. Instead the standard one was. And if I don't call error_callback(xhr, error, status) the message is shown then but the submit process never ends.

    From the description I understand the JSON I return is invalid. But I believe I've constructed it according to the documentation. What could I miss? Or it's a bug?

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin

    However I don't see those messages in my frontend. Should I also implement it in postSubmit handler? Or it should be done somehow automatically?

    It should be completely automatic as you indicate in your second post.

    Having that in mind I expected that this code would work fine (will show proper errors):

    In this case you have provided your own ajax function, which is going to complicate things. Can I suggest you use:

    ajax: {
      url: _url,
      contentType: 'application/json',
      data: function (d) {
        return JSON.stringify(d);
      }
    }
    

    That will allow Editor to perform its own error handling, including the 400 error.

    Regards,
    Allan

This discussion has been closed.