ArcGIS Buffer geoprocessing
ArcGIS_Buffer_Geoprocessing_VBNet\App_Code\Utility.vb
' Copyright 2011 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.
' 

Imports Microsoft.VisualBasic
Imports System
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 Shared Function CreateErrorCallbackResult(ByVal exception As System.Exception) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult
    ' Create a callback result to display an error message
    Dim jsAlertErrorMessage As String = GetJavaScriptErrorString(exception)
    Dim alertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertErrorMessage)
    Return alertCallbackResult
  End Function

  ''' <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 Shared Function GetJavaScriptErrorString(ByVal exception As System.Exception) As String
    ' Get the website's configuration file
    Dim webConfig As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath)

    ' Get the "compilation" section of the config file
    Dim compilationSection As System.Web.Configuration.CompilationSection = TryCast(webConfig.GetSection("system.web/compilation"), 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.
    Dim errorMessage As String = Nothing
    If (Not compilationSection Is Nothing) AndAlso (compilationSection.Debug) Then
      Dim stackTrace As String = exception.StackTrace.Replace("\", "\\")
      stackTrace = stackTrace.Replace(Constants.vbLf, "\n")
      stackTrace = stackTrace.Replace(Constants.vbCr, "\r")
      stackTrace = stackTrace.Replace("'", "\'")
      errorMessage = exception.Message.Replace("\", "\\")
      errorMessage = errorMessage.Replace(Constants.vbLf, "\n")
      errorMessage = errorMessage.Replace(Constants.vbCr, "\r")
      errorMessage = errorMessage.Replace("'", "\'")

      errorMessage = errorMessage & "\n\n" & stackTrace.Trim()
    Else
      errorMessage = exception.Message
    End If

    ' Create a callback result to display an error message
    Dim jsAlertException As String = "alert('" & errorMessage & "')"
    Return jsAlertException
  End Function
End Class