Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Throwing user friendly messages #4460


User avatar
0
michaelm created

Is there a way to globally replace all non-user friendly messages with a user friendly one.

simple Example: asp.net zero throws message "An internal error occurred during request" - replace with "We could not service your request at this stage, please try again later"

This will also be needed for the 500 error messages being thrown : Example: error 500 : "An internal error occurred during your request" - we would like to replace text with something like "We could not service your request at this stage, please try again later"


10 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @MichaelM,

    You can send all exceptions to client like this

    public override void PreInitialize() 
    {
        Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
    }
    
  • User Avatar
    0
    aaron created
    Support Team

    You can implement IExceptionFilter and replace the exception:

    public class ExceptionFilter : IExceptionFilter, ITransientDependency
    {
        public void OnException(ExceptionContext context)
        {
            if (context.Exception is UserFriendlyException)
            {
                return;
            }
    
            context.Exception = new UserFriendlyException("We could not service your request at this stage, please try again later", context.Exception);
        }
    }
    

    Then, in Startup class, add the filter in ConfigureServices method:

    services.AddMvc(options =>
    {
        options.Filters.AddService(typeof(ExceptionFilter));
    });
    
  • User Avatar
    0
    alper created
    Support Team

    hi there are multiple ways of achieving this;

    One is implementing IExceptionToErrorInfoConverter class. The Convert method is the place to go! (Next method is not being used).

  • User Avatar
    0
    michaelm created

    You guys Rock!

  • User Avatar
    0
    fguo created

    This is what I am looking for! I want to return the "500 - internal error" to client during development/staging, because not all developers has right to access host server to get the log file.

    I followed ismcagdas' example to set "SendAllExceptionsToClients = true;" in SNetWebHostModule. It works, but I get the whole stacks message which is hard to read. I want to filter out some key words and convert to a brief, easy to understand message. So, I tried following suggestion of aaron and alper by adding a class into *Web.Host/Startup, but there is no different. I still get the whole stacks messages. Here is my code:

    namespace *.Web.Startup
    {
        public class ExceptionFilter : IExceptionFilter, IExceptionToErrorInfoConverter, ITransientDependency
        {
            public IExceptionToErrorInfoConverter Next { set => throw new NotImplementedException(); }
    
            public ErrorInfo Convert(Exception exception)
            {
                return new ErrorInfo(exception.Message.Substring(0, 20));
            }
    
            public void OnException(ExceptionContext context)
            {
                if (context.Exception is UserFriendlyException)
                {
                    return;
                }
    
                context.Exception = new UserFriendlyException("We could not service your request at this stage, please try again later", context.Exception);
            }
        }
    }
    
    public IServiceProvider ConfigureServices(IServiceCollection services)
            {
                //MVC
                services.AddMvc(options =>
                {
                    options.Filters.Add(new CorsAuthorizationFilterFactory(DefaultCorsPolicyName));
                    options.Filters.AddService(typeof(ExceptionFilter));
                });
    

    What did I miss?

    Thanks,

  • User Avatar
    0
    aaron created
    Support Team

    Did you remove SendAllExceptionsToClients = true?

  • User Avatar
    0
    fguo created

    Yes. I tried that, but if I removed that code, it goes back without detail message to Client side. I still only get the general message "An internal error occurred during request".

  • User Avatar
    0
    aaron created
    Support Team

    Set its order:

    options.Filters.AddService(typeof(ExceptionFilter), order: 1);
    
  • User Avatar
    0
    fguo created

    Great! It does the trick! Thank you very much!!!

  • User Avatar
    0
    alper created
    Support Team

    thnx for the feedback ;)