@@ -18,118 +18,123 @@ public void OnException(ExceptionContext filterContext)
18
18
if ( ! filterContext . ExceptionHandled )
19
19
{
20
20
filterContext . ExceptionHandled = true ;
21
- var values = filterContext . RouteData . Values ;
22
- var Logger = Utilities . Logger ;
23
- string instCode = Convert . ToString ( values [ "institution" ] ) ;
24
- string area = Convert . ToString ( values [ "area" ] ) ;
25
- string controller = Convert . ToString ( values [ "controller" ] ) ;
26
- string action = Convert . ToString ( values [ "action" ] ) ;
27
- var urlAccessed = string . Format ( "{0}/{1}/{2}/{3}" , instCode , area , controller , action ) ;
28
- Logger . Log ( new GeneralException ( string . Format ( "Crash from {0}" , urlAccessed ) , filterContext . Exception ) ) ;
29
-
30
- bool doLogout = false ;
21
+ bool isFatal = false ;
31
22
try
32
23
{
33
- var _instCode = WebUtilities . InstitutionCode ?? Utilities . INST_DEFAULT_CODE ;
34
- if ( ! instCode . Equals ( _instCode , StringComparison . OrdinalIgnoreCase ) )
24
+ bool doLogout = false ;
25
+ var values = filterContext . RouteData . Values ;
26
+ string instCode = Convert . ToString ( values [ "institution" ] ) ;
27
+ try
35
28
{
36
- instCode = Utilities . INST_DEFAULT_CODE ;
37
- doLogout = true ;
29
+ var _instCode = WebUtilities . InstitutionCode ?? Utilities . INST_DEFAULT_CODE ;
30
+ if ( ! instCode . Equals ( _instCode , StringComparison . OrdinalIgnoreCase ) )
31
+ {
32
+ instCode = Utilities . INST_DEFAULT_CODE ;
33
+ doLogout = true ;
34
+ }
38
35
}
39
- }
40
- catch ( LogOutUserException )
41
- {
42
- doLogout = true ;
43
- }
44
- catch ( Exception ) //(GeneralException ex) when (ex.ExceptionType == ExceptionType.UnidentifiedInstitutionCode)
45
- {
46
- instCode = Utilities . INST_DEFAULT_CODE ;
47
- }
48
-
49
- // When view is not found, it usually throws
50
- //Exception Details: System.InvalidOperationException:
51
- // The view '~/Views/my-category/my-article-with-long-name.aspx' or its master could not be found. The following locations were searched: ~/Views/my-category/my-article-with-long-name.aspx
52
- if ( filterContext . Exception is InvalidOperationException && filterContext . Exception . Message . Contains ( "The view '~/Views" ) )
53
- {
54
- Logger . Log ( filterContext . Exception , true ) ;
55
- filterContext . Result = MvcUtility . GetPageResult ( "ViewNotFound" , "Error" , "" , instCode ) ;
56
- return ;
57
- }
58
-
59
- var genEx = filterContext . Exception as GeneralException ;
60
- if ( genEx != null )
61
- {
62
- if ( genEx . ExceptionType == ExceptionType . UnidentifiedInstitutionCode )
36
+ catch ( LogOutUserException )
63
37
{
64
- instCode = Utilities . INST_DEFAULT_CODE ;
65
- filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "Invalid Url. Please cross-check." , controller , action )
66
- {
67
- ErrorType = ExceptionType . UnidentifiedInstitutionCode ,
68
- AreaName = area ,
69
- FromUrl = urlAccessed ,
70
- ResponseCode = HttpStatusCode . NotFound ,
71
- } ;
38
+ doLogout = true ;
72
39
}
73
- else if ( genEx . ExceptionType == ExceptionType . DatabaseRelated )
40
+ catch ( Exception ) //(GeneralException ex) when (ex .ExceptionType == ExceptionType.UnidentifiedInstitutionCode )
74
41
{
75
- filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "A database error has occurred. Contact the administrator" , controller , action )
76
- {
77
- ErrorType = ExceptionType . DatabaseRelated ,
78
- AreaName = area ,
79
- FromUrl = urlAccessed ,
80
- ResponseCode = HttpStatusCode . InternalServerError ,
81
- } ;
42
+ instCode = Utilities . INST_DEFAULT_CODE ;
82
43
}
83
- else
44
+
45
+ // When view is not found, it usually throws
46
+ //Exception Details: System.InvalidOperationException:
47
+ // The view '~/Views/my-category/my-article-with-long-name.aspx' or its master could not be found. The following locations were searched: ~/Views/my-category/my-article-with-long-name.aspx
48
+ if ( filterContext . Exception is InvalidOperationException && filterContext . Exception . Message . Contains ( "The view '~/Views" ) )
84
49
{
85
- filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( genEx , controller , action )
86
- {
87
- AreaName = area ,
88
- FromUrl = urlAccessed
89
- } ;
50
+ isFatal = true ;
51
+ filterContext . Result = MvcUtility . GetPageResult ( "ViewNotFound" , "Error" , "" , instCode ) ;
52
+ return ;
90
53
}
91
- filterContext . Result = MvcUtility . GetPageResult ( "Index" , "Error" , "" , instCode ) ;
92
- return ;
93
- }
94
54
95
- if ( doLogout || filterContext . Exception is LogOutUserException )
96
- {
97
- WebUtilities . LogOut ( ) ;
98
- filterContext . Result = MvcUtility . GetLoginPageResult ( instCode ) ;
99
- }
100
- else
101
- {
102
- var dbExType = typeof ( System . Data . Common . DbException ) ;
103
- if ( dbExType . IsAssignableFrom ( filterContext . Exception . GetType ( ) )
104
- || ( filterContext . Exception . GetBaseException ( ) != null && dbExType . IsAssignableFrom ( filterContext . Exception . GetBaseException ( ) . GetType ( ) ) ) )
55
+ var urlAccessed = filterContext . RequestContext . HttpContext . Request . RawUrl ; // string.Format("/{0}{1}/{2}/{3}", instCode, string.IsNullOrWhiteSpace(area) ? "" : ("/" + area), controller, action);
56
+
57
+ string area = Convert . ToString ( values [ "area" ] ) ;
58
+ string controller = Convert . ToString ( values [ "controller" ] ) ;
59
+ string action = Convert . ToString ( values [ "action" ] ) ;
60
+ var genEx = filterContext . Exception as GeneralException ;
61
+ if ( genEx != null )
105
62
{
106
- filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "A database error has occurred. Contact the administrator" , controller , action )
63
+ if ( genEx . ExceptionType == ExceptionType . UnidentifiedInstitutionCode )
107
64
{
108
- ErrorType = ExceptionType . DatabaseRelated ,
109
- AreaName = area ,
110
- FromUrl = urlAccessed ,
111
- ResponseCode = HttpStatusCode . InternalServerError ,
112
- } ;
65
+ instCode = Utilities . INST_DEFAULT_CODE ;
66
+ filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "Invalid Url. Please cross-check." , controller , action )
67
+ {
68
+ ErrorType = ExceptionType . UnidentifiedInstitutionCode ,
69
+ AreaName = area ,
70
+ FromUrl = urlAccessed ,
71
+ ResponseCode = HttpStatusCode . NotFound ,
72
+ } ;
73
+ }
74
+ else if ( genEx . ExceptionType == ExceptionType . DatabaseRelated )
75
+ {
76
+ filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "A database error has occurred. Contact the administrator" , controller , action )
77
+ {
78
+ ErrorType = ExceptionType . DatabaseRelated ,
79
+ AreaName = area ,
80
+ FromUrl = urlAccessed ,
81
+ ResponseCode = HttpStatusCode . InternalServerError ,
82
+ } ;
83
+ }
84
+ else
85
+ {
86
+ filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( genEx , controller , action )
87
+ {
88
+ AreaName = area ,
89
+ FromUrl = urlAccessed
90
+ } ;
91
+ }
92
+ filterContext . Result = MvcUtility . GetPageResult ( "Index" , "Error" , "" , instCode ) ;
93
+ return ;
113
94
}
114
- else if ( filterContext . Exception is HttpAntiForgeryException )
95
+
96
+ if ( doLogout || filterContext . Exception is LogOutUserException )
115
97
{
116
- filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "Looks like this is a cross-site request forgery. We can't find the token." , controller , action )
117
- {
118
- AreaName = area ,
119
- FromUrl = urlAccessed
120
- } ;
98
+ WebUtilities . LogOut ( ) ;
99
+ filterContext . Result = MvcUtility . GetLoginPageResult ( instCode ) ;
121
100
}
122
101
else
123
102
{
124
- filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( filterContext . Exception , controller , action )
103
+ var dbExType = typeof ( System . Data . Common . DbException ) ;
104
+ if ( dbExType . IsAssignableFrom ( filterContext . Exception . GetType ( ) )
105
+ || ( filterContext . Exception . GetBaseException ( ) != null && dbExType . IsAssignableFrom ( filterContext . Exception . GetBaseException ( ) . GetType ( ) ) ) )
125
106
{
126
- AreaName = area ,
127
- FromUrl = urlAccessed
128
- } ;
107
+ filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "A database error has occurred. Contact the administrator" , controller , action )
108
+ {
109
+ ErrorType = ExceptionType . DatabaseRelated ,
110
+ AreaName = area ,
111
+ FromUrl = urlAccessed ,
112
+ ResponseCode = HttpStatusCode . InternalServerError ,
113
+ } ;
114
+ }
115
+ else if ( filterContext . Exception is HttpAntiForgeryException )
116
+ {
117
+ filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( "Looks like this is a cross-site request forgery. We can't find the token." , controller , action )
118
+ {
119
+ AreaName = area ,
120
+ FromUrl = urlAccessed
121
+ } ;
122
+ }
123
+ else
124
+ {
125
+ filterContext . Controller . TempData [ ErrorMessageModel . ErrorMessageKey ] = new ErrorMessageModel ( filterContext . Exception , controller , action )
126
+ {
127
+ AreaName = area ,
128
+ FromUrl = urlAccessed
129
+ } ;
130
+ }
131
+ filterContext . Result = MvcUtility . GetPageResult ( "Index" , "Error" , "" , instCode ) ;
129
132
}
130
- filterContext . Result = MvcUtility . GetPageResult ( "Index" , "Error" , "" , instCode ) ;
131
133
}
132
- return ;
134
+ finally
135
+ {
136
+ Utilities . Logger . Log ( filterContext . Exception , isFatal ) ;
137
+ }
133
138
}
134
139
135
140
}
0 commit comments