Open Closed

UserFriendlyException #1205


User avatar
0
learnabp created
  • ABP Framework version: V4.2.2
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:

I have the following code in my OnPost of a page where i am throwing a new UserFriendlyException

public virtual async Task<IActionResult> OnPostAsync()
{

    var tenant = await TenantRepository.FindByNameAsync(Tenant.Name);

    if (tenant != null)
    {

        throw new UserFriendlyException(message: "Tenant Name already exists please choose another");

    }

However the page go to a 403 Forbidden page and not shows a popup dialog, can you let me know what I am doing wrong


8 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Did you want to show the page alerts?

    https://docs.abp.io/en/abp/latest/UI/AspNetCore/Page-Alerts https://github.com/abpframework/abp/blob/dev/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs#L101

  • User Avatar
    0
    learnabp created

    no a dialogbox which shows like the one that often says "internal error occured"

    like above but with mu custom messgae of tenant not found

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi @learnabp, I can't see UserFriendlyException but I'll suggest something below. If you already construct your exception same, please share your exception class too.


    Make sure your exception is a DomainException and it has an error code and your localization json files have a resource with that error code as key.

    You can examine 'AuthorAlreadyExistsException' in this sample

  • User Avatar
    0
    learnabp created

    what do you mean by DomainException .... do i have to throw the BusinessException in Domain Project

  • User Avatar
    0
    learnabp created

    what do you mean by DomainException .... do i have to throw the BusinessException in Domain Project

    i am throwing the exception in my OnPost when i submit the form on a page to check if tenant exists

    
        if (tenant != null)
        {
    
            throw new UserFriendlyException(message: "Tenant Name already exists please choose another");
    
        }
    
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    If exception is inherited from Business exception and it has errorcode, exception detail text will be displayed from resources, exception message won't be displayed in UI automatically. You have to add a resource for errorcode of your exception

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    @learnabp let me introduce for your situation:

    • As a best-practice, BusinessException should be thrown from Domain layer. So You have to create your Exception in your Domain layer:
    public class UserFriendlyException : BusinessException
    {
        public UserFriendlyException(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context)
        {
        }
    
        public UserFriendlyException(string tenantName)
        {
            // As an example, place your awesome unique code here
            Code = "Acme.BookStore:0001"; 
    
            WithData(nameof(TenantName), tenantName);
        }
        public string TenantName { get; set; }
    }
    
    • You can throw like that:
        if (tenant != null)
        {
            throw new UserFriendlyException(Tenant.Name);
        }
    
    • You can localize what user sees in your localization files: en.json
      {
        "culture": "en",
        "texts": {
          "Acme.BookStore:0001": "Tenant Name {TenantName} already exists please choose another",
          //...
        }
      }
      
      You can display different messages for each language with that.

    If you create a Business Exception like that, you won't see "internal error occured".

    Let me know if that helps or not. @learnabp

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 01, 2024, 05:35