การรายงานข้อขัดข้อง
ตั้งค่าการรายงานข้อขัดข้องเพื่อรวบรวมและส่งบันทึกข้อขัดข้องโดยอัตโนมัติ ช่วยให้คุณระบุและแก้ไขปัญหาสำคัญในแอปพลิเคชัน
การรายงานข้อขัดข้อง เป็นบทเรียน Electron Desktop App Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Electron Desktop App Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Electron Desktop App Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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 totrueto 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!
คำถามที่พบบ่อย
บทเรียน “การรายงานข้อขัดข้อง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การรายงานข้อขัดข้อง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Electron Desktop App Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Electron Desktop App Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การรายงานข้อขัดข้อง”
ตั้งค่าการรายงานข้อขัดข้องเพื่อรวบรวมและส่งบันทึกข้อขัดข้องโดยอัตโนมัติ ช่วยให้คุณระบุและแก้ไขปัญหาสำคัญในแอปพลิเคชัน คุณปฏิบัติ Electron Desktop App Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Electron Desktop App Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Electron Desktop App Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การรายงานข้อขัดข้อง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Electron Desktop App Development นี้ได้ไหม
ได้ บทเรียน Electron Desktop App Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดการเหตุการณ์วงจรการทำงานของแอป
- การนำการอัปเดตอัตโนมัติไปใช้
- การรายงานข้อขัดข้อง
- การเชื่อมโยงเชิงลึกและตัวจัดการโพรโทคอล