onAfterSaveError server side response format

Hi, I have written my own web server and am struggling with what exactly my servers response should be when using data processor
'onAfterSaveError', function(id, status, response, details)

The response and the details objects are what i'm having trouble returning, currently I am returning this from my server when after a database error

StatusCode := 500;
ContentType := 'application/json';
Response := '{ "response":{"status":"error"} ,"details":{"text":"test error"} }';

any ideas what I should try?
Thanks
Tom

Comments

  • Hey @Tomk, our documentation includes the needed server response format, please take a look: https://docs.webix.com/desktop__dataprocessor.html#serverresponse.

  • edited August 12

    Hi I have read that section which then leads on to the onAfterSaveError documentation below

    The structure of the response object:

    {
       id:"id of the item",
       status:"update status",
       newid:"new id after operation"
    }
    

    The structure of the details object:

    {
       text:"full text of server side response",
       data:"webix ajax data related to the error",
       loader: xhr // xmlHttpRequest object related to the error
    }
    

    I have the event in my java script like this below, it is returning the id and status values already (without me doing anything server side for them) that actually makes the response object of no use to me as I will probably never have a newid if there is an error.

    dp.attachEvent('onAfterSaveError', function(id, status, response, details)
    

    I really just need the details object part so I can return some text to the user about the error. Do you have an actual server code snippet for this part?
    Tom

  • edited August 13

    Unfortunately I can't provide you with the server side sample in the language you've written syntax in, but as for the server running on Node.js/Express, the response object would look something like this:

    res.send({
                status:"error",
                custom_text:"Test message"
            });
    

    Where the key is having the property status:"error", which will trigger the server side error, but it won't trigger the onAfterSaveError event. To trigger the event, you have to send a response with a status > 400 (meaning it will be classified as an error on the client-side too):

    res.status(500).send({
                status:"error",
                custom_text:"Test message"
            });
    

    The response object should now be available on the client-side via the onAfterSaveError event. You can get the response object via the XHR loader: details.loader.responseText:

    dp.attachEvent("onAfterSaveError", function(id, status, response, details) {console.log(details.loader.responseText)})
    

    I would also like to note that if you want to revert the changes upon the error while saving on the server-side, you should enable undoOnError property fot the dataprocessor, i.e:

    const dp = webix.dp($$('$datatable1'));
    dp.define({undoOnError: true});
    

    Also please don't forget that you will need to have the undo property of your view set to true.

  • Thanks,
    I did end up finding that I could use details.loader.response. Still not sure how the details object is populated, however it does not really mater now. I do get the onAfterSaveError to fire even on a status code 200 provided I set status:"error". But I get different results passed back

    so I have done a function to handle both like this (I call this from onAfterSaveError passing all the same parameters)

    function displayJSONError(id, status, response, details){
    if (isObjAssigned(details.loader)) { //status code >400
    var errJSON = JSON.parse(details.loader.response.toString());
    $$("infotext").setValue(status + " " + "error: [ " + errJSON.error_text + " ]");
    }
    else //status code 200
    {
    $$("infotext").setValue(status + " " + "error: [ " + response.error_text + " ]");
    }
    $$("scrollview").show();
    }

    I will leave it like this unless you think there is something wrong with this approach

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion