Quantcast

getting dstore/dgrid to respond to server errors

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

getting dstore/dgrid to respond to server errors

Simon Walter
Hi all,

I've set up my dgrid with a memory/rest store. It works well. I would
like to take the next step and program it to handle errors. I can send
any data I like from the server. So there are no restrictions on that
side. Right now I am sending 500, 404, and 403 HTTP codes along with
some JSON data like {error: 500, reason: "Could not create a new item"}

If I add a new item to the store with restStore.add({}), the server will
respond with JSON that dgrid expects:
{id: 5, data1: "asdf", data2: "fdsa"}
which it will use for an item in the collection.
I don't know what data/format is expected for errors.

I'd like to know how to get dstore to respond to certain JSON responses
and then handle them. For example, if I have a target of "/grid/getData"
and that location returns 404, I would like to have dstore call some
code that will popup a message and let the user know what's up instead
of just displaying nothing - failing inelegantly. Same for putting data
to "/grid/saveItem/345". If that returns 500 for example, I would like
to notify the user that their data was not saved and that there was a
problem with the server and then of course revert their changes to the
store.

Kind regards,

Simon


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: getting dstore/dgrid to respond to server errors

tk8ykex5z5
This may be what you want:

this.on("dgrid-error", lang.hitch(this, function(event) {
    var msg = event.error.message;
    // Do something with the error
}));

See

https://github.com/SitePen/dgrid/blob/v1.0.0/doc/usage/Working-with-Events.md

Brian
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: getting dstore/dgrid to respond to server errors

Kenneth G. Franqueiro
There's also an example under Handling Errors in
http://dgrid.io/tutorials/1.0/grids_and_stores/

--Ken

On 3/3/2016 4:16 PM, tk8ykex5z5 wrote:

> This may be what you want:
>
>
>
> See
>
> https://github.com/SitePen/dgrid/blob/v1.0.0/doc/usage/Working-with-Events.md
>
> Brian
>
>
>
>
> --
> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/getting-dstore-dgrid-to-respond-to-server-errors-tp4006737p4006750.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
>
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: getting dstore/dgrid to respond to server errors

Simon Walter
Thanks Ken and Brian, that's perfect for the initial loading of the data. I'm glad I am off to a good start.

For some reason, however, dgrid-error is not emitted for errors that occur when calling add() or remove() on the store and save() on the grid. I am using OnDemandGrid and Editor for the grid and Rest, RequestMemory, and Trackable for store store.

I can see that: "if an error is encountered as a result of a store operation triggered by `autoSave`, a `dgrid-error` event will be emitted."

What about if I don't have autoSave enabled and I call theGrid.save() explicitly? Will an error event be emitted if, for example, a 500 response is received?

Do I need to listen for different events? Or maybe listen to events emitted by the store?

I tried snooping around the code to see if there are other error events to listen for, but I don't really know what I am looking at. I see in Rest.js there is:
store.emit(httpResponse.status === 201 ? 'add' : 'update', event);

So I am guessing that it's not so difficult to check for errors and then emit an error event. I could start hacking that but not sure if that's unnecessary. Please advise. Thanks.

Kind regards,

Simon

On 2016/03/04 6:21, Kenneth G. Franqueiro wrote:
There's also an example under Handling Errors in
http://dgrid.io/tutorials/1.0/grids_and_stores/

--Ken

On 3/3/2016 4:16 PM, tk8ykex5z5 wrote:
This may be what you want:



See

https://github.com/SitePen/dgrid/blob/v1.0.0/doc/usage/Working-with-Events.md

Brian




--
View this message in context: http://dojo-toolkit.33424.n3.nabble.com/getting-dstore-dgrid-to-respond-to-server-errors-tp4006737p4006750.html
Sent from the Dojo Toolkit mailing list archive at Nabble.com.



--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: getting dstore/dgrid to respond to server errors

Kenneth G. Franqueiro
If you're calling add or remove on the store, those return promises that
reject on error.

dgrid's save method returns a promise which should reject if any of the
put operations it performs fail.

Essentially, if you're dealing directly with something that already
returns a promise that indicates success or failure, use that.

--Ken

On 3/4/2016 2:19 AM, Simon Walter wrote:

> Thanks Ken and Brian, that's perfect for the initial loading of the
> data. I'm glad I am off to a good start.
>
> For some reason, however, |dgrid-error| is not emitted for errors that
> occur when calling add() or remove() on the store and save() on the
> grid. I am using OnDemandGrid and Editor for the grid and Rest,
> RequestMemory, and Trackable for store store.
>
> I can see that: "if an error is encountered as a result of a store
> operation triggered by `autoSave`, a `dgrid-error` event will be emitted."
>
> What about if I don't have autoSave enabled and I call theGrid.save()
> explicitly? Will an error event be emitted if, for example, a 500
> response is received?
>
> Do I need to listen for different events? Or maybe listen to events
> emitted by the store?
>
> I tried snooping around the code to see if there are other error events
> to listen for, but I don't really know what I am looking at. I see in
> Rest.js there is:
> store.emit(httpResponse.status === 201 ? 'add' : 'update', event);
>
> So I am guessing that it's not so difficult to check for errors and then
> emit an error event. I could start hacking that but not sure if that's
> unnecessary. Please advise. Thanks.
>
> Kind regards,
>
> Simon
>
> On 2016/03/04 6:21, Kenneth G. Franqueiro wrote:
>> There's also an example under Handling Errors in
>> http://dgrid.io/tutorials/1.0/grids_and_stores/
>>
>> --Ken
>>
>> On 3/3/2016 4:16 PM, tk8ykex5z5 wrote:
>>> This may be what you want:
>>>
>>>
>>>
>>> See
>>>
>>> https://github.com/SitePen/dgrid/blob/v1.0.0/doc/usage/Working-with-Events.md
>>>
>>> Brian
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/getting-dstore-dgrid-to-respond-to-server-errors-tp4006737p4006750.html
>>> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
>>>
>
>
>
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: getting dstore/dgrid to respond to server errors

Simon Walter
Ken, thanks for that succinct answer. After studying dojo's promises, I
understand better what needs to be done.

Kind regards,

Simon

On 2016/03/05 1:25, Kenneth G. Franqueiro wrote:

> If you're calling add or remove on the store, those return promises that
> reject on error.
>
> dgrid's save method returns a promise which should reject if any of the
> put operations it performs fail.
>
> Essentially, if you're dealing directly with something that already
> returns a promise that indicates success or failure, use that.
>
> --Ken
>
> On 3/4/2016 2:19 AM, Simon Walter wrote:
>> Thanks Ken and Brian, that's perfect for the initial loading of the
>> data. I'm glad I am off to a good start.
>>
>> For some reason, however, |dgrid-error| is not emitted for errors that
>> occur when calling add() or remove() on the store and save() on the
>> grid. I am using OnDemandGrid and Editor for the grid and Rest,
>> RequestMemory, and Trackable for store store.
>>
>> I can see that: "if an error is encountered as a result of a store
>> operation triggered by `autoSave`, a `dgrid-error` event will be emitted."
>>
>> What about if I don't have autoSave enabled and I call theGrid.save()
>> explicitly? Will an error event be emitted if, for example, a 500
>> response is received?
>>
>> Do I need to listen for different events? Or maybe listen to events
>> emitted by the store?
>>
>> I tried snooping around the code to see if there are other error events
>> to listen for, but I don't really know what I am looking at. I see in
>> Rest.js there is:
>> store.emit(httpResponse.status === 201 ? 'add' : 'update', event);
>>
>> So I am guessing that it's not so difficult to check for errors and then
>> emit an error event. I could start hacking that but not sure if that's
>> unnecessary. Please advise. Thanks.
>>
>> Kind regards,
>>
>> Simon
>>
>> On 2016/03/04 6:21, Kenneth G. Franqueiro wrote:
>>> There's also an example under Handling Errors in
>>> http://dgrid.io/tutorials/1.0/grids_and_stores/
>>>
>>> --Ken
>>>
>>> On 3/3/2016 4:16 PM, tk8ykex5z5 wrote:
>>>> This may be what you want:
>>>>
>>>>
>>>>
>>>> See
>>>>
>>>> https://github.com/SitePen/dgrid/blob/v1.0.0/doc/usage/Working-with-Events.md
>>>>
>>>> Brian
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/getting-dstore-dgrid-to-respond-to-server-errors-tp4006737p4006750.html
>>>> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
>>>>
>>
>>

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: getting dstore/dgrid to respond to server errors

Simon Walter
Hi everyone,

I have a couple follow up questions regarding REST responses and
dgrid/dstore.

I am using these mixins with a dgrid: OnDemandGrid, RequestMemory, Rest,
Trackable, Editor.

I have a button like so:

             this.button.save = new Button({
                 label: "Save",
                 onClick: function(){
                     var promise = self.grid.save().then(
                     function(response){
                         // need the http response code
                         console.log(response);
                     },
                     function(httpResponse){
                         // need to stop the grid from updating with the
not saved data!
                         console.log(httpResponse);
                     }
                     );
                     console.log(promise);
                 }
             });

How can I use RequestMemory, but then when the call to the server fails,
not keep the changes that were made on the screen but revert to the
original values?

Same question for when I remove a row. Can I force the dgrid to cancel
the changes to RequestMemory when the call fails (via errback or some
other place).

Also, when using then(), it looks like the argument sent to the callback
function is different than the argument sent to the errback function.
Specifically, the argument sent to the errback function contains useful
information like the http response code. Only the text of the response
is sent to the callback function.

Is there a way I can get the http response code while using then() and
the callback? Or is that information obtainable from the promise?

Kind regards,

Simon

On 03/10/2016 05:06 PM, Simon Walter wrote:

> Ken, thanks for that succinct answer. After studying dojo's promises, I
> understand better what needs to be done.
>
> Kind regards,
>
> Simon
>
> On 2016/03/05 1:25, Kenneth G. Franqueiro wrote:
>> If you're calling add or remove on the store, those return promises that
>> reject on error.
>>
>> dgrid's save method returns a promise which should reject if any of the
>> put operations it performs fail.
>>
>> Essentially, if you're dealing directly with something that already
>> returns a promise that indicates success or failure, use that.
>>
>> --Ken
>>
>> On 3/4/2016 2:19 AM, Simon Walter wrote:
>>> Thanks Ken and Brian, that's perfect for the initial loading of the
>>> data. I'm glad I am off to a good start.
>>>
>>> For some reason, however, |dgrid-error| is not emitted for errors that
>>> occur when calling add() or remove() on the store and save() on the
>>> grid. I am using OnDemandGrid and Editor for the grid and Rest,
>>> RequestMemory, and Trackable for store store.
>>>
>>> I can see that: "if an error is encountered as a result of a store
>>> operation triggered by `autoSave`, a `dgrid-error` event will be emitted."
>>>
>>> What about if I don't have autoSave enabled and I call theGrid.save()
>>> explicitly? Will an error event be emitted if, for example, a 500
>>> response is received?
>>>
>>> Do I need to listen for different events? Or maybe listen to events
>>> emitted by the store?
>>>
>>> I tried snooping around the code to see if there are other error events
>>> to listen for, but I don't really know what I am looking at. I see in
>>> Rest.js there is:
>>> store.emit(httpResponse.status === 201 ? 'add' : 'update', event);
>>>
>>> So I am guessing that it's not so difficult to check for errors and then
>>> emit an error event. I could start hacking that but not sure if that's
>>> unnecessary. Please advise. Thanks.
>>>
>>> Kind regards,
>>>
>>> Simon
>>>
>>> On 2016/03/04 6:21, Kenneth G. Franqueiro wrote:
>>>> There's also an example under Handling Errors in
>>>> http://dgrid.io/tutorials/1.0/grids_and_stores/
>>>>
>>>> --Ken
>>>>
>>>> On 3/3/2016 4:16 PM, tk8ykex5z5 wrote:
>>>>> This may be what you want:
>>>>>
>>>>>
>>>>>
>>>>> See
>>>>>
>>>>> https://github.com/SitePen/dgrid/blob/v1.0.0/doc/usage/Working-with-Events.md
>>>>>
>>>>> Brian
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> View this message in context: http://dojo-toolkit.33424.n3.nabble.com/getting-dstore-dgrid-to-respond-to-server-errors-tp4006737p4006750.html
>>>>> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
>>>>>
>>>

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Loading...