MB.
MB.

Reputation: 4221

How to properly overwrite the exceptionHandler in angularjs?

For an app I'm using a skeleton that is very similar to https://github.com/angular/angular-seed.

I've tried something like this, in services.js:

'use strict';

/* Services */

angular.module('mApp.services', []).
  factory('$exceptionHandler', function () {
    return function (exception, cause) {
        alert(exception.message);
    }
});

This doesn't do anything, it doesn't seem to overwrite the exceptionHandler.

If I try:

var mod = angular.module('mApp', []);

mod.factory('$exceptionHandler', function() {
  return function(exception, cause) {
    alert(exception);
  };
});

It overwrite the whole app.

How can I properly overwrite the exceptionHandler if I am using a skeleton similar to the default angular app?

Upvotes: 2

Views: 2322

Answers (2)

Mak
Mak

Reputation: 1

try this example

http://jsfiddle.net/STEVER/PYpdM/

var myApp = angular.module('myApp', ['ng']).provider({

    $exceptionHandler: function(){
        var handler = function(exception, cause) {
            alert(exception);
            //I need rootScope here
        };

        this.$get = function() {
            return handler;
        };
    }
});

myApp.controller('MyCtrl', function($scope, $exceptionHandler) {
    console.log($exceptionHandler);
    throw "Fatal error";
});

Upvotes: 0

joakimbl
joakimbl

Reputation: 18081

It's hard to know for certain without seeing the rest of your app, but I'm guessing angular.module('myApp').factory( ... will work. If you leave out the second parameter (,[]) angular will retrieve an existing module for further configuring. If you keep it angular will create a new module.

Upvotes: 2

Related Questions