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)
-
0
Hi @MichaelM,
You can send all exceptions to client like this
public override void PreInitialize() { Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true; }
-
0
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)); });
-
0
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).
-
0
You guys Rock!
-
0
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,
-
0
Did you remove SendAllExceptionsToClients = true?
-
0
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".
-
0
Set its order:
options.Filters.AddService(typeof(ExceptionFilter), order: 1);
-
0
Great! It does the trick! Thank you very much!!!
-
0
thnx for the feedback ;)