|
-
Jul 12th, 2004, 01:37 AM
#1
Thread Starter
Hyperactive Member
Error handler RESOLVED
Hey,
How to make nice error handler?
I need, that if error eccurs, user would be redirected to my designed page, which descripbes about error, where it coursed, how to contact administrator or event email me, descibbing this error.
This should work in all pages. I do not want to use "Try" in every statement.
Regards in advance
Last edited by Norkis; Jul 13th, 2004 at 03:06 AM.
-
Jul 12th, 2004, 04:17 AM
#2
A nice way of handling errors is as follows:
in your web.config within system.web
PHP Code:
<customErrors mode="On" defaultRedirect="errorpage.aspx" />
in global.asax.vb:
VB Code:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
Dim objError As System.Exception = Server.GetLastError
HttpContext.Current.Application.Add("lastException", objError)
End Sub
Design errorpage.aspx however you like. In this example the user isn't told what went wrong but admin is emailed detailed information on the error.
errorpage.aspx.vb
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim strError As String
Dim objError As System.Exception = HttpContext.Current.Application.Get("lastException")
lblTitle.Text = "Application Error!"
lblmessage.Text = "There has been an unexpected error. Administration have been informed. Please try again later."
'now send an email to the admin
Try
strError &= vbNewLine
strError &= objError.GetBaseException.Message & vbNewLine
strError &= objError.GetBaseException.StackTrace
strError &= vbNewLine & "Source : " & objError.GetBaseException.Source
Catch ex As Exception
strError = "Unable to obtain error information"
End Try
Try
Dim ErrorUrl As String = Request.Params("aspxerrorpath").ToString
Dim strErrormsg As String
If User.Identity.IsAuthenticated Then
strErrormsg = "An Error has occurred on xxx.co.uk, user ID: " & User.Identity.Name & " requested the page " & ErrorUrl & ". "
Else
strErrormsg = "An Error has occurred on xxx.co.uk, An unauthenticated user requested the page " & ErrorUrl & ". "
End If
Dim ObjMM As New MailMessage
ObjMM.To = "xxx.co.uk"
ObjMM.Subject = "xxx Site Error"
ObjMM.Body = strErrormsg
ObjMM.Body &= vbNewLine & strError
ObjMM.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = "smtp.xxx.co.uk"
SmtpMail.Send(ObjMM)
Catch
End Try
FormsAuthentication.SignOut()
Session.Abandon()
End Sub
Hopefully thats enough to get you started.
-
Jul 12th, 2004, 04:22 AM
#3
Fanatic Member
I might not have posted the question Fishcake but cheers for the answer.
I think I'll make use of it.
-
Jul 12th, 2004, 06:39 AM
#4
Thread Starter
Hyperactive Member
Thanks.
Also few question:
Why using
VB Code:
HttpContext.Current.Application.Add("lastException", objError)
apart session variable?
Why
VB Code:
FormsAuthentication.SignOut()
Session.Abandon()
is called?
How to get which page coursed an error and what url was (I don't really know, how default.aspx?var1=blah&var2=balh is called )
-
Jul 12th, 2004, 05:12 PM
#5
1. I originally tried to use Session variables for that purpose but couldn't get it to work and so came up with this solution. Someone here will be able to explain far better than i why this is.
2. On the site that that example is from if an error occured the user was signed out and the session abandoned, so a user had to start from the start again. Totally upto you.
3.
VB Code:
Request.Params("aspxerrorpath")
Gives you the path the error occured on.
-
Jul 13th, 2004, 03:05 AM
#6
Thread Starter
Hyperactive Member
VB Code:
Request.Params("aspxerrorpath")
return only path name, where error occurs.
To get all url, I put Request.RawUrl into variable and retrieved it, where I needed.
Thanks for help.
RESOLVED
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|