Expose caught panics and their stack to error reporters

Open
#155 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Read the public error API and laravel_core::errors::panic::Panic, then trace how Errors::report_all and into_error attach panic traces. Check the existing trace capture path and its wants_trace() condition to understand production behavior. Done means reporters can identify a panic and access its message, location, and panic-site stack in every environment; the issue leaves the public stack type open for design.

Written by the indexing model from the issue text.

Description

Summary

Please let error reporters get a caught panic, with its message, location, and the stack recorded where it panicked, from the laravel::Error that Errors::report_all receives. It should work in every environment, not only with APP_DEBUG on.

Concretely, this would mean:

  1. Re-exporting the panic type.
  2. Exposing its stack, recorded in production too.

Problem

When Laravel Rust catches a panic, it reports a laravel::Error that wraps laravel_core::errors::panic::Panic. An error-tracking integration needs three things from it:

  1. Whether it's a panic, to report it as a crash (mechanism panic) rather than an ordinary error.
  2. Where it happened: message, file, line, and column, which trackers use for grouping and issue titles.
  3. The stack at the panic site, recorded before unwinding. This is the most valuable part of a crash report.

None of these is available through the laravel crate:

  • The type: Panic is public in laravel-rust-core, but laravel::errors doesn't re-export it, so reporters can't downcast_ref::<Panic>().
  • The location: Panic::location() exists, but PanicLocation's column is private, and Panic has no message() getter.
  • The stack: Laravel's hook already records it and ties it to the reported error, since into_error matches the panic and calls set_trace. But it does so only when wants_trace() is true (debug mode), and the trace is readable only through the crate-internal Internals::trace.

Our workaround

PostHog/posthog-rs#426 (posthog::laravel) works around this as follows:

  • At boot it installs its own panic hook, chained after Laravel's. The hook records each panic's stack, message, and file:line:column in a 32-entry buffer.
  • It treats a reported error as a panic when error.type_name() == "laravel::errors::panic::Panic".
  • It parses the error's display text, panic at {file}:{line}:{column}: {message} or panic: {message}, and matches it to a buffered stack. The file path has to be suffix-matched, because Laravel shortens it.

This depends on implementation details:

  • A 0.1.x release that moves the type or changes the text still compiles, but panics silently lose their stacks or stop being recognised as panics. We only notice because a test asserts that panics keep a stack.
  • Two panics with the same message and location can be matched to the wrong stack.
  • It adds a second process-wide panic hook. That hook has to be installed after Laravel's, which doesn't pass caught panics on to earlier hooks, and it's sensitive to the order other libraries install hooks in.

Proposed solution

Both parts below together:

  1. Re-export the panic type. Expose laravel::errors::panic::{Panic, PanicLocation}, with public Panic::message() and PanicLocation::column(). Reporters can then check error.downcast_ref::<Panic>() with no type-name or text matching.

  2. Expose the stack recorded at the panic site, in production too. Laravel's hook already records it and attaches it to the right error. It only needs to:

    • record it outside debug mode, either always or behind a setting such as Errors::capture_panic_traces(true);
    • expose it publicly, for example Panic::trace() or Error::panic_trace().

    Recording an unresolved stack only walks the frames, symbol lookup can be done later or on the tracker's server, and panics are rare, so the cost is small.

With both, integrations can drop their own panic hook, the buffer, the text parsing, and the hook-ordering workarounds. Every reporter, whether logs, PostHog, Sentry, or others, gets the same data from the error it already receives.

Open question: the stack type
  • Exposing backtrace::Backtrace ties Laravel's public API to that crate's version.
  • std::backtrace::Backtrace can't enumerate frames on stable Rust.
  • A small type of our own, such as the frames' instruction addresses, perhaps with a lazy symbolicated view, avoids both problems. It's also what error trackers that symbolicate on the server want.

Alternatives considered

  • Keep matching the type name and parsing the text. This is our current approach: it works on 0.1.4 and 0.1.5, but it breaks silently.
  • Re-export only, without the stack. This removes the text parsing, but integrations still need their own panic hook to get a stack, and with it the hook-ordering problems.
  • A separate panic-only reporter, such as Errors::report_panics(...). Integrations usually want one reporter for every failure and branch on whether it's a panic, so data on the error itself is more useful.
  • Accessors such as Error::is_panic() and Error::panic_location(). These are convenient, but they don't provide the stack.
  • A global panic hook only. Without a link to the reported laravel::Error, a stack can't be reliably tied to its request and report when panics on several threads interleave.

Context

This came up while building the PostHog integration (PostHog/posthog-rs#426). Any error tracker would need the same. Related: #154, a shutdown-only hook requested for the same integration.

Dominant language
Rust
Stars
2.2k
Forks
105
Avg merge
7h 19m
Merged PRs (30d)
41

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from laravel/rust

All issues in laravel/rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.