Last active
April 26, 2020 06:29
-
-
Save Dinir/0564469c1beacd8adc06627274e9b50e to your computer and use it in GitHub Desktop.
a small script that stores error logs somewhere accessible
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
class ErrorLogCollector { | |
constructor() { | |
/* | |
By putting it in an array, the log variable can be copied by reference. | |
And when attempted to display the whole array on a textarea | |
it will only show the text. | |
*/ | |
this.errorLog = [''] | |
this.write = this.write.bind(this) | |
this.writeCustomError = this.writeCustomError.bind(this) | |
window.onerror = this.write | |
window.addEventListener('customErrorMessage', this.writeCustomError) | |
} | |
write (message, url, lineNo, columnNo, error) { | |
const timestamp = | |
(Math.floor(1000*performance.now())/1000).toFixed(3) | |
this.errorLog[0] += `[${timestamp}] ` | |
this.errorLog[0] += error ? error.stack : message | |
this.errorLog[0] += '\n' | |
return false | |
} | |
writeCustomError (e) { | |
if (!e.detail instanceof Error) { return null } | |
const customError = e.detail | |
return this.write(customError.message, null, null, null, customError) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment