0

Searching product is working fine when product is found, but if user search with letters, or bad criteria, nothing happens. I can see the error message in JavaScript-console and in Header there is Status Code 204, no Content. I think that Angular is just working badly, when empty object is coming back. How can I tell an user that there is no products with his/her criteria, cause I can't catch the error message at all at the moment. What is the correct and best solution to handle this? Catching errors or solving that result was empty and showing an error message in HTML-page?

Service:

return $resource(

                    'http://localhost:8080/RMAServer/webresources/com.rako.entity.jdeserials/:id',
                    {},
                    {
                        get: { method: 'GET',isArray:false, params: {id: '@serial'} },
                        update: { method: 'PUT', params: {id: '@serial'} }
                    });

Controller

    //Searching product with serial number/LOTN
                $scope.searchProduct = function () {
                    $scope.serials = lotnSvc.get({id: $scope.serial}).$promise.then(
                        function (data) {
                            var litm = data.litm;

                            productSvc.get({id: litm}, function (product) {
                            $scope.product = product;
                            getBrands();
                        },function(error){
                      alert('ERROR when searching product'); 
                      console.log("error---->" + error);
                    });
                    },function(error){
                      alert('ERROR when searching product'); 
                      console.log("error---->" + error); 
                    });

                };

Error message in javaScript console

Error: [$resource:badcfg] Error in resource configuration for action `get`. Expected response to contain an object but got an array
http://errors.angularjs.org/1.3.15/$resource/badcfg?p0=get&p1=object&p2=array
at REGEX_STRING_REGEXP 

Status code in Header

Status code in header after GET

Sami
  • 2,311
  • 13
  • 46
  • 80
  • You are returning an array of errors. You have set `isArray:false` and data must be in an array format thus `response to contain an object but got an array` – Satpal May 30 '15 at 11:59
  • I thought that "bad criteria" would return 400 status code (bad request). – Wédney Yuri May 30 '15 at 12:00
  • @Wédney Yuri - I Thought so as well, but if you check my updated question, there is a picture of header. – Sami May 30 '15 at 13:28
  • @Satpal - I don't think so, because Header's status code is 204 No Content. – Sami May 30 '15 at 13:28
  • @Sami you can change the server code? I think the most correct would it return 400 (bad request) in the case of "bad criteria". – Wédney Yuri May 30 '15 at 13:33
  • From w3.org (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2): Successful 2xx -> This class of status code indicates that the client's request was successfully received, understood, and accepted. – Wédney Yuri May 30 '15 at 13:38
  • @WédneyYuri Sure, but it's up to the API to decide to accept a request, don't you think? Moreover 400 **always** implies a problem with the app. It shouldn't issue such requests in the first place. I'm pretty sure `204` in this case means "nothing found matching the criteria", so it could also happen with valid input. Neither Angular, nor the user nor the API is to blame for this problem. The app is simply ignorant of the API. – a better oliver May 30 '15 at 14:47

1 Answers1

1

Just try to send the success and the error handlers as second and third parameters to "get" function instead of using promises. There was the same problem: How to handle $resource service errors in AngularJS

Community
  • 1
  • 1
MaximSadym
  • 61
  • 4