1

I'm new to angular and kind of lost right now. I have a provder that handles if server sends response or not and then I do some stuff based on it.

Here is the provider code

define(['angular', '../module'], function (angular, module) {
    return module.provider('httpProvider', ['$httpProvider', function ($httpProvider) {
        var interceptor = ['$q', '$rootScope', function ($q, $rootScope) {
            return {
                response: function (response) {
                    $rootScope.serverError = true;
                    return response;
                },
                responseError: function (rejection) {
                    if (rejection.status === 0) {
                        $rootScope.serverError = true;
                    }

                    return $q.reject(rejection);
                },
            };
        }];

        $httpProvider.interceptors.push(interceptor);
    }]);
});

And it throws error:

Provider 'httpProvider' must define $get factory method.

Any idea?

EDIT:

Here is how my factory looks now, and its created fine, but I can not inject it into config

define(['angular', './module'], function (angular, module) {
    module.factory('httpInterceptor', function () {
        var interceptor = ['$q', '$rootScope', function ($q, $rootScope) {
            return {
                response: function (response) {
                    $rootScope.serverError = true;
                    return response;
                },
                responseError: function (rejection) {
                    if (rejection.status === 0) {
                        $rootScope.serverError = true;
                    }

                    return $q.reject(rejection);
                }
            };
        }];

        return interceptor;
    });
});

In module config I use it this way:

$httpProvider.interceptors.push('httpInterceptor');

But it actually push to the array just a string ( who would expect that right? D: ) and nothing is happening. I've changed the factory to always has serverError set to true, so I can test it, but it will actually do nothing, so it means that response or responseError functions are never called.

Any idea?

Michal Takáč
  • 1,005
  • 3
  • 17
  • 37
  • 1
    I'm not really good with providers, but the doc recommends a factory to handle an interceptor : https://docs.angularjs.org/api/ng/service/$http#interceptors – Deblaton Jean-Philippe Aug 13 '15 at 12:31
  • Why are you trying to create dummy httpProvider? just to define interceptors? You can do it in config or with simple service. Doesn't look like you need a provider for this. You can of course, but this is not what providers are for. – dfsq Aug 13 '15 at 12:38
  • Yes. Is it possible to do it differently? I'm pretty lost in angular for now. – Michal Takáč Aug 13 '15 at 12:42
  • @dfsq Can you show me an example? When i've tried to use factory instead of provider, it throws: module.factory is not a function. I've had this in config, but wanted to have a service for that, and this is what i've been able to make :D – Michal Takáč Aug 13 '15 at 12:50
  • @MichalTakáč, this isn't a case of `.factory` or `.provider`. You aren't creating another configurable service. You are configuring an existing `$http` service, so all you need is a `.config` of an existing `$httpProvider` - this is what dfsq meant. In other words, `module.config(function($httpProvider){...})` – New Dev Aug 13 '15 at 13:09
  • possible duplicate of [AngularJs routeProvider http status 403](http://stackoverflow.com/questions/25041929/angularjs-routeprovider-http-status-403) – Krzysztof Safjanowski Aug 13 '15 at 13:39

2 Answers2

0

Here is how you should create interceptors:

angular.module('myApp', []).
    config(function($httpProvider) {
        $httpProvider.interceptors.push('myInterceptor');
    });

angular.module('myApp').
    factory('myInterceptor', function() {
        return {

            //interceptor object

        };
    });
0

Ok, I've been able to fix it, I figure out that I've created the factory wrong way, the right one is this:

define(['angular', './module'], function (angular, module) {
    module.factory('httpInterceptor',['$q', '$rootScope', function ($q, $rootScope) {
            return {
                'request': function(config) {
                    return config;
                },
                'response': function (response) {
                    $rootScope.serverError = false;
                    return response;
                },
                'responseError': function (rejection) {
                    if (rejection.status === 0) {
                        $rootScope.serverError = true;
                    }

                    return $q.reject(rejection);
                }
            };
    }]);
});

In config of module I use:

$httpProvider.interceptors.push('httpInterceptor');

Thanks all of you for help.

Michal Takáč
  • 1,005
  • 3
  • 17
  • 37