PDA

View Full Version : [CLOSED] Handle errors when loading store´s data



RCN
Apr 12, 2012, 7:21 PM
The following example throws an exception when Store is loading. I would like to know the correct way to handle the error for avoiding "Internal Server Error" exception.

I have some questions about the process of loading stores data

In version 2.0 the listener LoadException of the store was removed and there is just the Exception listener, is it correct?
why cant i set the Success and ErrorMessages of properties of the StoreResult (StoreResponseData)?
I would also like to know which is the correct way to handle an exception when loading the stores data.
Do i have to implement a custom class to accomplish it?




<ext:GridPanel ID="GridPanel1" runat="server" Width="700" Height="500" Title="ExtJS.com - Browse Forums">
<Store>
<ext:Store ID="Store1" runat="server" PageSize="30" RemoteSort="true" AutoLoad="true">
<Proxy>
<ext:AjaxProxy Url="/Example/Test/">
<Reader>
<ext:JsonReader Root="data" TotalProperty="total" />
</Reader>
</ext:AjaxProxy>
</Proxy>
<Model>
<ext:Model ID="Model1" runat="server">
<Fields>
<ext:ModelField Name="ID" />
<ext:ModelField Name="Name" />
</Fields>
</ext:Model>
</Model>
<Listeners>
<Exception Handler="alert('error')" />
</Listeners>
</ext:Store>
</Store>
<ColumnModel ID="ColumnModel1" runat="server">
<Columns>
<ext:Column ID="Column1" runat="server" Text="ID" DataIndex="ID" />
<ext:Column ID="Column2" runat="server" Text="Name" DataIndex="Name" />
</Columns>
</ColumnModel>
<View>
<ext:GridView ID="GridView1" runat="server" TrackOver="false" LoadMask="true">
</ext:GridView>
</View>
<BottomBar>
<ext:PagingToolbar runat="server" />
</BottomBar>
</ext:GridPanel>





public StoreResult Test(int start, int limit)
{
throw new Exception("Message");

StoreResult response = new StoreResult();

List<Person> data = new List<Person>();

Random randow = new Random();
DateTime now = DateTime.Now;

for (int i = start + 1; i <= start + limit; i++)
{
Person p = new Person()
{
ID = i,
Name = "name " + i,
};

data.Add(p);
}

response.Data = data;
response.Total = 50000;

return response;
}

Daniil
Apr 13, 2012, 12:50 PM
Hi,


1. In version 2.0 the listener LoadException of the store was removed and there is just the Exception listener, is it correct?




LoadException is already deprecated in ExtJS 3.4. So, it has been just removed in ExtJS 4.

Yes, please use Exception. By the way, Store Exception is relayed from its proxy. So, you can use proxy Exception as well.



2. why cant i set the Success and ErrorMessages of properties of the StoreResult (StoreResponseData)?


Thanks for the report. We will add.



3. I would also like to know which is the correct way to handle an exception when loading the stores data.


Please use set up Exception handler of Store or its proxy.



4. Do i have to implement a custom class to accomplish it?


I don't think you need to implement a custom class since we will add Success and ErrorMessage for the StoreResult class.

RCN
May 03, 2012, 8:57 PM
2. why cant i set the Success and ErrorMessages of properties of the StoreResult (StoreResponseData)?

is there any estimate of when this problem will be solved?

Daniil
May 04, 2012, 3:19 PM
We forgot to notice you, the Success and Message properties have been added April'13.

Though, I think there is a bug in ExtJS which I've reported here:
http://www.sencha.com/forum/showthread.php?201005

Here is the example how get it working for now. Please see the comments.

I will monitor it and notice you when ExtJS or we will come up with the permanent fix.

Example View

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.Net.MVC v2 Example</title>

<ext:ResourcePlaceHolder runat="server" Mode="ScriptFiles" />

<%--This is a temporary fix--%>
<script type="text/javascript">
Ext.data.AbstractStore.override({
onProxyException : function (proxy, response, operation) {
var error = operation.getError(),
message;

if (!Ext.isDefined(error)) {
error = Ext.decode(response.responseText).message;
}

message = Ext.isString(error) ? error : ("(" + error.status + ")" + error.statusText);

this.fireEvent("exception", proxy, response, operation);

if (Ext.net.DirectEvent.fireEvent("ajaxrequestexception", response, { "errorMessage": message }, null, null, null, null, operation) !== false) {
if (this.showWarningOnFailure !== false) {
Ext.net.DirectEvent.showFailure(response, response.responseText);
}
}
}
});
</script>
</head>
<body>
<ext:ResourceManager runat="server" />

<ext:Store runat="server" ShowWarningOnFailure="false">
<Model>
<ext:Model runat="server" />
</Model>
<Proxy>
<ext:AjaxProxy Url="/Test/GetData">
<Listeners>
<Exception Handler="var msg = Ext.decode(response.responseText).message; //when it will be fixed you will be able to use just operation.getError()
alert(msg);" />
</Listeners>
</ext:AjaxProxy>
</Proxy>
</ext:Store>
</body>
</html>


Example Controller Action

public ActionResult GetData()
{
StoreResult r = new StoreResult();
r.Message = "Hello! I am the Error.";
r.Success = false;

return r;
}

Daniil
May 07, 2012, 3:03 PM
There was an update within the thread.
http://www.sencha.com/forum/showthread.php?201005&p=796241&viewfull=1#post796241

So, you have to set up

<ext:JsonReader MessageProperty="message" ReadRecordsOnFailure="false" />

See also
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.reader.Reader-cfg-messageProperty
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.reader.Reader-cfg-readRecordsOnFailure