/// <summary>
        /// Handles an exception that occurs. First, the error is recorded. Certain types, such as security exceptions and directory permission
        /// errors, are are rendered to the user with user-friendly text. For other exceptions, a generic message is displayed, unless
        /// the system is configured to show detailed error messages (showErrorDetails="true" in galleryserverpro.config), in which
        /// case full details about the exception is displayed. If the user has disabled the exception handler
        /// (enableExceptionHandler="false"), then the error is recorded but no other action is taken. This allows
        /// global error handling in web.config or global.asax to deal with it.
        /// </summary>
        /// <param name="ex">The exception.</param>
        public static void HandleGalleryException(Exception ex)
        {
            try
            {
                LogError(ex);
            }
            catch (Exception errHandlingEx)
            {
                if (!ex.Data.Contains("Error Handling Exception"))
                {
                    ex.Data.Add("Error Handling Exception", String.Format("The following error occurred while handling the exception: {0} - {1} Stack trace: {2}", errHandlingEx.GetType(), errHandlingEx.Message, errHandlingEx.StackTrace));
                }
            }

            // If the error is security related, go to a special page that offers a friendly error message.
            if (ex is ErrorHandler.CustomExceptions.GallerySecurityException)
            {
                // User is not allowed to access the requested page. Redirect to home page.
                HttpContext.Current.Server.ClearError();
                Util.Redirect(PageId.album);
            }
            else if (ex is ErrorHandler.CustomExceptions.CannotWriteToDirectoryException)
            {
                // Gallery Server cannot write to a directory. Application startup code checks for this condition,
                // so we'll get here most often when Gallery Server is first configured and the required permissions were not given.
                // Provide friendly, customized message to help the user resolve the situation.
                HttpContext.Current.Server.ClearError();
                HttpContext.Current.Items["CurrentException"] = ex;
                Util.Transfer(PageId.error_cannotwritetodirectory);
            }
            else if (ex is ErrorHandler.CustomExceptions.InvalidLicenseException)
            {
                // Gallery Server has determined the current request cannot be completed because it would violate the licensing rules.
                // Redirect to a friendly page to explain the situation.
                HttpContext.Current.Server.ClearError();
                HttpContext.Current.Items["CurrentException"] = ex;
                Util.Transfer(PageId.error_invalidlicense);
            }
            else
            {
                // An unexpected exception is happening.
                // If Gallery Server's exception handling is enabled, clear the error and display the relevant error message.
                // Otherwise, don't do anything, which lets it propogate up the stack, thus allowing for error handling code in
                // global.asax and/or web.config (e.g. <customErrors...> or some other global error handler) to handle it.
                if (ConfigManager.GetGalleryServerProConfigSection().Core.EnableExceptionHandler)
                {
                    // Redirect to generic error page.
                    HttpContext.Current.Server.ClearError();
                    HttpContext.Current.Items["CurrentAppError"] = AppError.Create(ex);
                    Util.Transfer(PageId.error_generic);
                }
            }
        }
Exemple #2
0
        public void OnException(ExceptionContext context)
        {
            var appError = AppError.Create(
                context.HttpContext.Request.Path,
                context.Exception.StackTrace,
                context.Exception.Message,
                context.Exception.InnerException?.Message,
                context.HttpContext.Request.Method,
                _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(),
                context.HttpContext.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value
                );

            _applicationDbContext.AppErrors.Add(appError);
            _applicationDbContext.SaveChanges();
        }
Exemple #3
0
        /// <summary>
        /// Handles an <paramref name="ex" /> that occurred in the gallery with ID = <paramref name="galleryId" />.
        /// </summary>
        /// <param name="ex">The exception to handle.</param>
        /// <param name="galleryId">The ID of the gallery the <paramref name="ex">exception</paramref> is associated with. If the
        /// ID is unknown, use <see cref="Int32.MinValue" />.</param>
        public static void HandleGalleryException(Exception ex, int galleryId)
        {
            if (ex == null)
            {
                return;
            }

            try
            {
                LogError(ex, galleryId);
            }
            catch (Exception errHandlingEx)
            {
                if (!ex.Data.Contains("Error Handling Exception"))
                {
                    ex.Data.Add("Error Handling Exception", String.Format(CultureInfo.CurrentCulture, "The function HandleGalleryException experienced the following error while trying to log an error: {0} - {1} Stack trace: {2}", errHandlingEx.GetType(), errHandlingEx.Message, errHandlingEx.StackTrace));
                }
            }

            // If the error is security related, go to a special page that offers a friendly error message.
            if (ex is ErrorHandler.CustomExceptions.GallerySecurityException)
            {
                // User is not allowed to access the requested page. Redirect to home page.
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Server.ClearError();
                }

                Utils.Redirect(PageId.album);
            }
            else if (ex is ErrorHandler.CustomExceptions.CannotWriteToDirectoryException)
            {
                // Gallery Server cannot write to a directory. Application startup code checks for this condition,
                // so we'll get here most often when Gallery Server is first configured and the required permissions were not given.
                // Provide friendly, customized message to help the user resolve the situation.
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Server.ClearError();
                    HttpContext.Current.Items["CurrentException"] = ex;
                }

                Utils.Transfer(PageId.error_cannotwritetodirectory);
            }
            else
            {
                // An unexpected exception is happening.
                // If Gallery Server's exception handling is enabled, clear the error and display the relevant error message.
                // Otherwise, don't do anything, which lets it propagate up the stack, thus allowing for error handling code in
                // global.asax and/or web.config (e.g. <customErrors...> or some other global error handler) to handle it.
                bool enableExceptionHandler = false;
                try
                {
                    if (galleryId > Int32.MinValue)
                    {
                        enableExceptionHandler = Factory.LoadGallerySetting(galleryId).EnableExceptionHandler;
                    }
                }
                catch { }

                if (enableExceptionHandler)
                {
                    // Redirect to generic error page.
                    if (HttpContext.Current != null)
                    {
                        HttpContext.Current.Server.ClearError();
                        HttpContext.Current.Items["CurrentAppError"] = AppError.Create(ex, galleryId);
                    }

                    Utils.Transfer(PageId.error_generic);
                }
            }
        }