Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localize exception message in pages. #7321

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars;
using Volo.Abp.AutoMapper;
using Volo.Abp.ExceptionHandling;
using Volo.Abp.Identity.AspNetCore;
using Volo.Abp.Modularity;
using Volo.Abp.UI.Navigation;
Expand All @@ -19,7 +20,8 @@ namespace Volo.Abp.Account.Web
typeof(AbpAccountHttpApiModule),
typeof(AbpIdentityAspNetCoreModule),
typeof(AbpAutoMapperModule),
typeof(AbpAspNetCoreMvcUiThemeSharedModule)
typeof(AbpAspNetCoreMvcUiThemeSharedModule),
typeof(AbpExceptionHandlingModule)
)]
public class AbpAccountWebModule : AbpModule
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Volo.Abp.Account.Localization;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Abp.ExceptionHandling;
using Volo.Abp.Identity;
using IdentityUser = Volo.Abp.Identity.IdentityUser;

Expand All @@ -18,6 +19,7 @@ public abstract class AccountPageModel : AbpPageModel
public IdentityUserManager UserManager { get; set; }
public IdentitySecurityLogManager IdentitySecurityLogManager { get; set; }
public IOptions<IdentityOptions> IdentityOptions { get; set; }
public IExceptionToErrorInfoConverter ExceptionToErrorInfoConverter { get; set; }

protected AccountPageModel()
{
Expand All @@ -42,5 +44,15 @@ protected virtual void CheckCurrentTenant(Guid? tenantId)
throw new ApplicationException($"Current tenant is different than given tenant. CurrentTenant.Id: {CurrentTenant.Id}, given tenantId: {tenantId}");
}
}

protected virtual string GetLocalizeExceptionMessage(Exception exception)
{
if (exception is ILocalizeErrorMessage || exception is IHasErrorCode)
{
return ExceptionToErrorInfoConverter.Convert(exception, false).Message;
}

return exception.Message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public virtual async Task<IActionResult> OnPostAsync()
}
catch (UserFriendlyException e)
{
Alerts.Danger(e.Message);
Alerts.Danger(GetLocalizeExceptionMessage(e));
return Page();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -100,7 +98,7 @@ public virtual async Task<IActionResult> OnPostAsync()
}
catch (BusinessException e)
{
Alerts.Danger(e.Message);
Alerts.Danger(GetLocalizeExceptionMessage(e));
return Page();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public virtual async Task<IActionResult> OnPostAsync()
{
if (!string.IsNullOrWhiteSpace(e.Message))
{
Alerts.Warning(e.Message);
Alerts.Warning(GetLocalizeExceptionMessage(e));
return Page();
}

Expand Down