Results 1 to 6 of 6

Thread: Error handler RESOLVED

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    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.

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    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:
    1. Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    2.         ' Fires when an error occurs
    3.         Dim objError As System.Exception = Server.GetLastError
    4.         HttpContext.Current.Application.Add("lastException", objError)
    5.     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:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.         Dim strError As String
    4.         Dim objError As System.Exception = HttpContext.Current.Application.Get("lastException")
    5.         lblTitle.Text = "Application Error!"
    6.         lblmessage.Text = "There has been an unexpected error. Administration have been informed. Please try again later."
    7.         'now send an email to the admin
    8.         Try
    9.             strError &= vbNewLine
    10.             strError &= objError.GetBaseException.Message & vbNewLine
    11.             strError &= objError.GetBaseException.StackTrace
    12.             strError &= vbNewLine & "Source : " & objError.GetBaseException.Source
    13.         Catch ex As Exception
    14.             strError = "Unable to obtain error information"
    15.         End Try
    16.  
    17.         Try
    18.             Dim ErrorUrl As String = Request.Params("aspxerrorpath").ToString
    19.             Dim strErrormsg As String
    20.             If User.Identity.IsAuthenticated Then
    21.                 strErrormsg = "An Error has occurred on xxx.co.uk, user ID: " & User.Identity.Name & " requested the page " & ErrorUrl & ". "
    22.             Else
    23.                 strErrormsg = "An Error has occurred on xxx.co.uk, An unauthenticated user requested the page " & ErrorUrl & ". "
    24.             End If
    25.             Dim ObjMM As New MailMessage
    26.             ObjMM.From = "[email protected]"
    27.             ObjMM.To = "xxx.co.uk"
    28.             ObjMM.Subject = "xxx Site Error"
    29.             ObjMM.Body = strErrormsg
    30.             ObjMM.Body &= vbNewLine & strError
    31.             ObjMM.BodyFormat = MailFormat.Text
    32.             SmtpMail.SmtpServer = "smtp.xxx.co.uk"
    33.             SmtpMail.Send(ObjMM)
    34.         Catch
    35.         End Try
    36.         FormsAuthentication.SignOut()
    37.         Session.Abandon()
    38.     End Sub
    Hopefully thats enough to get you started.

  3. #3
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    I might not have posted the question Fishcake but cheers for the answer.

    I think I'll make use of it.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309
    Thanks.

    Also few question:

    Why using
    VB Code:
    1. HttpContext.Current.Application.Add("lastException", objError)
    apart session variable?


    Why
    VB Code:
    1. FormsAuthentication.SignOut()
    2.         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 )

  5. #5
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    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:
    1. Request.Params("aspxerrorpath")
    Gives you the path the error occured on.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309
    VB Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width