-
Notifications
You must be signed in to change notification settings - Fork 3k
fix(mobile): Persist crash diagnostics before runtime fatals #4033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
4
commits into
pingdotgg:main
Choose a base branch
from
juliusmarminge:codex/pr3910-crash-capture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1620f1f
fix(mobile): Persist crash diagnostics before runtime fatals
juliusmarminge 959a39b
refactor(mobile): Keep outbox attachment conversion with composer upl…
juliusmarminge af4ea04
fix(mobile): Preserve crash log capture ordering
juliusmarminge 6209d13
fix(mobile): Safely serialize fatal errors
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { Directory, File, Paths } from "expo-file-system"; | ||
| import type { ErrorUtils as ErrorUtilsInterface } from "react-native"; | ||
|
|
||
| const CRASH_LOG_DIRECTORY = "crash-logs"; | ||
| const MAX_PERSISTED_CRASH_LOGS = 20; | ||
| const REPORTED_ON_LAUNCH_COUNT = 3; | ||
|
|
||
| let crashSequence = 0; | ||
|
|
||
| function safeErrorMessage(error: unknown): string { | ||
| try { | ||
| return String(error); | ||
| } catch { | ||
| return "[Uncoercible error]"; | ||
| } | ||
| } | ||
|
|
||
| export function installCrashLogger(): void { | ||
| const errorUtils = (globalThis as { ErrorUtils?: ErrorUtilsInterface }).ErrorUtils; | ||
| if (errorUtils === undefined) { | ||
| return; | ||
| } | ||
| const previousHandler = errorUtils.getGlobalHandler(); | ||
| errorUtils.setGlobalHandler((error: unknown, isFatal?: boolean) => { | ||
| if (isFatal === true) { | ||
| try { | ||
| persistCrashRecord(error); | ||
| } catch { | ||
| // Never let the logger mask the original error. | ||
| } | ||
| } | ||
| previousHandler(error, isFatal); | ||
| }); | ||
| // Deferred so install (which runs before the app module graph) does no | ||
| // file IO on the startup path. | ||
| setTimeout(() => { | ||
| reportAndPrunePreviousCrashes(); | ||
| }, 0); | ||
| } | ||
|
|
||
| function persistCrashRecord(error: unknown): void { | ||
| const directory = new Directory(Paths.document, CRASH_LOG_DIRECTORY); | ||
| directory.create({ idempotent: true, intermediates: true }); | ||
| const cause = error instanceof Error ? error : null; | ||
| const record = { | ||
| capturedAt: new Date().toISOString(), | ||
| isFatal: true, | ||
| message: cause?.message ?? safeErrorMessage(error), | ||
| name: cause?.name ?? null, | ||
| stack: cause?.stack ?? null, | ||
| }; | ||
| crashSequence += 1; | ||
| // Keep lexical directory ordering aligned with capture order for records | ||
| // written within the same millisecond. | ||
| const sequence = String(crashSequence).padStart(6, "0"); | ||
| const file = new File(directory, `crash-${Date.now()}-${sequence}.json`); | ||
| file.create({ intermediates: true, overwrite: true }); | ||
| file.write(JSON.stringify(record, null, 2)); | ||
| } | ||
|
|
||
| function reportAndPrunePreviousCrashes(): void { | ||
| try { | ||
| const directory = new Directory(Paths.document, CRASH_LOG_DIRECTORY); | ||
| if (!directory.exists) { | ||
| return; | ||
| } | ||
| const files = directory | ||
| .list() | ||
| .filter( | ||
| (entry): entry is File => | ||
| entry instanceof File && entry.name.startsWith("crash-") && entry.name.endsWith(".json"), | ||
| ) | ||
| .sort((a, b) => a.name.localeCompare(b.name)); | ||
| for (const file of files.slice(-REPORTED_ON_LAUNCH_COUNT)) { | ||
| try { | ||
| console.warn(`[crash-log] previous fatal JS error (${file.name}):`, file.textSync()); | ||
| } catch { | ||
| // Skip unreadable records. | ||
| } | ||
| } | ||
| for (const file of files.slice(0, Math.max(0, files.length - MAX_PERSISTED_CRASH_LOGS))) { | ||
|
juliusmarminge marked this conversation as resolved.
|
||
| try { | ||
| file.delete(); | ||
| } catch { | ||
| // Leave undeletable records for the next prune. | ||
| } | ||
| } | ||
| } catch { | ||
| // Reporting past crashes must never affect startup. | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { installCrashLogger } from "./crashLog"; | ||
|
|
||
| installCrashLogger(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.