crash-reporter.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. const binding = process.electronBinding('crash_reporter');
  3. class CrashReporter {
  4. constructor () {
  5. this.productName = null;
  6. this.crashesDirectory = null;
  7. }
  8. init (options) {
  9. throw new Error('Not implemented');
  10. }
  11. start (options) {
  12. if (options == null) options = {};
  13. const {
  14. productName,
  15. companyName,
  16. extra = {},
  17. ignoreSystemCrashHandler = false,
  18. submitURL,
  19. uploadToServer = true
  20. } = options;
  21. if (companyName == null) throw new Error('companyName is a required option to crashReporter.start');
  22. if (submitURL == null) throw new Error('submitURL is a required option to crashReporter.start');
  23. const ret = this.init({
  24. submitURL,
  25. productName
  26. });
  27. this.productName = ret.productName;
  28. this.crashesDirectory = ret.crashesDirectory;
  29. if (extra._productName == null) extra._productName = ret.productName;
  30. if (extra._companyName == null) extra._companyName = companyName;
  31. if (extra._version == null) extra._version = ret.appVersion;
  32. binding.start(ret.productName, companyName, submitURL, ret.crashesDirectory, uploadToServer, ignoreSystemCrashHandler, extra);
  33. }
  34. getLastCrashReport () {
  35. const reports = this.getUploadedReports()
  36. .sort((a, b) => {
  37. const ats = (a && a.date) ? new Date(a.date).getTime() : 0;
  38. const bts = (b && b.date) ? new Date(b.date).getTime() : 0;
  39. return bts - ats;
  40. });
  41. return (reports.length > 0) ? reports[0] : null;
  42. }
  43. getUploadedReports () {
  44. const crashDir = this.getCrashesDirectory();
  45. if (!crashDir) {
  46. throw new Error('crashReporter has not been started');
  47. }
  48. return binding.getUploadedReports(crashDir);
  49. }
  50. getCrashesDirectory () {
  51. return this.crashesDirectory;
  52. }
  53. getUploadToServer () {
  54. if (process.type === 'browser') {
  55. return binding.getUploadToServer();
  56. } else {
  57. throw new Error('getUploadToServer can only be called from the main process');
  58. }
  59. }
  60. setUploadToServer (uploadToServer) {
  61. if (process.type === 'browser') {
  62. return binding.setUploadToServer(uploadToServer);
  63. } else {
  64. throw new Error('setUploadToServer can only be called from the main process');
  65. }
  66. }
  67. addExtraParameter (key, value) {
  68. binding.addExtraParameter(key, value);
  69. }
  70. removeExtraParameter (key) {
  71. binding.removeExtraParameter(key);
  72. }
  73. getParameters () {
  74. return binding.getParameters();
  75. }
  76. }
  77. module.exports = CrashReporter;