1

When I minify/obfuscate/uglify my code, my httpInterceptor is not working anymore.

I get the following error :

Error: error:unpr
Unknown Provider

Unknown provider: aProvider <- a

My code with all irrelevant stuff removed:

angular.module("MyApp").factory('sessionChecker', ['$q', '$injector',
    function ($q, $injector) {
        var sessionRecoverer = {
            responseError: function (response) {
                // Session has expired
                if (response.status === 401) {
                    var $location = $injector.get('$location');
                    var $modal = $injector.get('$modal');
                    $location.path("/login");

                } else if (response.status === 500) {
                    var $location = $injector.get('$location');
                    //Do stuff..
                } else if (response.status === 0) {
                    var $modal = $injector.get('$modal');
                    //Do stuff..
                }
                return $q.reject(response);
            }
        };
        return sessionRecoverer;
    }]);

angular.module("MyApp").config(['$httpProvider',
    function ($httpProvider) {
        $httpProvider.interceptors.push('sessionChecker');
    }]);

How would I inject my dependencies correctly, or am I missing something? This is working fine without minifying my code.

Any help would be appreciated.

4
  • aProvider doesn't exist in the source code you have shown, why are you using the $injector to inject things, that can be injected through the constructor... Commented Feb 26, 2015 at 9:56
  • The error is after minifying.. Commented Feb 26, 2015 at 9:57
  • I don't see how your minifier is doing that, seeing as everything looks to me to be fine. Commented Feb 26, 2015 at 9:58
  • I don't know how my minfier is doing that. It gives weird angular errors on minified code.. I get what you say about using the $injector, but if I try injecting through the constructor I get a 'Circular Dependency' error. Minified or not.. Commented Feb 26, 2015 at 10:01

1 Answer 1

2

Are you sure it is this piece of code that is generating the error? (This code looks OK to me...) But this error is definitely caused by not using the ['dep1', dep2', function(dep1,dep2){...}] syntax (or equivalent with $inject).

You can try the strict DI mode so that Angular will report the error in non-minified mode (ref here):

<div ng-app="myApp" ng-strict-di>

Or (if bootstrapping manually):

angular.bootstrap(document, ['myApp'], {
    strictDi: true
});
Sign up to request clarification or add additional context in comments.

4 Comments

I'm pretty sure this is the code that is generating this error. This fires as soon as I lose connection to my server (i.e. pulling the internet cable from the LAN port), and that is exactly when this error pops, thus failing the code to execute (showing a modal).
You are using the $modal; you are probably defining a controller which uses the simplified injection pattern. It is in the 2nd //Do stuff.. part...
You, my friend, are a hero. You even find mistakes in code I didn't post. My apologies for that. It was exactly what you said.. I used simplified injection on the $modal, thinking it was not needed at that point.. Meet my lazy brain..
Have found myself in exactly the same situation :)

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.