{"record":{"id":"b046cfda31bb8b5f","repo":"gleam-lang/gleam","slug":"panic-expression-evaluated","errorCode":null,"errorMessage":"`panic` expression evaluated.","messagePattern":"`panic` expression evaluated\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/javascript/expression.rs","lineNumber":2584,"sourceCode":"        let message = match message {\n            Some(m) => self.not_in_tail_position(None, |this| this.wrap_expression(arena, m)),\n            None => string(\n                arena,\n                \"`todo` expression evaluated. This code has not yet been implemented.\",\n            ),\n        };\n        self.throw_error(arena, \"todo\", &message, *location, vec![])\n    }\n\n    fn panic(\n        &mut self,\n        arena: &'doc DocumentArena<'a, 'doc>,\n        location: &'a SrcSpan,\n        message: Option<&'a TypedExpr>,\n    ) -> Document<'a, 'doc> {\n        let message = match message {\n            Some(m) => self.not_in_tail_position(None, |this| this.wrap_expression(arena, m)),\n            None => string(arena, \"`panic` expression evaluated.\"),\n        };\n        self.throw_error(arena, \"panic\", &message, *location, vec![])\n    }\n\n    pub(crate) fn throw_error<Fields>(\n        &mut self,\n        arena: &'doc DocumentArena<'a, 'doc>,\n        error_name: &'a str,\n        message: &Document<'a, 'doc>,\n        location: SrcSpan,\n        fields: Fields,\n    ) -> Document<'a, 'doc>\n    where\n        Fields: IntoIterator<Item = (&'a str, Document<'a, 'doc>)>,\n    {\n        self.tracker.make_error_used = true;\n        let module = self.module_name.clone().to_doc(arena).surround(\n            arena,","sourceCodeStart":2566,"sourceCodeEnd":2602,"githubUrl":"https://github.com/gleam-lang/gleam/blob/7e623aa83da3776faee50ca4ab9a6c40124acd95/compiler-core/src/javascript/expression.rs#L2566-L2602","documentation":"Gleam's `panic` keyword is compiled to JavaScript as a `throw` of a `GleamError` built by the generated `throwMakeError(...)` call (compiler-core/src/javascript/expression.rs:2584-2634). When the `panic` expression has no custom label (`panic as \"...\"`), the default message is exactly \"`panic` expression evaluated.\". The thrown error also carries the originating .gleam module, line number, and function name, so it pinpoints which panic site was reached. This is the designed runtime abort for Gleam programs compiled to JavaScript, not a compiler bug.","triggerScenarios":"Executing generated JavaScript (via `gleam run` on the JavaScript target, or importing the emitted .mjs from Node) in a way that reaches a message-less `panic`: a bare `panic` in a function body, a `case` branch ending in `panic`, or a dependency helper that panics on the input you passed (e.g. an 'impossible' empty-input branch).","commonSituations":"Scaffolding code where `panic` was left as a placeholder; a case branch believed unreachable that is in fact reachable at runtime; unexpected inputs such as an empty list or a missing key reaching a panicking helper inside a published Hex package.","solutions":["Read the thrown error's module/line/function fields (e.g. module `my_module`, line 42, function `head`) and open that .gleam file — that is the exact panic site","Replace the panicking branch with real handling: return `Result(t, e)` or handle the case instead of aborting","If the abort is intentional, add a label `panic as \"descriptive message\"` so the next failure names the cause","If the panic is inside a dependency, check the call's preconditions (non-empty list, present key, valid range) and validate before calling"],"exampleFix":"// before (src/my_module.gleam)\npub fn head(list: List(a)) -> a {\n  case list {\n    [x, ..] -> x\n    [] -> panic\n  }\n}\n\n// after\npub fn head(list: List(a)) -> Result(a, Nil) {\n  case list {\n    [x, ..] -> Ok(x)\n    [] -> Error(Nil)\n  }\n}","handlingStrategy":"try-catch","validationCode":"// Gleam: give callers a checkable value instead of an abort\npub fn head(list: List(a)) -> Result(a, Nil) {\n  case list {\n    [x, ..] -> Ok(x)\n    [] -> Error(Nil)\n  }\n}","typeGuard":null,"tryCatchPattern":"// JavaScript host around generated Gleam .mjs\nimport * as myModule from \"./build/dev/javascript/my_pkg/my_module.mjs\";\nimport { GleamError } from \"./build/dev/javascript/my_pkg/gleam.mjs\";\n\ntry {\n  myModule.doThing();\n} catch (e) {\n  if (e instanceof GleamError) {\n    // payload identifies the panic site (module, line, function)\n    console.error(\"Gleam panic reached:\", e);\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Prefer Result-returning helpers over panicking ones for any input-dependent path","Never leave a bare `panic` in scaffolding that ships; at minimum label it `panic as \"reason\"`","Treat any panic reached in production output as a defect to fix at the panic site, not an exception to catch routinely"],"tags":["gleam","javascript","codegen","panic","runtime-error"],"backgroundTag":"explicit-panic-executed","analyzedSha":"7e623aa83da3776faee50ca4ab9a6c40124acd95","analyzedAt":"2026-08-17T00:07:02.091Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}