Base solution for your next web application
Open Closed

DontWrapResult UserFriendlyException #4534


User avatar
0
BobIngham created

.Net Core, Angular 5.0.2 I am trying to return UserFriendlyException errors to app written using Ionic 3. I can sign up and find a tenant using a (unique) voucher code, return the tenantId to the app and add this to the sign in process which gives me the correct tenantId as a claim in the JWT. To return data to the app I have implemented the [DontWrapResult] attribute which works well. However, I can not find a way to return well formatted errors to the app using the UserFriendlyException constructor. For example, I am trying to implement UserFriendlyException to indicate that a username is already taken (as per the standard system). The error received at the app has a status of 0 and a statusText of "Unknown error". I have tried implementing [DontWrapResult(WrapOnError = false)] but I am still getting the same results. The question is: How to return UserFriendlyException to the app in a manner which can be read in the app?


7 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Why not wrap on error?

  • User Avatar
    0
    BobIngham created

    It does not matter whether I use the WrapOnError qualifier or not, I am still getting the same result.

  • User Avatar
    0
    aaron created
    Support Team

    You set it to false though. Did you try true?

  • User Avatar
    0
    BobIngham created

    Yes, exactly the same result.

  • User Avatar
    0
    ismcagdas created
    Support Team

    What happens when you completely remove DontWrapResult attribute ?

  • User Avatar
    0
    BobIngham created

    Hi Aaron, This was a bit sticky but I found the answer. First i will show you code when everything goes well. To register with the system an Ionic app sends a username, password and voucher code. The call from Ionic uses httpClient:

    public onSignUp(AppRegisterModel): Observable<AppRegisterResultModel> {
        return this.httpClient.post("http://...//api/services/app/Account/RegisterFromApp", AppRegisterModel)
          .map(registerResultModel => registerResultModel)
          .catch(this.handleError);
      }
    

    AppRegisterModel contains a voucher code unique to each tenant. On the server I find the tenant from the voucher code:

    var tenantId = await GetTenantIdFromVoucherCodeAsync(model.VoucherCode);
    

    and then I add the user in a unit of work using the SetTenantId filter and ensure AbpSession uses the same value:

    using (_unitOfWorkManager.Current.SetTenantId(tenantId))
    {
       AbpSession.Use(tenantId, null);
    

    Successful registration returns a tenantId which is saved locally and added to the sign in process.
    AppTokenAuthController is a direct copy of TokenAuthController with changes only to the input model and the first method:

    [HttpPost]
    public async Task<AuthenticateResultModel> AppAuthenticate([FromBody] AppAuthenticateModel model)
    {
      var loginResult = await GetLoginResultAsync(
        model.UserNameOrEmailAddress,
        model.Password,
        await GetTenancyNameFromTenantId(model.TenantId)
      );
    

    AppAuthenticateModel includes TenantId which is added to the JWT token and can now be used securely between an Ionic app and a Zero server. So far so good but if anything goes wrong it's not nice. At the moment my error message returned by a _UserFriendlyException_can be found here:

    error.error.error.message;
    

    Despite the fact it's not nice it works perfectly and we get a nice user friendly error message displayed on the Ionic app. I messed with the

    [DontWrapResult(WrapOnError = true)]
    

    with different values but the above configuration proved stable.

    This also closes [https://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=10636&p=26158#p26158]).

    Thanks for your help.

    Bob

  • User Avatar
    0
    aaron created
    Support Team

    Thanks for letting us know!