Skip to content

Instantly share code, notes, and snippets.

@hexsprite
Created August 18, 2017 16:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hexsprite/852596b9a4d797548ccee7e1b49f2fc1 to your computer and use it in GitHub Desktop.
Save hexsprite/852596b9a4d797548ccee7e1b49f2fc1 to your computer and use it in GitHub Desktop.
service worker error tracking example
/* eslint-env browser, serviceworker */
self.addEventListener('error', function (event) {
logError(event.error)
})
self.addEventListener('unhandledrejection', function (event) {
let { reason, detail } = event
if (!reason && detail) {
reason = detail.reason
}
var message = 'unhandled rejection was null or undefined!'
message = reason ? reason.message || String(reason) : message
if (reason instanceof Error) {
logError(reason)
} else {
logError(message)
}
})
function logError (error) {
let errObj = {
name: Object.getPrototypeOf(error).name,
message: error.message,
stack: error.stack || new Error().stack
}
fetch('/service-worker/error-track', {
credentials: 'include',
method: 'POST',
body: JSON.stringify(errObj),
headers: {
'Content-type': 'application/json'
}
}).catch(function (error) {
console.error('service-worker/error: logger failed', error)
})
}
// for testing error logging
// causeError()
// function causeError () {
// console.log(window.Rollbar)
// }
// causeUnhandledRejection()
// function causeUnhandledRejection () {
// const PTest = function () {
// return new Promise(function (resolve, reject) {
// throw new Error('failed')
// })
// }
// PTest().then(function () {
// console.log('yeah!')
// })
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment