locked
How to handle error situation with try/catch RRS feed

  • Question

  • User-1188570427 posted

    What is the best way to handle this error message? I would like to log the error to my database, but if there is an error within the logging process, then I still want to try to capture my error and the logging of the error.  Instead of sending the user to the error page, I want to show the error message as is below. 

    We are using azure for our hosting.

                    try
                    {
                        // Error occurs here....
                    }
                    catch (Exception exception)
                    {
                        // I want to log the exception, and show an specific message to the end user. 
                        // If an error occurs in logging the exception (ex: inner exception is NULL)
                        // then I'll cause another error... and my initial error will be gone
                        // What is the best way to handle this?                    
                        using (var errorService = new ApplicationService())
                        {
                            try
                            {
                                await errorService.CreateEventLog(new EventLogCreateDTO()
                                {
                                    EventCategory = "User registration error",
    
                                    // EventDescription = string.Format("Full error message: {0}", exception.ToString()),
                                    EventDescription = string.Format("Full error message: {0}", exception.InnerException.ToString()),
                                    EventLogDate = DateTime.UtcNow,
                                    EventSourceDescription = exception.ToString(),
                                    EventType = EventLogType.Error,
                                    UserId = UserId
                                });
                            }
                            catch (Exception exError)
                            {
                                // What should I do here?
                            }
                        }
    
                        ModelState.AddModelError(string.Empty, "An error has occurred registering the user. Please contact your local administrator.");
                    }

    Monday, January 27, 2020 8:05 PM

Answers

  • User475983607 posted

    I've done this and we already have it set up, but this doesn't account for the initial error that was created first. It just logs the error from not being able to log the initial error when registering. If this make sense?

    I want to know if there is a way to capture them all and throw it back so the unhandled exception call will log both errors?

    You're over thinking this... Use standard variables to reference the original exception or whatever parameters you're logging.  Add this information to an Exception type of you choosing and fire the exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 27, 2020 9:11 PM

All replies

  • User475983607 posted

    What is the best way to handle this error message? I would like to log the error to my database, but if there is an error within the logging process, then I still want to try to capture my error and the logging of the error.  Instead of sending the user to the error page, I want to show the error message as is below. 

    We are using azure for our hosting.

    Create a global exception handler in the GLobal.asax that catches unhandled exceptions.   See the standard Web Forms exception handling documentation for details.

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling

    Monday, January 27, 2020 8:41 PM
  • User-1188570427 posted

    tvb2727

    What is the best way to handle this error message? I would like to log the error to my database, but if there is an error within the logging process, then I still want to try to capture my error and the logging of the error.  Instead of sending the user to the error page, I want to show the error message as is below. 

    We are using azure for our hosting.

    Create a global exception handler in the GLobal.asax that catches unhandled exceptions.   See the standard Web Forms exception handling documentation for details.

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/aspnet-error-handling

    I've done this and we already have it set up, but this doesn't account for the initial error that was created first. It just logs the error from not being able to log the initial error when registering. If this make sense?

    I want to know if there is a way to capture them all and throw it back so the unhandled exception call will log both errors?

    Monday, January 27, 2020 9:03 PM
  • User475983607 posted

    I've done this and we already have it set up, but this doesn't account for the initial error that was created first. It just logs the error from not being able to log the initial error when registering. If this make sense?

    I want to know if there is a way to capture them all and throw it back so the unhandled exception call will log both errors?

    You're over thinking this... Use standard variables to reference the original exception or whatever parameters you're logging.  Add this information to an Exception type of you choosing and fire the exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 27, 2020 9:11 PM