Decode ASP.NET WEB API exception to user frendly message

  1. #1

    Decode ASP.NET WEB API exception to user frendly message

    I am using EXT.NET to communicate with ASP.NET WinForms WEB API server.
    From time to time my WEB API server throw some exceptions.
    How I can parse these messages so users can read message.

    Here is how I make call to web server

    <ext:Button ID="adoDugme" runat="server" Text="klikniMe" Icon="CarStart">
        <DirectEvents>
            <Click 
                  
                  Url="~/desktopmodules/parkingKartice/api/op/SaveModel"
                   Success =" alert(Ext.encode(result));"
                    Failure="alert(Ext.decode(result).ExceptionMessage)"    
                    Type="Load" 
                    Method="POST" 
                    
                    Json="true" >
                <ExtraParams>
                    <ext:Parameter Name="values" Value="Ext.encode(#{grid}.getRowsValues({dirtyRowsOnly : true}))" Mode="Raw"></ext:Parameter>
                </ExtraParams>
                </Click>
        </DirectEvents>
    
    </ext:Button>
    Here is what I get when I fall into exception

    Click image for larger version. 

Name:	googleChromeCapture.PNG 
Views:	59 
Size:	59.8 KB 
ID:	9581

    If i simply comment Failure handler , I get Ext.Net error window but message is again unreadebile
  2. #2
    Hi @Admir,

    So, what do you see in an alert box?
    Failure="alert(Ext.decode(result).ExceptionMessage)"
  3. #3
    When I call

    alert(Ext.decode(result).ExceptionMessage)
    Chrome's console report exception like this
    Uncaught Ext.JSON.decode(): You're trying to decode an invalid JSON String: [object Object] 
    Ext.Error.Ext.extend.statics.raise 
    Ext.JSON.me.decode 
    Ext.net.directRequest.userFailure (index):46
    o.failure 
    Ext.apply.callback 
    Ext.define.onComplete 
    Ext.define.onStateChange 
    (anonymous function)
    Here is How Response looks like

    {"Message":"An error has occurred.","ExceptionMessage":"Nema ovdje nikoga","ExceptionType":"System.Exception","StackTrace":"   at Christoc.Modules.parkingKartice.opController.SaveModel(HttpRequestMessage request) in c:\\dnn\\DesktopModules\\parkingKartice\\opController.cs:line 67\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}
    When I passte that to json2csharp.com

    I get decoded class

    public class RootObject
    {
        public string Message { get; set; }
        public string ExceptionMessage { get; set; }
        public string ExceptionType { get; set; }
        public string StackTrace { get; set; }
    }
    Last edited by Admir; Apr 09, 2014 at 7:33 AM.
  4. #4
    I think to decode it by Ext.decode(), all double quotes have to be escaped by "\" symbol.

Similar Threads

  1. [CLOSED] Show a modal dialog when exception is caught with message (MVC)
    By pawangyanwali in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Nov 26, 2012, 6:06 AM
  2. [CLOSED] JSON Decode Exception
    By softmachine2011 in forum 2.x Legacy Premium Help
    Replies: 5
    Last Post: Sep 28, 2012, 2:58 PM
  3. html decode textarea
    By maxdiable in forum 1.x Help
    Replies: 2
    Last Post: Jan 22, 2010, 3:54 AM
  4. Replies: 2
    Last Post: Aug 31, 2009, 6:03 PM
  5. HTML Decode using XTemplate
    By rthiney in forum 1.x Help
    Replies: 2
    Last Post: Aug 11, 2009, 2:34 PM

Tags for this Thread

Posting Permissions