2

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?

MB.
  • 4,167
  • 8
  • 52
  • 79

2 Answers2

2

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.

joakimbl
  • 18,081
  • 5
  • 54
  • 53
  • You are right, removing [] worked. However is $exceptionHandler a service? In the skel app the services are loaded like this: https://github.com/angular/angular-seed/blob/master/app/js/services.js – MB. May 10 '13 at 19:16
  • Yes, it's a service provided by angular: http://docs.angularjs.org/api/ng.$exceptionHandler - you use this service to create your own exception handler (you're not creating a new service) – joakimbl May 10 '13 at 19:32
0

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";
});
Mak
  • 1