{"record":{"id":"336d291681836de4","repo":"clockworklabs/SpacetimeDB","slug":"view-call-failed-err","errorCode":null,"errorMessage":"view call failed: {err}","messagePattern":"view call failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/host/wasmtime/wasm_instance_env.rs","lineNumber":1897,"sourceCode":"                sender,\n                args_source.0,\n                result_sink,\n                true,\n            )?;\n\n            Ok(code)\n        })();\n\n        caller.data_mut().instance_env.swap_func_type(prev_func_type);\n\n        let result_bytes = {\n            let env = caller.data_mut();\n            env.take_bytes_sink(nested_result_sink.expect(\"nested view result sink missing\"))\n        };\n        let code = call_result?;\n\n        decode_view_result_sink_code(code, result_bytes).map_err(|err| match err {\n            ViewResultSinkError::User(err) => anyhow!(\"view call failed: {err}\"),\n            ViewResultSinkError::UnexpectedCode(code) => anyhow!(\n                \"unexpected return code {code} from view call, expected 0, 2, or {failure}\",\n                failure = HOST_CALL_FAILURE.get()\n            ),\n        })\n    }\n\n    /// Aborts a mutable transaction,\n    /// blocking until the transaction has been aborted.\n    ///\n    /// Returns `0` on success, or an error code otherwise.\n    ///\n    /// # Traps\n    ///\n    /// This function does not trap.\n    ///\n    /// # Errors\n    ///","sourceCodeStart":1879,"sourceCodeEnd":1915,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/core/src/host/wasmtime/wasm_instance_env.rs#L1879-L1915","documentation":"After invoking a view through the wasm result sink, the host decodes the sink's return code and collected bytes. A user error code — the view itself reported a failure through the documented channel — is surfaced as `view call failed: {err}` carrying the view's own error message. This is the view's runtime error, not a host malfunction; unexpected codes get a different message.","triggerScenarios":"A view body returns an error during subscription evaluation or the in-procedure refresh path: raw-SQL views returning invalid SQL, view logic rejecting inputs (bad sender or args), or SDK-level error propagation inside the view.","commonSituations":"Raw-SQL views building queries from unvalidated data; views assuming row/column shapes that changed after a migration; views that error on edge-case inputs such as empty arguments or unknown senders.","solutions":["Read the embedded {err} — it is the error the view produced; fix the view logic or its SQL accordingly.","Parameterize and validate any SQL the view returns; never splice unescaped values into it.","Add unit tests for the view over edge-case inputs (empty tables, unusual args).","Republish the fixed module; materialized state corrects on the next evaluation."],"exampleFix":"// before: raw-SQL view builds a query from unvalidated input\nlet sql = format(\"SELECT * FROM jobs WHERE owner = '{}'\", sender.to_hex());\n\n// after: parameterized query, value bound at execution\nlet sql = \"SELECT * FROM jobs WHERE owner = $1\".to_string();\n// bind the sender identity as a parameter when executing","handlingStrategy":"try-catch","validationCode":"// for raw-SQL views: validate the query shape before returning it from the view\nlet sql = build_query(args);\nsqlparser::parse(&sql)?; // reject malformed SQL before the host executes it","typeGuard":null,"tryCatchPattern":"let err = call_view(...).unwrap_err();\nif let Some(msg) = err.to_string().strip_prefix(\"view call failed: \") {\n    // this is the view's own error message — fix the view logic or its SQL\n    report_view_error(msg);\n}","preventionTips":["Parameterize all SQL produced by raw-SQL views.","Unit-test views with empty tables and unusual arguments.","Handle error propagation in view code explicitly."],"tags":["views","view-execution","user-error","raw-sql"],"backgroundTag":"view-execution-error","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}