Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / MIT / System / Web / Mobile / MobileErrorInfo.cs / 1305376 / MobileErrorInfo.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- using System; using System.CodeDom.Compiler; using System.Collections; using System.Collections.Specialized; using System.Globalization; using System.Text.RegularExpressions; using System.Security.Permissions; namespace System.Web.Mobile { /* * Mobile Error Info * Contains information about an error that occurs in a mobile application. * This information can be used to format the error for the target device. * * */ [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)] [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")] public class MobileErrorInfo { ///public static readonly String ContextKey = "MobileErrorInfo"; private static object _lockObject = new object(); private const String _errorType = "Type"; private const String _errorDescription = "Description"; private const String _errorMiscTitle = "MiscTitle"; private const String _errorMiscText = "MiscText"; private const String _errorFile = "File"; private const String _errorLineNumber = "LineNumber"; private static Regex[] _searchExpressions = null; private static bool _searchExpressionsBuilt = false; private const int _expressionCount = 3; private StringDictionary _dictionary = new StringDictionary(); internal MobileErrorInfo(Exception e) { // Don't want any failure to escape here... try { // For some reason, the compile exception lives in the // InnerException. HttpCompileException compileException = e.InnerException as HttpCompileException; if (compileException != null) { this.Type = SR.GetString(SR.MobileErrorInfo_CompilationErrorType); this.Description = SR.GetString(SR.MobileErrorInfo_CompilationErrorDescription); this.MiscTitle = SR.GetString(SR.MobileErrorInfo_CompilationErrorMiscTitle); CompilerErrorCollection errors = compileException.Results.Errors; if (errors != null && errors.Count >= 1) { CompilerError error = errors[0]; this.LineNumber = error.Line.ToString(CultureInfo.InvariantCulture); this.File = error.FileName; this.MiscText = error.ErrorNumber + ":" + error.ErrorText; } else { this.LineNumber = SR.GetString(SR.MobileErrorInfo_Unknown); this.File = SR.GetString(SR.MobileErrorInfo_Unknown); this.MiscText = SR.GetString(SR.MobileErrorInfo_Unknown); } return; } HttpParseException parseException = e as HttpParseException; if (parseException != null) { this.Type = SR.GetString(SR.MobileErrorInfo_ParserErrorType); this.Description = SR.GetString(SR.MobileErrorInfo_ParserErrorDescription); this.MiscTitle = SR.GetString(SR.MobileErrorInfo_ParserErrorMiscTitle); this.LineNumber = parseException.Line.ToString(CultureInfo.InvariantCulture); this.File = parseException.FileName; this.MiscText = parseException.Message; return; } // We try to use the hacky way of parsing an HttpException of an // unknown subclass. HttpException httpException = e as HttpException; if (httpException != null && ParseHttpException(httpException)) { return; } } catch { // Don't need to do anything here, just continue to base case // below. } // Default to the most basic if none of the above succeed. this.Type = e.GetType().FullName; this.Description = e.Message; this.MiscTitle = SR.GetString(SR.MobileErrorInfo_SourceObject); String s = e.StackTrace; if(s != null) { int i = s.IndexOf('\r'); if (i != -1) { s = s.Substring(0, i); } this.MiscText = s; } } /// public String this[String key] { get { String s = _dictionary[key]; return (s == null) ? String.Empty : s; } set { _dictionary[key] = value; } } /// public String Type { get { return this[_errorType]; } set { this[_errorType] = value; } } /// public String Description { get { return this[_errorDescription]; } set { this[_errorDescription] = value; } } /// public String MiscTitle { get { return this[_errorMiscTitle]; } set { this[_errorMiscTitle] = value; } } /// public String MiscText { get { return this[_errorMiscText]; } set { this[_errorMiscText] = value; } } /// public String File { get { return this[_errorFile]; } set { this[_errorFile] = value; } } /// public String LineNumber { get { return this[_errorLineNumber]; } set { this[_errorLineNumber] = value; } } // Return true if we succeed private bool ParseHttpException(HttpException e) { int i; Match match = null; String errorMessage = e.GetHtmlErrorMessage(); if (errorMessage == null) { return false; } // Use regular expressions to scrape the message output // for meaningful data. One problem: Some parts of the // output are optional, and any regular expression that // uses the ()? syntax doesn't pick it up. So, we have // to have all the different combinations of expressions, // and use each one in order. EnsureSearchExpressions(); for (i = 0; i < _expressionCount; i++) { match = _searchExpressions[i].Match(errorMessage); if (match.Success) { break; } } if (i == _expressionCount) { return false; } this.Type = TrimAndClean(match.Result("${title}")); this.Description = TrimAndClean(match.Result("${description}")); if (i <= 1) { // These expressions were able to match the miscellaneous // title/text section. this.MiscTitle = TrimAndClean(match.Result("${misctitle}")); this.MiscText = TrimAndClean(match.Result("${misctext}")); } if (i == 0) { // This expression was able to match the file/line # // section. this.File = TrimAndClean(match.Result("${file}")); this.LineNumber = TrimAndClean(match.Result("${linenumber}")); } return true; } private static void EnsureSearchExpressions() { // Create precompiled search expressions. They're here // rather than in static variables, so that we can load // them from resources on demand. But once they're loaded, // they're compiled and always available. if (!_searchExpressionsBuilt) { lock(_lockObject) { if (!_searchExpressionsBuilt) { // // Why three similar expressions? See ParseHttpException above. _searchExpressions = new Regex[_expressionCount]; _searchExpressions[0] = new Regex( " (?'title'.*?) .*?" + ": (?'description'.*?)
.*?" + "((?'misctitle'.*?): (?'misctext'.*?)
(?'file'.*?) Line:(?'linenumber'.*?)
(?'title'.*?).*?" + ": (?'description'.*?)
.*?" + "((?'misctitle'.*?): (?'misctext'.*?)
(?'title'.*?).*?: (?'description'.*?)
", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); _searchExpressionsBuilt = true; } } } } private static String TrimAndClean(String s) { return s.Replace("\r\n", " ").Trim(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- using System; using System.CodeDom.Compiler; using System.Collections; using System.Collections.Specialized; using System.Globalization; using System.Text.RegularExpressions; using System.Security.Permissions; namespace System.Web.Mobile { /* * Mobile Error Info * Contains information about an error that occurs in a mobile application. * This information can be used to format the error for the target device. * * */ [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)] [Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")] public class MobileErrorInfo { ///public static readonly String ContextKey = "MobileErrorInfo"; private static object _lockObject = new object(); private const String _errorType = "Type"; private const String _errorDescription = "Description"; private const String _errorMiscTitle = "MiscTitle"; private const String _errorMiscText = "MiscText"; private const String _errorFile = "File"; private const String _errorLineNumber = "LineNumber"; private static Regex[] _searchExpressions = null; private static bool _searchExpressionsBuilt = false; private const int _expressionCount = 3; private StringDictionary _dictionary = new StringDictionary(); internal MobileErrorInfo(Exception e) { // Don't want any failure to escape here... try { // For some reason, the compile exception lives in the // InnerException. HttpCompileException compileException = e.InnerException as HttpCompileException; if (compileException != null) { this.Type = SR.GetString(SR.MobileErrorInfo_CompilationErrorType); this.Description = SR.GetString(SR.MobileErrorInfo_CompilationErrorDescription); this.MiscTitle = SR.GetString(SR.MobileErrorInfo_CompilationErrorMiscTitle); CompilerErrorCollection errors = compileException.Results.Errors; if (errors != null && errors.Count >= 1) { CompilerError error = errors[0]; this.LineNumber = error.Line.ToString(CultureInfo.InvariantCulture); this.File = error.FileName; this.MiscText = error.ErrorNumber + ":" + error.ErrorText; } else { this.LineNumber = SR.GetString(SR.MobileErrorInfo_Unknown); this.File = SR.GetString(SR.MobileErrorInfo_Unknown); this.MiscText = SR.GetString(SR.MobileErrorInfo_Unknown); } return; } HttpParseException parseException = e as HttpParseException; if (parseException != null) { this.Type = SR.GetString(SR.MobileErrorInfo_ParserErrorType); this.Description = SR.GetString(SR.MobileErrorInfo_ParserErrorDescription); this.MiscTitle = SR.GetString(SR.MobileErrorInfo_ParserErrorMiscTitle); this.LineNumber = parseException.Line.ToString(CultureInfo.InvariantCulture); this.File = parseException.FileName; this.MiscText = parseException.Message; return; } // We try to use the hacky way of parsing an HttpException of an // unknown subclass. HttpException httpException = e as HttpException; if (httpException != null && ParseHttpException(httpException)) { return; } } catch { // Don't need to do anything here, just continue to base case // below. } // Default to the most basic if none of the above succeed. this.Type = e.GetType().FullName; this.Description = e.Message; this.MiscTitle = SR.GetString(SR.MobileErrorInfo_SourceObject); String s = e.StackTrace; if(s != null) { int i = s.IndexOf('\r'); if (i != -1) { s = s.Substring(0, i); } this.MiscText = s; } } /// public String this[String key] { get { String s = _dictionary[key]; return (s == null) ? String.Empty : s; } set { _dictionary[key] = value; } } /// public String Type { get { return this[_errorType]; } set { this[_errorType] = value; } } /// public String Description { get { return this[_errorDescription]; } set { this[_errorDescription] = value; } } /// public String MiscTitle { get { return this[_errorMiscTitle]; } set { this[_errorMiscTitle] = value; } } /// public String MiscText { get { return this[_errorMiscText]; } set { this[_errorMiscText] = value; } } /// public String File { get { return this[_errorFile]; } set { this[_errorFile] = value; } } /// public String LineNumber { get { return this[_errorLineNumber]; } set { this[_errorLineNumber] = value; } } // Return true if we succeed private bool ParseHttpException(HttpException e) { int i; Match match = null; String errorMessage = e.GetHtmlErrorMessage(); if (errorMessage == null) { return false; } // Use regular expressions to scrape the message output // for meaningful data. One problem: Some parts of the // output are optional, and any regular expression that // uses the ()? syntax doesn't pick it up. So, we have // to have all the different combinations of expressions, // and use each one in order. EnsureSearchExpressions(); for (i = 0; i < _expressionCount; i++) { match = _searchExpressions[i].Match(errorMessage); if (match.Success) { break; } } if (i == _expressionCount) { return false; } this.Type = TrimAndClean(match.Result("${title}")); this.Description = TrimAndClean(match.Result("${description}")); if (i <= 1) { // These expressions were able to match the miscellaneous // title/text section. this.MiscTitle = TrimAndClean(match.Result("${misctitle}")); this.MiscText = TrimAndClean(match.Result("${misctext}")); } if (i == 0) { // This expression was able to match the file/line # // section. this.File = TrimAndClean(match.Result("${file}")); this.LineNumber = TrimAndClean(match.Result("${linenumber}")); } return true; } private static void EnsureSearchExpressions() { // Create precompiled search expressions. They're here // rather than in static variables, so that we can load // them from resources on demand. But once they're loaded, // they're compiled and always available. if (!_searchExpressionsBuilt) { lock(_lockObject) { if (!_searchExpressionsBuilt) { // // Why three similar expressions? See ParseHttpException above. _searchExpressions = new Regex[_expressionCount]; _searchExpressions[0] = new Regex( " (?'title'.*?) .*?" + ": (?'description'.*?)
.*?" + "((?'misctitle'.*?): (?'misctext'.*?)
(?'file'.*?) Line:(?'linenumber'.*?)
(?'title'.*?).*?" + ": (?'description'.*?)
.*?" + "((?'misctitle'.*?): (?'misctext'.*?)
(?'title'.*?).*?: (?'description'.*?)
", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled); _searchExpressionsBuilt = true; } } } } private static String TrimAndClean(String s) { return s.Replace("\r\n", " ").Trim(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- JoinElimination.cs
- RedirectionProxy.cs
- ToolStripDesignerUtils.cs
- SystemIPGlobalProperties.cs
- DocumentGrid.cs
- MachineSettingsSection.cs
- Resources.Designer.cs
- XmlByteStreamWriter.cs
- TimeZoneInfo.cs
- ConfigXmlAttribute.cs
- IconBitmapDecoder.cs
- RequestCachePolicyConverter.cs
- ResourceManager.cs
- BitVec.cs
- XhtmlBasicListAdapter.cs
- WindowCollection.cs
- ComplusTypeValidator.cs
- CharacterString.cs
- SettingsAttributes.cs
- LogStore.cs
- x509utils.cs
- SiteMapDataSource.cs
- TypeInitializationException.cs
- SimpleRecyclingCache.cs
- MembershipPasswordException.cs
- XsltException.cs
- SQLRoleProvider.cs
- EntityContainerAssociationSetEnd.cs
- TypeUsageBuilder.cs
- CompilerResults.cs
- sqlcontext.cs
- ConfigurationStrings.cs
- DesignTimeXamlWriter.cs
- StateDesigner.CommentLayoutGlyph.cs
- ConfigurationValidatorBase.cs
- XmlElementAttribute.cs
- ServiceHostFactory.cs
- WebBrowserNavigatingEventHandler.cs
- XPathEmptyIterator.cs
- MappedMetaModel.cs
- DataGridDefaultColumnWidthTypeConverter.cs
- COM2EnumConverter.cs
- FastEncoderWindow.cs
- DefaultProxySection.cs
- GridViewRow.cs
- EllipseGeometry.cs
- Vector3DConverter.cs
- Byte.cs
- LambdaReference.cs
- HostVisual.cs
- TextShapeableCharacters.cs
- SequenceDesigner.cs
- BufferModeSettings.cs
- DropShadowEffect.cs
- SessionStateModule.cs
- DataServiceHostFactory.cs
- BindingCollection.cs
- SQLMembershipProvider.cs
- COM2PropertyBuilderUITypeEditor.cs
- HebrewNumber.cs
- CompareValidator.cs
- FrameworkElementAutomationPeer.cs
- DateTimeStorage.cs
- RegexMatch.cs
- CustomPopupPlacement.cs
- BuildResult.cs
- SQLMoney.cs
- ChannelPoolSettings.cs
- RenderDataDrawingContext.cs
- HttpGetProtocolImporter.cs
- LayoutEngine.cs
- Assembly.cs
- SortedDictionary.cs
- ResourcesBuildProvider.cs
- _ConnectionGroup.cs
- FileChangesMonitor.cs
- RefType.cs
- XmlQualifiedNameTest.cs
- BrowserPolicyValidator.cs
- HttpContext.cs
- ScrollBar.cs
- ObjectDisposedException.cs
- Version.cs
- MetadataException.cs
- cryptoapiTransform.cs
- MediaPlayer.cs
- StringAnimationBase.cs
- XmlDocument.cs
- Localizer.cs
- AuthenticationModulesSection.cs
- ProfileGroupSettings.cs
- TdsParserSafeHandles.cs
- TextEndOfLine.cs
- SetterBaseCollection.cs
- DoubleAnimationUsingPath.cs
- DataGridViewCellValidatingEventArgs.cs
- ModelProperty.cs
- SystemIPGlobalProperties.cs
- ExpressionsCollectionConverter.cs
- FixedSOMLineCollection.cs