Base solution for your next web application
Open Closed

Handle ajax error response from controller #2449


User avatar
0
jeromevoxteneo created

Hello,

I have trouble in my code actually and for example when the user set a wrong password the login method in AccountController throw a UserFriendlyException that's work fine.

But I don't get the right error message in the popup. I put some console.log in js file and it seems that the problem is :

.fail(function (jqXHR) {
                    console.log('fail ajax', jqXHR);
                    if (jqXHR.responseJSON && jqXHR.responseJSON.__abp) {
                        abp.ajax.handleResponse(jqXHR.responseJSON, userOptions, $dfd, jqXHR);
                    } else {
                        abp.ajax.handleNonAbpErrorResponse(jqXHR, userOptions, $dfd);
                    }
                });

there is no responseJSON but responseText

In preview I get the right error:

Login failed!

Description : Une exception non gérée s'est produite au moment de l'exécution de la requête Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code. 

Détails de l'exception: Abp.UI.UserFriendlyException: Login failed!

Erreur source: 


Ligne 224 :                    return loginResult;
Ligne 225 :                default:
Ligne 226 :                    throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt(loginResult.Result, usernameOrEmailAddress, tenancyName);
Ligne 227 :            }
Ligne 228 :        }

Fichier source : C:\projects\dotnet-abpzero-iexpertise\iExpertise.Web\Controllers\AccountController.cs    Ligne : 226

I don't know where is my mistake, I don't change the code that call the login action:

abp.ui.setBusy(
                null,
                abp.ajax({
                    contentType: app.consts.contentTypes.formUrlencoded,
                    url: $loginForm.attr('action'),
                    data: $loginForm.serialize()
                })
            );

I just check in last version of aspnet zero template and all works fine. Only my code failed :(

Do you have idea why I get this behavior ?


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    It might be related to customErrors mode in web.config. Can you check if it's value is "On" or not ?

  • User Avatar
    0
    jeromevoxteneo created

    After further investigation (add symbol from Abp boilerplate framework in order to debug OnException override method in AbpController):

    // If custom errors are disabled, we need to let the normal ASP.NET exception handler
                // execute so that the user can see useful debugging information.
                if (!context.HttpContext.IsCustomErrorEnabled)
                {
                    base.OnException(context);
                    return;
                }
    

    IsCustomErrorEnabled is false.

    What define is this boolean value is false or true?

  • User Avatar
    0
    jeromevoxteneo created

    Here is my config:

    <customErrors mode="Off">
          <error statusCode="404" redirect="~/Error/E404" />
        </customErrors>
    
  • User Avatar
    0
    jeromevoxteneo created

    Ok after switching CustomErrors from Off to On the behavior is ok.

    Thank you

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)