Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add a logger interceptor #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/templates/client/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ <h3>Login with demo / 1234</h3>
<script src="bower_components/angular-leaflet-directive/dist/angular-leaflet-directive.js"></script>
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script src="bower_components/angular-local-storage/dist/angular-local-storage.js"></script>
<script src="bower_components/stacktrace-js/stacktrace.js"></script>
<script src="bower_components/angular-loggly/logglyService.js"></script>
<script src="bower_components/angular-loading-bar/build/loading-bar.js"></script>
<!-- endbower -->
<script type="text/javascript" src="bower_components/angular-loggly/loggly-jslogger/src/loggly.tracker.js"></script>

<!-- endbuild -->

<!-- build:js({.tmp,app}) scripts/scripts.js -->
Expand All @@ -102,6 +106,9 @@ <h3>Login with demo / 1234</h3>
<script src="scripts/controllers/account.js"></script>
<script src="scripts/services/account.js"></script>
<script src="scripts/controllers/homepage.js"></script>
<script src="scripts/services/stacktrace.js"></script>
<script src="scripts/services/errorlog.js"></script>
<script src="scripts/services/exceptionhandler.js"></script>
<!-- endbuild -->
</body>
</html>
67 changes: 67 additions & 0 deletions app/templates/client/app/scripts/services/errorlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

/**
* @ngdoc service
* @name clientApp.errorLogService
* @description
* # errorLogService
* Factory in the clientApp.
*/
angular.module('clientApp')
.factory('ErrorLog', function ($log, $window, StackTrace, Config, loggly) {
// Log the given error to the remote server.
function log(exception, cause) {

// Pass off the error to the default error handler
// on the AngualrJS logger. This will output the
// error to the console (and let the application
// keep running normally for the user).
$log.error.apply( $log, arguments );

// Now, we need to try and log the error the server.
// --
// NOTE: In production, I have some debouncing
// logic here to prevent the same client from
// logging the same error over and over again! All
// that would do is add noise to the log.
try {

var errorMessage = exception.toString();
var stackTrace = StackTrace.print({ e: exception });

var data = {
errorUrl: $window.location.href,
errorMessage: errorMessage,
stackTrace: stackTrace,
cause: cause || ''
};

if (Config.logglyApiKey) {
// Log to the "loggly" service.
// @todo: Move to app.config().
loggly.setApiKey(Config.logglyApiKey);
// @todo: We can only use loggly.log - loggly.error() doesn't pass
// the json object properly.
loggly.log(data);
}
else {
// Log the JavaScript error to the server.
$.ajax({
type: 'POST',
url: Config.logErrorUrl,
contentType: 'application/json',
data: data
});
}
}
catch (loggingError) {
// For Developers - log the log-failure.
$log.warn( "Error logging failed" );
$log.log( loggingError );

}
}

// Return the logging function.
return( log );
});
16 changes: 16 additions & 0 deletions app/templates/client/app/scripts/services/exceptionhandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

/**
* @ngdoc service
* @name clientApp.exceptionHandler
* @description
* # exceptionHandler
* Provider in the clientApp.
*/
angular.module('clientApp')
.provider('$exceptionHandler', function () {

this.$get = function (ErrorLog) {
return ErrorLog;
};
});
18 changes: 18 additions & 0 deletions app/templates/client/app/scripts/services/stacktrace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

/**
* @ngdoc service
* @name clientApp.stackTrace
* @description
* # stackTrace
* Factory in the clientApp.
*/
angular.module('clientApp')
.factory('StackTrace', function () {

// Public API here
return {
// "printStackTrace" is a global object.
print: printStackTrace
};
});
6 changes: 5 additions & 1 deletion app/templates/client/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
"angular-leaflet-directive": "~0.7.9",
"angular-ui-router": "~0.2.13",
"angular-local-storage": "~0.1.5",
"stacktrace-js": "~0.6.4",
"angular-loading-bar": "~0.6.0"
},
"devDependencies": {
"angular-mocks": "~1.3.0",
"angular-scenario": "~1.3.0"
},
"appPath": "app"
"appPath": "app",
"resolutions": {
"angular": "1.3.x"
}
}
18 changes: 18 additions & 0 deletions app/templates/client/test/spec/services/errorlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

describe('Service: errorLogService', function () {

// load the service's module
beforeEach(module('clientApp'));

// instantiate service
var errorLogService;
beforeEach(inject(function (_errorLogService_) {
errorLogService = _errorLogService_;
}));

it('should do something', function () {
expect(!!errorLogService).toBe(true);
});

});
18 changes: 18 additions & 0 deletions app/templates/client/test/spec/services/exceptionhandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

describe('Service: exceptionHandler', function () {

// load the service's module
beforeEach(module('clientApp'));

// instantiate service
var exceptionHandler;
beforeEach(inject(function (_exceptionHandler_) {
exceptionHandler = _exceptionHandler_;
}));

it('should do something', function () {
expect(!!exceptionHandler).toBe(true);
});

});
18 changes: 18 additions & 0 deletions app/templates/client/test/spec/services/stacktrace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

describe('Service: stackTrace', function () {

// load the service's module
beforeEach(module('clientApp'));

// instantiate service
var stackTrace;
beforeEach(inject(function (_stackTrace_) {
stackTrace = _stackTrace_;
}));

it('should do something', function () {
expect(!!stackTrace).toBe(true);
});

});