0Pricing
Electron Desktop App Development · Leçon

Rapports d’erreurs

Configurez la collecte de rapports d’erreurs pour recueillir et envoyer automatiquement les journaux de plantage, afin de vous aider à identifier et corriger les problèmes critiques de votre application.

Rapports d’erreurs est une leçon Electron Desktop App Development gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Electron Desktop App Development, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Electron Desktop App Development comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Why Crash Reports Matter

Ever had an app suddenly close without warning? That's a crash! Crash reporting is a vital tool that automatically collects information about these unexpected errors.

This data helps developers understand what went wrong, fix bugs, and improve the stability and reliability of their applications. It's like a black box for your app!

Electron's Built-in Tool

Electron comes with a powerful, built-in crashReporter module. This module monitors your application for crashes and, when one occurs, sends a report to a designated server.

It works for both the main process (Node.js) and renderer processes (web content), ensuring comprehensive coverage.

Starting the Crash Reporter

To enable crash reporting, you need to initialize it in your main process as early as possible, typically before any windows are created. Here's how:

const { app, crashReporter, BrowserWindow } = require('electron');

// IMPORTANT: Call crashReporter.start() as early as possible
crashReporter.start({
  productName: 'MyElectronApp',
  companyName: 'MyCompany',
  submitURL: 'http://localhost:1127/post', // Replace with your actual crash server URL
  uploadToServer: true,
});

app.whenReady().then(() => {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
  });
  mainWindow.loadFile('index.html');
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    // In macOS, usually re-create a window in the app when the dock icon is clicked
    // For this example, we'll just ensure the app doesn't quit on macOS by default
  }
});

Configuring Your Reports

When you call crashReporter.start(), you provide several important options:

  • productName: The name of your application.
  • companyName: Your company's name.
  • submitURL: This is the most crucial! It's the URL of the server endpoint that will receive your crash reports.
  • uploadToServer: Set to true to automatically send reports.

Without a valid submitURL pointing to a server that can accept these reports, they won't be sent anywhere!

Crashing the Main Process

You can intentionally crash the main process to test your setup. The process.crash() method will terminate the process immediately, triggering the crash reporter.

Remember, this is for testing! In a real app, crashes are usually unexpected. The crash reporter will send a report even if the app completely freezes.

const { app, crashReporter, BrowserWindow } = require('electron');

crashReporter.start({
  productName: 'MyElectronApp',
  companyName: 'MyCompany',
  submitURL: 'http://localhost:1127/post',
  uploadToServer: true,
});

app.whenReady().then(() => {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
  });
  mainWindow.loadFile('index.html');

  // Simulate a crash after 5 seconds for testing
  setTimeout(() => {
    console.log('Simulating main process crash...');
    process.crash(); // This will crash the app!
  }, 5000);
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    // In macOS, usually re-create a window in the app when the dock icon is clicked
  }
});

Handling Renderer Crashes

The crashReporter also monitors your renderer processes. If a renderer process crashes (e.g., due to a JavaScript error or a native module issue in the web content), Electron will detect it.

The main process will then attempt to send a crash report, similar to a main process crash. You can listen for the 'render-process-gone' event on a BrowserWindow to detect renderer crashes.

Attaching Extra Context

Crash reports are more useful with context! You can add custom key-value pairs to your crash reports using crashReporter.addExtraParameter().

This allows you to include information like the user's ID, current application state, or specific feature flags, which can be invaluable for debugging.

const { app, crashReporter, BrowserWindow } = require('electron');

crashReporter.start({
  productName: 'MyElectronApp',
  companyName: 'MyCompany',
  submitURL: 'http://localhost:1127/post',
  uploadToServer: true,
});

// Add extra parameters BEFORE a crash occurs
crashReporter.addExtraParameter('userId', 'user123');
crashReporter.addExtraParameter('appState', 'editing_document');

app.whenReady().then(() => {
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
  });
  mainWindow.loadFile('index.html');

  // Simulate a crash for demonstration
  setTimeout(() => {
    console.log('Simulating crash with extra data...');
    process.crash();
  }, 5000);
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    // In macOS, usually re-create a window in the app when the dock icon is clicked
  }
});

Receiving Crash Reports

Once Electron sends a crash report, it needs a server to receive it. This server typically stores the report and might process it further, perhaps even symbolicate the stack traces.

You can set up your own basic HTTP endpoint or use a specialized crash reporting service like Sentry, Bugsnag, or Crashlytics, which provide robust dashboards and analytics.

Best Practices & Privacy

When implementing crash reporting, keep these in mind:

  • Privacy: Be careful not to send sensitive user data in crash reports.
  • Consent: Inform users that crash reports are being sent and give them an option to opt-out if possible.
  • Testing: Always test your crash reporting setup thoroughly to ensure reports are being sent and received correctly.
  • Symbolication: For native crashes, you'll need to symbolicate the reports to get human-readable stack traces.

Crash Reporting Quiz

Test your understanding of Electron's crash reporting.

Summary & What's Next

In this lesson, you learned how to set up crash reporting in your Electron application. We covered initializing the crashReporter, configuring its options, simulating crashes, and adding extra data to reports.

Implementing crash reporting is a crucial step towards building robust and reliable desktop applications. Next, consider exploring specific crash reporting services to manage and analyze your collected data more effectively!

Questions Fréquemment Posées

La leçon « Rapports d’erreurs » est-elle gratuite ?

Oui — le texte complet de « Rapports d’erreurs » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Electron Desktop App Development, passe à CoddyKit PRO. Le cours Electron Desktop App Development comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Rapports d’erreurs » ?

Configurez la collecte de rapports d’erreurs pour recueillir et envoyer automatiquement les journaux de plantage, afin de vous aider à identifier et corriger les problèmes critiques de votre applicat… Tu pratiques Electron Desktop App Development avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Electron Desktop App Development ?

Aucune expérience préalable n'est requise. Electron Desktop App Development sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.

Combien de temps prend la leçon « Rapports d’erreurs » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Electron Desktop App Development ?

Oui. Chaque leçon Electron Desktop App Development inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Gérer les événements du cycle de vie de l’application
  2. Mettre en œuvre les mises à jour automatiques
  3. Rapports d’erreurs
  4. Liens profonds et gestionnaires de protocoles
← Retour à Electron Desktop App Development