passsy / capture-stack-tracesr3
Capture Stack Traces in Catch Blocks
PublicCatch the stack trace together with the error and pass both along, because an error without its stack trace says what failed but not where.
tests, run by the registry7 passed · slowest 4.3 ms
✓fires2.7 ms
fires on a catch without a stack trace the agent writes
write lib/uploader.dartfired: line 4
Future<void> upload(Report report) async {
try {
await uploadReport(report);
} catch (e) {
log('upload failed: $e');
}
}✓quiet2.6 ms
stays quiet on a catch that keeps its stack trace
write lib/uploader.dartstayed quiet
try {
await uploadReport(report);
} catch (e, stackTrace) {
log('upload failed', error: e, stackTrace: stackTrace);
} on FormatException catch (e, stack) {
rethrow;
}✓quiet2.5 ms
stays quiet on catch (e) in a comment
write lib/hint.dartstayed quiet
/// Never write catch (e) here.✓quiet2.2 ms
stays quiet on a legacy catch when the agent edits elsewhere or reads
write lib/uploader.dart · wrote line 3stayed quiet
Future<void> upload(Report report) async {
try {
await uploadReport(report);
} catch (e) {
log('upload failed: $e');
}
}read lib/uploader.dartno handler of the detector wants this event
Future<void> upload(Report report) async {
try {
await uploadReport(report);
} catch (e) {
log('upload failed: $e');
}
}✓skips0.0 ms
stays quiet in generated code
write lib/uploader.g.dartno handler of the detector wants this event
Future<void> upload(Report report) async {
try {
await uploadReport(report);
} catch (e) {
log('upload failed: $e');
}
}✓fires4.3 ms
points at the principle when the user mentions a stack trace
prompt · in fixtures/dart_appfired: "stack trace"
The crash log has no stack trace, why?✓quiet2.4 ms
stays quiet on the same words outside a Dart project
promptstayed quiet
The crash log has no stack trace, why?detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, read, source, write } from "@wellactually/sdk/v3/test";
const loader = source`
Future<void> upload(Report report) async {
try {
await uploadReport(report);
} catch (e) {
log('upload failed: $e');
}
}
`;
test("fires on a catch without a stack trace the agent writes", () => {
expect(detect(write("lib/uploader.dart", loader))).toEqual([{ line: 4 }]);
});
test("stays quiet on a catch that keeps its stack trace", () => {
const code = source`
try {
await uploadReport(report);
} catch (e, stackTrace) {
log('upload failed', error: e, stackTrace: stackTrace);
} on FormatException catch (e, stack) {
rethrow;
}
`;
expect(detect(write("lib/uploader.dart", code))).toEqual([]);
});
test("stays quiet on catch (e) in a comment", () => {
const code = source`
/// Never write catch (e) here.
`;
expect(detect(write("lib/hint.dart", code))).toEqual([]);
});
test("stays quiet on a legacy catch when the agent edits elsewhere or reads", () => {
expect(detect(edit("lib/uploader.dart", loader, { written: " await uploadReport(report);" }))).toEqual([]);
expect(detect(read("lib/uploader.dart", loader))).toEqual([]);
});
test("stays quiet in generated code", () => {
expect(detect(write("lib/uploader.g.dart", loader))).toEqual([]);
});
const dartApp = { project: "fixtures/dart_app" };
test("points at the principle when the user mentions a stack trace", () => {
expect(detect(prompt("The crash log has no stack trace, why?"), dartApp)).toEqual([{ match: "stack trace", depth: "pointer" }]);
});
test("stays quiet on the same words outside a Dart project", () => {
expect(detect(prompt("The crash log has no stack trace, why?"))).toEqual([]);
});