Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular $http error response statusText always undefined

I am trying to return a custom error message to the user to let them know what went wrong if an error occurs, but I have tried everything to display the message and nothing seems to be capturing it. Here is my angular code:

$scope.save = function (style) {
    styleAPIservice.addStyle(style).success(function () {
        $scope.success = "Style added successfully";
    }).error(function (data, status, headers, config, statusText) {
        $scope.error = "An error occurred while saving style: " + data + "|"+data.data+"|"+data.statusText+"|"+statusText;
    });
}

Here is the styleAPIservice function it's calling:

styleAPI.addStyle = function (style) {
    return $http.post(AppRoot + "api/StyleAPI/",
        JSON.stringify(style),
            {
                headers: {
                    'Content-Type': 'application/json'
                }
            });
}

And here is the API function:

[HttpPost]
public HttpResponseMessage PostStyle(Style style)
{
    if (string.IsNullOrEmpty(style.Pattern.PatternName) || string.IsNullOrEmpty(style.StockNumber))
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Pattern Name and Stock Number are required.");

    var checkStyle = StyleRepository.LoadStyleByStockNumber(style.StockNumber);
    if (checkStyle != null)
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Style number already exists. Please use update.");

    try
    {
        // Save style
    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Error creating style: " + ex.Message);
    }

    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, style);
    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = style.StyleId }));
    return response;
}

And here is what is returned when an error occurs (such as Style already exists):

An error occurred while saving style: [object Object]|undefined|undefined|undefined

What am I doing wrong? I feel like I've searched everywhere and tried every possible suggestion, but I am just at a loss as to why I can't display my error messages.

like image 477
CoolCriSyS Avatar asked Dec 01 '25 03:12

CoolCriSyS


2 Answers

Replace .error() by .catch().

$http.post('/url',json)
    .success(function(data, status, headers, config){

      // some code here

    })
    .catch(function(data, status, headers, config){ // <--- catch instead error

        data.statusText; //contains the error message

    });
like image 108
Markenson França Avatar answered Dec 02 '25 15:12

Markenson França


For anyone else who may have had this issue, the message was in data.Message. I found it by debugging the JS.

like image 28
CoolCriSyS Avatar answered Dec 02 '25 17:12

CoolCriSyS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!