2014-12-12 08:58:07 -05:00
|
|
|
app = angular.module 'ErrorCatcher', []
|
|
|
|
|
|
|
|
app.config ['$provide', ($provide) ->
|
|
|
|
$provide.decorator '$exceptionHandler', ['$log', '$delegate', ($log, $delegate) ->
|
|
|
|
return (exception, cause) ->
|
|
|
|
if (Raven?.captureException?)
|
|
|
|
Raven.captureException exception;
|
|
|
|
$delegate(exception, cause)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2016-11-04 11:44:12 -04:00
|
|
|
# Interceptor to check auth failures in all $http requests
|
2014-12-12 08:58:07 -05:00
|
|
|
# http://bahmutov.calepin.co/catch-all-errors-in-angular-app.html
|
2016-11-04 11:44:12 -04:00
|
|
|
|
|
|
|
app.factory 'unAuthHttpResponseInterceptor', ['$q','$location', ($q, $location) ->
|
|
|
|
responseError: (response) ->
|
|
|
|
# redirect any unauthorised or forbidden responses back to /login
|
|
|
|
#
|
|
|
|
# set disableAutoLoginRedirect:true in the http request config
|
|
|
|
# to disable this behaviour
|
|
|
|
if response.status in [401, 403] and not response.config?.disableAutoLoginRedirect
|
|
|
|
# for /project urls set the ?redir parameter to come back here
|
|
|
|
# otherwise just go to the login page
|
|
|
|
if window.location.pathname.match(/^\/project/)
|
|
|
|
window.location = "/login?redir=#{encodeURI(window.location.pathname)}"
|
|
|
|
else
|
|
|
|
window.location = "/login"
|
|
|
|
# pass the response back to the original requester
|
|
|
|
return $q.reject(response)
|
|
|
|
]
|
|
|
|
|
|
|
|
app.config ['$httpProvider', ($httpProvider) ->
|
|
|
|
$httpProvider.interceptors.push 'unAuthHttpResponseInterceptor'
|
|
|
|
]
|