File: MobileErrorInfo.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (337 lines) | stat: -rw-r--r-- 12,235 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//------------------------------------------------------------------------------
// <copyright file="MobileErrorInfo.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

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
    {
        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.ContextKey"]/*' />
        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;
            }
        }


        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.this"]/*' />
        public String this[String key]
        {
            get
            {
                String s = _dictionary[key];
                return (s == null) ? String.Empty : s;
            }
            set
            {
                _dictionary[key] = value;
            }
        }

        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.Type"]/*' />
        public String Type
        {
            get
            {
                return this[_errorType];
            }
            set
            {
                this[_errorType] = value;
            }
        }

        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.Description"]/*' />
        public String Description
        {
            get
            {
                return this[_errorDescription];
            }
            set
            {
                this[_errorDescription] = value;
            }
        }

        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.MiscTitle"]/*' />
        public String MiscTitle
        {
            get
            {
                return this[_errorMiscTitle];
            }
            set
            {
                this[_errorMiscTitle] = value;
            }
        }

        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.MiscText"]/*' />
        public String MiscText
        {
            get
            {
                return this[_errorMiscText];
            }
            set
            {                  
                this[_errorMiscText] = value;
            }
        }

        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.File"]/*' />
        public String File
        {
            get
            {
                return this[_errorFile];
            }
            set
            {
                this[_errorFile] = value;
            }
        }

        /// <include file='doc\MobileErrorInfo.uex' path='docs/doc[@for="MobileErrorInfo.LineNumber"]/*' />
        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>(?'title'.*?)</title>.*?" +
                                ": </b>(?'description'.*?)<br>.*?" + 
                                "(<b>(?'misctitle'.*?): </b>(?'misctext'.*?)<br)+.*?" +
                                "(Source File:</b>(?'file'.*?)&nbsp;&nbsp; <b>Line:</b>(?'linenumber'.*?)<br)+",
                            RegexOptions.Singleline | 
                                RegexOptions.IgnoreCase | 
                                RegexOptions.CultureInvariant |
                                RegexOptions.Compiled);

                        _searchExpressions[1] = new Regex(
                            "<title>(?'title'.*?)</title>.*?" +
                                ": </b>(?'description'.*?)<br>.*?" + 
                                "(<b>(?'misctitle'.*?): </b>(?'misctext'.*?)<br)+.*?",
                            RegexOptions.Singleline | 
                                RegexOptions.IgnoreCase | 
                                RegexOptions.CultureInvariant |
                                RegexOptions.Compiled);

                        _searchExpressions[2] = new Regex(
                            "<title>(?'title'.*?)</title>.*?: </b>(?'description'.*?)<br>",
                            RegexOptions.Singleline | 
                            RegexOptions.IgnoreCase | 
                            RegexOptions.CultureInvariant |
                                RegexOptions.Compiled);

                        _searchExpressionsBuilt = true;
                    }
                }
            }
        }

        private static String TrimAndClean(String s)
        {
            return s.Replace("\r\n", " ").Trim();
        }
    }
}