Created
August 18, 2017 16:00
-
-
Save hexsprite/852596b9a4d797548ccee7e1b49f2fc1 to your computer and use it in GitHub Desktop.
service worker error tracking example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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