Error handling in blazor UI

pull/8074/head
Halil İbrahim Kalkan 5 years ago
parent b17eb8e9cd
commit e377beaa7b

@ -140,8 +140,9 @@ namespace Volo.Abp.AspNetCore.Components
return localizer;
}
protected async Task ShowError(Exception exception)
protected async Task HandleErrorAsync(Exception exception)
{
Logger.LogException(exception);
await InvokeAsync(async () =>
{
await UserExceptionInformer.InformAsync(new UserExceptionInformerContext(exception));

@ -227,10 +227,17 @@ namespace Volo.Abp.BlazoriseUI
protected virtual async Task GetEntitiesAsync()
{
await UpdateGetListInputAsync();
var result = await AppService.GetListAsync(GetListInput);
Entities = MapToListViewModel(result.Items);
TotalCount = (int?)result.TotalCount;
try
{
await UpdateGetListInputAsync();
var result = await AppService.GetListAsync(GetListInput);
Entities = MapToListViewModel(result.Items);
TotalCount = (int?)result.TotalCount;
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
private IReadOnlyList<TListViewModel> MapToListViewModel(IReadOnlyList<TGetListOutputDto> dtos)
@ -287,20 +294,26 @@ namespace Volo.Abp.BlazoriseUI
protected virtual async Task OpenCreateModalAsync()
{
CreateValidationsRef?.ClearAll();
try
{
CreateValidationsRef?.ClearAll();
await CheckCreatePolicyAsync();
await CheckCreatePolicyAsync();
NewEntity = new TCreateViewModel();
NewEntity = new TCreateViewModel();
// Mapper will not notify Blazor that binded values are changed
// so we need to notify it manually by calling StateHasChanged
await InvokeAsync(() =>
// Mapper will not notify Blazor that binded values are changed
// so we need to notify it manually by calling StateHasChanged
await InvokeAsync(() =>
{
StateHasChanged();
CreateModal?.Show();
});
}
catch (Exception ex)
{
StateHasChanged();
CreateModal?.Show();
});
await HandleErrorAsync(ex);
}
}
protected virtual Task CloseCreateModalAsync()
@ -310,21 +323,27 @@ namespace Volo.Abp.BlazoriseUI
protected virtual async Task OpenEditModalAsync(TListViewModel entity)
{
EditValidationsRef?.ClearAll();
await CheckUpdatePolicyAsync();
try
{
EditValidationsRef?.ClearAll();
await CheckUpdatePolicyAsync();
var entityDto = await AppService.GetAsync(entity.Id);
var entityDto = await AppService.GetAsync(entity.Id);
EditingEntityId = entity.Id;
EditingEntity = MapToEditingEntity(entityDto);
EditingEntityId = entity.Id;
EditingEntity = MapToEditingEntity(entityDto);
await InvokeAsync(() =>
await InvokeAsync(() =>
{
StateHasChanged();
EditModal?.Show();
});
}
catch (Exception ex)
{
StateHasChanged();
EditModal?.Show();
});
await HandleErrorAsync(ex);
}
}
protected virtual TUpdateViewModel MapToEditingEntity(TGetOutputDto entityDto)
@ -360,15 +379,22 @@ namespace Volo.Abp.BlazoriseUI
protected virtual async Task CreateEntityAsync()
{
if (CreateValidationsRef?.ValidateAll() ?? true)
try
{
await OnCreatingEntityAsync();
if (CreateValidationsRef?.ValidateAll() ?? true)
{
await OnCreatingEntityAsync();
await CheckCreatePolicyAsync();
var createInput = MapToCreateInput(NewEntity);
await AppService.CreateAsync(createInput);
await CheckCreatePolicyAsync();
var createInput = MapToCreateInput(NewEntity);
await AppService.CreateAsync(createInput);
await OnCreatedEntityAsync();
await OnCreatedEntityAsync();
}
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
@ -386,15 +412,22 @@ namespace Volo.Abp.BlazoriseUI
protected virtual async Task UpdateEntityAsync()
{
if (EditValidationsRef?.ValidateAll() ?? true)
try
{
await OnUpdatingEntityAsync();
if (EditValidationsRef?.ValidateAll() ?? true)
{
await OnUpdatingEntityAsync();
await CheckUpdatePolicyAsync();
var updateInput = MapToUpdateInput(EditingEntity);
await AppService.UpdateAsync(EditingEntityId, updateInput);
await CheckUpdatePolicyAsync();
var updateInput = MapToUpdateInput(EditingEntity);
await AppService.UpdateAsync(EditingEntityId, updateInput);
await OnUpdatedEntityAsync();
await OnUpdatedEntityAsync();
}
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
@ -421,7 +454,7 @@ namespace Volo.Abp.BlazoriseUI
}
catch (Exception ex)
{
await ShowError(ex);
await HandleErrorAsync(ex);
}
}

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blazorise;
@ -38,33 +39,40 @@ namespace Volo.Abp.FeatureManagement.Blazor.Components
public virtual async Task OpenAsync([NotNull]string providerName, string providerKey = null)
{
ProviderName = providerName;
ProviderKey = providerKey;
try
{
ProviderName = providerName;
ProviderKey = providerKey;
ToggleValues = new Dictionary<string, bool>();
SelectionStringValues = new Dictionary<string, string>();
ToggleValues = new Dictionary<string, bool>();
SelectionStringValues = new Dictionary<string, string>();
Groups = (await FeatureAppService.GetAsync(ProviderName, ProviderKey)).Groups;
Groups = (await FeatureAppService.GetAsync(ProviderName, ProviderKey)).Groups;
SelectedTabName = GetNormalizedGroupName(Groups.First().Name);
SelectedTabName = GetNormalizedGroupName(Groups.First().Name);
foreach (var featureGroupDto in Groups)
{
foreach (var featureDto in featureGroupDto.Features)
foreach (var featureGroupDto in Groups)
{
if (featureDto.ValueType is ToggleStringValueType)
foreach (var featureDto in featureGroupDto.Features)
{
ToggleValues.Add(featureDto.Name, bool.Parse(featureDto.Value));
}
if (featureDto.ValueType is SelectionStringValueType)
{
SelectionStringValues.Add(featureDto.Name, featureDto.Value);
if (featureDto.ValueType is ToggleStringValueType)
{
ToggleValues.Add(featureDto.Name, bool.Parse(featureDto.Value));
}
if (featureDto.ValueType is SelectionStringValueType)
{
SelectionStringValues.Add(featureDto.Name, featureDto.Value);
}
}
}
}
await InvokeAsync(Modal.Show);
await InvokeAsync(Modal.Show);
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
public virtual Task CloseModal()
@ -74,19 +82,26 @@ namespace Volo.Abp.FeatureManagement.Blazor.Components
protected virtual async Task SaveAsync()
{
var features = new UpdateFeaturesDto
try
{
Features = Groups.SelectMany(g => g.Features).Select(f => new UpdateFeatureDto
var features = new UpdateFeaturesDto
{
Name = f.Name,
Value = f.ValueType is ToggleStringValueType ? ToggleValues[f.Name].ToString() :
Features = Groups.SelectMany(g => g.Features).Select(f => new UpdateFeatureDto
{
Name = f.Name,
Value = f.ValueType is ToggleStringValueType ? ToggleValues[f.Name].ToString() :
f.ValueType is SelectionStringValueType ? SelectionStringValues[f.Name] : f.Value
}).ToList()
};
}).ToList()
};
await FeatureAppService.UpdateAsync(ProviderName, ProviderKey, features);
await FeatureAppService.UpdateAsync(ProviderName, ProviderKey, features);
await InvokeAsync(Modal.Hide);
await InvokeAsync(Modal.Hide);
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
protected virtual string GetNormalizedGroupName(string name)

@ -46,7 +46,14 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity
{
await base.OnInitializedAsync();
Roles = (await AppService.GetAssignableRolesAsync()).Items;
try
{
Roles = (await AppService.GetAssignableRolesAsync()).Items;
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
protected override async Task SetPermissionsAsync()
@ -79,17 +86,24 @@ namespace Volo.Abp.Identity.Blazor.Pages.Identity
protected override async Task OpenEditModalAsync(IdentityUserDto entity)
{
EditModalSelectedTab = DefaultSelectedTab;
try
{
EditModalSelectedTab = DefaultSelectedTab;
var userRoleNames = (await AppService.GetRolesAsync(entity.Id)).Items.Select(r => r.Name).ToList();
var userRoleNames = (await AppService.GetRolesAsync(entity.Id)).Items.Select(r => r.Name).ToList();
EditUserRoles = Roles.Select(x => new AssignedRoleViewModel
{
Name = x.Name,
IsAssigned = userRoleNames.Contains(x.Name)
}).ToArray();
EditUserRoles = Roles.Select(x => new AssignedRoleViewModel
{
Name = x.Name,
IsAssigned = userRoleNames.Contains(x.Name)
}).ToArray();
await base.OpenEditModalAsync(entity);
await base.OpenEditModalAsync(entity);
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
protected override Task OnUpdatingEntityAsync()

@ -1,6 +1,6 @@
@using Microsoft.Extensions.Localization
@using Volo.Abp.PermissionManagement.Localization
@inject IStringLocalizer<AbpPermissionManagementResource> L
@inherits Volo.Abp.AspNetCore.Components.AbpComponentBase
<Modal @ref="_modal">
<ModalBackdrop />
<ModalContent Size="ModalSize.Large" IsCentered="true">

@ -1,8 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blazorise;
using Microsoft.AspNetCore.Components;
using Volo.Abp.PermissionManagement.Localization;
namespace Volo.Abp.PermissionManagement.Blazor.Components
{
@ -65,39 +67,51 @@ namespace Volo.Abp.PermissionManagement.Blazor.Components
}
}
public PermissionManagementModal()
{
LocalizationResource = typeof(AbpPermissionManagementResource);
}
public async Task OpenAsync(string providerName, string providerKey, string entityDisplayName = null)
{
_providerName = providerName;
_providerKey = providerKey;
try
{
_providerName = providerName;
_providerKey = providerKey;
var result = await PermissionAppService.GetAsync(_providerName, _providerKey);
var result = await PermissionAppService.GetAsync(_providerName, _providerKey);
_entityDisplayName = entityDisplayName ?? result.EntityDisplayName;
_groups = result.Groups;
_entityDisplayName = entityDisplayName ?? result.EntityDisplayName;
_groups = result.Groups;
_grantedPermissionCount = 0;
_notGrantedPermissionCount = 0;
foreach (var permission in _groups.SelectMany(x => x.Permissions))
{
if (permission.IsGranted && permission.GrantedProviders.All(x => x.ProviderName != _providerName))
_grantedPermissionCount = 0;
_notGrantedPermissionCount = 0;
foreach (var permission in _groups.SelectMany(x => x.Permissions))
{
_disabledPermissions.Add(permission);
continue;
}
if (permission.IsGranted && permission.GrantedProviders.All(x => x.ProviderName != _providerName))
{
_disabledPermissions.Add(permission);
continue;
}
if (permission.IsGranted)
{
_grantedPermissionCount++;
}
else
{
_notGrantedPermissionCount++;
if (permission.IsGranted)
{
_grantedPermissionCount++;
}
else
{
_notGrantedPermissionCount++;
}
}
}
_selectedTabName = GetNormalizedGroupName(_groups.First().Name);
_selectedTabName = GetNormalizedGroupName(_groups.First().Name);
await InvokeAsync(_modal.Show);
await InvokeAsync(_modal.Show);
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
private Task CloseModal()
@ -107,17 +121,24 @@ namespace Volo.Abp.PermissionManagement.Blazor.Components
private async Task SaveAsync()
{
var updateDto = new UpdatePermissionsDto
try
{
Permissions = _groups
.SelectMany(g => g.Permissions)
.Select(p => new UpdatePermissionDto { IsGranted = p.IsGranted, Name = p.Name })
.ToArray()
};
var updateDto = new UpdatePermissionsDto
{
Permissions = _groups
.SelectMany(g => g.Permissions)
.Select(p => new UpdatePermissionDto { IsGranted = p.IsGranted, Name = p.Name })
.ToArray()
};
await PermissionAppService.UpdateAsync(_providerName, _providerKey, updateDto);
await PermissionAppService.UpdateAsync(_providerName, _providerKey, updateDto);
await InvokeAsync(_modal.Hide);
await InvokeAsync(_modal.Hide);
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
private string GetNormalizedGroupName(string name)

@ -1,4 +1,5 @@
@using Volo.Abp.SettingManagement.Localization
@inherits Volo.Abp.AspNetCore.Components.AbpComponentBase
@inject AbpBlazorMessageLocalizerHelper<AbpSettingManagementResource> LH
@if (EmailSettings != null)

@ -1,9 +1,8 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Blazorise;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Volo.Abp.AspNetCore.Components.Messages;
using Volo.Abp.ObjectMapping;
using Volo.Abp.SettingManagement.Localization;
namespace Volo.Abp.SettingManagement.Blazor.Pages.SettingManagement.EmailSettingGroup
@ -16,26 +15,40 @@ namespace Volo.Abp.SettingManagement.Blazor.Pages.SettingManagement.EmailSetting
[Inject]
protected IUiMessageService UiMessageService { get; set; }
[Inject]
protected IStringLocalizer<AbpSettingManagementResource> L { get; set; }
[Inject]
protected IObjectMapper ObjectMapper { get; set; }
protected EmailSettingsDto EmailSettings;
protected Validations IdentitySettingValidation;
public EmailSettingGroupViewComponent()
{
ObjectMapperContext = typeof(AbpSettingManagementBlazorModule);
LocalizationResource = typeof(AbpSettingManagementResource);
}
protected override async Task OnInitializedAsync()
{
EmailSettings = await EmailSettingsAppService.GetAsync();
try
{
EmailSettings = await EmailSettingsAppService.GetAsync();
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
protected virtual async Task UpdateSettingsAsync()
{
await EmailSettingsAppService.UpdateAsync(ObjectMapper.Map<EmailSettingsDto, UpdateEmailSettingsDto>(EmailSettings));
await UiMessageService.Success(L["SuccessfullySaved"]);
try
{
await EmailSettingsAppService.UpdateAsync(ObjectMapper.Map<EmailSettingsDto, UpdateEmailSettingsDto>(EmailSettings));
await UiMessageService.Success(L["SuccessfullySaved"]);
}
catch (Exception ex)
{
await HandleErrorAsync(ex);
}
}
}
}

@ -1,9 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Blazorise;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.BlazoriseUI;
using Volo.Abp.FeatureManagement.Blazor.Components;
using Volo.Abp.TenantManagement.Localization;
@ -18,8 +14,6 @@ namespace Volo.Abp.TenantManagement.Blazor.Pages.TenantManagement
protected FeatureManagementModal FeatureManagementModal;
protected TenantInfoModel TenantInfo;
public TenantManagement()
{
LocalizationResource = typeof(AbpTenantManagementResource);
@ -30,8 +24,6 @@ namespace Volo.Abp.TenantManagement.Blazor.Pages.TenantManagement
DeletePolicyName = TenantManagementPermissions.Tenants.Delete;
ManageFeaturesPolicyName = TenantManagementPermissions.Tenants.ManageFeatures;
TenantInfo = new TenantInfoModel();
}
protected override async Task SetPermissionsAsync()
@ -46,9 +38,4 @@ namespace Volo.Abp.TenantManagement.Blazor.Pages.TenantManagement
return string.Format(L["TenantDeletionConfirmationMessage"], entity.Name);
}
}
public class TenantInfoModel
{
public Guid Id { get; set; }
}
}

Loading…
Cancel
Save