views:

29

answers:

2

If I'm making a call to an ASMX web service and an exception occurs, the web service will return something like this:

{"Message":"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).","StackTrace":"   at System.Guid.TryParseGuidWithNoStyle(String guidString, GuidResult& result)\r\n   at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult& result)\r\n   at System.Guid..ctor(String g)\r\n   at System.ComponentModel.GuidConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.FormatException"}

I'd like to display this message in an alert or jQuery dialog box, but in order to do that, I want to pretty print it. Is there a JS library out there constructed for this purpose?

+1  A: 

No library that I know of, but this should get you started... save as an .html File for an example:

<html>
<head>
 <style type="text/css">
  /*Style it here*/
  .msgContainer
  {
   font-family:Arial;
   border:solid 1px black;
  }
  .msgTitle
  {
   background-color:#FBEC5D;
   height:30px;
   line-height:30px;
  }
  .msgBody
  {
   font-size:10pt;
   background-color:#E8E8E8;
  }
  .msgType
  {
   font-size:12pt;
   font-weight:bold;
   color:red;
  }
 </style>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
 <script type="text/javascript">
  //Document Ready: Everything inside this function fires after the page is loaded
  $(document).ready(function () {

   var prettyPrint = {
    EndingsToBreaks: function(str) {
     var br = '<br />';
     return str.replace( /\r\n|\r|\n/g, br );
    },

    DisplayError:function(error, divID){
     var $Dest = $('#' + divID);
     var _msg = this.EndingsToBreaks(error.Message);
     var _sTrace = this.EndingsToBreaks(error.StackTrace);
     var _sType = this.EndingsToBreaks(error.ExceptionType);
     $Dest.append("<div class='msgContainer' ><div class='msgTitle'><span class='msgType'>" + _sType + "</span>:" + _msg + "</div><div class='msgBody'>" +_sTrace+ "</div></div>");
    }
   }

   //Get your error Message
   var testError = {"Message":"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).","StackTrace":"   at System.Guid.TryParseGuidWithNoStyle(String guidString, GuidResult& result)\r\n   at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult& result)\r\n   at System.Guid..ctor(String g)\r\n   at System.ComponentModel.GuidConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.FormatException"}

   //Format & Style it...
   prettyPrint.DisplayError(testError, "dvMessage");
  });
 </script>
</head>
<body>
 <div id="dvMessage"></div>
</body>
</html>
Brandon Boone
+1  A: 

You don't need any lib just jquery ui dialog. and also its allreay pretty prented (has spaces and enters)

function show_error(json) {
   $('.error').remove();
   $(document.documentElement).append('<div class="error"><h1><b>' + json['ExceptionType'] + 
      '</b>: ' + json['Message'] + '</h1><pre class="trace">' + json['StackTrace'] + 
      '</pre></div>');
   $('.error').dialog();
}

it will put your stack trace in pre tag.

jcubic