ArcGIS_Buffer_Geoprocessing_CSharp\App_Code\Utility.cs
// Copyright 2010 ESRI // // All rights reserved under the copyright laws of the United States // and applicable international laws, treaties, and conventions. // // You may freely redistribute and use this sample code, with or // without modification, provided you include the original copyright // notice and use restrictions. // // See the use restrictions. // public class Utility { /// <summary> /// Constructs a callback result that will display a javascript alert with an error message /// based on the passed-in exception /// </summary> /// <param name="exception">The exception from which the error message will be derived</param> /// <returns></returns> public static ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult CreateErrorCallbackResult( System.Exception exception) { // Create a callback result to display an error message string jsAlertErrorMessage = GetJavaScriptErrorString(exception); ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult alertCallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertErrorMessage); return alertCallbackResult; } /// <summary> /// Constructs the syntax to display a javascript alert with an error message based on the /// passed-in exception /// </summary> /// <param name="exception">The exception from which the error message will be derived</param> /// <returns></returns> public static string GetJavaScriptErrorString(System.Exception exception) { // Get the website's configuration file System.Configuration.Configuration webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( System.Web.HttpContext.Current.Request.ApplicationPath); // Get the "compilation" section of the config file System.Web.Configuration.CompilationSection compilationSection = webConfig.GetSection("system.web/compilation") as System.Web.Configuration.CompilationSection; // If the config file's compilation section specifies debug mode, include // stack trace information in the error message. Otherwise, just return // the exception message. string errorMessage = null; if ((compilationSection != null) && (compilationSection.Debug)) { string stackTrace = exception.StackTrace.Replace("\\", "\\\\"); stackTrace = stackTrace.Replace("\n", "\\n"); stackTrace = stackTrace.Replace("\r", "\\r"); stackTrace = stackTrace.Replace("'", "\\'"); errorMessage = exception.Message.Replace("\\", "\\\\"); errorMessage = errorMessage.Replace("\n", "\\n"); errorMessage = errorMessage.Replace("\r", "\\r"); errorMessage = errorMessage.Replace("'", "\\'"); errorMessage = errorMessage + "\\n\\n" + stackTrace.Trim(); } else errorMessage = exception.Message; // Create a callback result to display an error message string jsAlertException = "alert('" + errorMessage + "')"; return jsAlertException; } }