0

Ive used couple of days trying to figugure out how to return an error to angular ajax request to web api.

in my js AccountController i have a login method:

$scope.Login = function () {
    AccountService.Login($scope.UserData.LoginName, $scope.UserData.Password).success(function (account) {
        $scope.UserData = account;
    }).error(function () {
        console.log("failed");
    });
};

and in web api i have folowing:

public Account Login(string loginName, string password) 
{ 
    var emptyAccount = new Account(); 
    password = Encrypt(password);
    var account = db.Accounts.FirstOrDefault(c=>c.Password == password && c.LoginName == loginName);

    if (account == null)
    {
        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
    }

    acount.Password = "";
    return account;
}

The problem is that i throw a new HttpResponseException which fire off and dont return anything back to ajax. How do i fix this?

2 Answers 2

2

Normally, in this case it is the error handler that will get triggered.

.error(function () {
    alert("login failed");
}

This is where you could handle the error.

Also you probably want to return 401 Unauthorized in this case instead of 404. Also in general it is considered bad practice to throw exceptions in cases where you can handle it gracefully:

public HttpResponseMessage Login(string loginName, string password)
{
    var emptyAccount = new Account();
    password = Encrypt(password);
    var account = db.Accounts.FirstOrDefault(c => c.Password == password && c.LoginName == loginName);

    if (account == null)
    {
        return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "unauthorized");
    }

    acount.Password = "";
    return Request.CreateResponse(HttpStatusCode.OK, account);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh thanks that was exactly what i wanted, was almost about to give up on that
0

When you sending the data, and it's hit the servers, it will return header 200 OK because it's already hit your controller, even when your controller code is throw error (in later process).

so, if you want to know what error that thrown, I will create AccountResponse Dto into that, and introduce new Error field in that like so:

public class AccountResponse()
{
    public Account account { get; set;}
    public string ErrorMessage { get; set; }
}

and then on the controller:

public AccountResponse Login(string loginName, string password) 
{ 
    var emptyAccount = new Account(); 
    password = Encrypt(password);
    var account = db.Accounts.FirstOrDefault(c=>c.Password == password && c.LoginName == loginName);

    if (account == null)
    {
        return new AccountResponse(){ ErrorMessage = Response.ResponseCodes.something;
    }

    AccountResponse.account = account;
    AccountResponse.account.Password = "";
    return AccountResponse;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.