{"record":{"id":"03e86513eb561f33","repo":"dbt-labs/dbt-core","slug":"message-if-exception","errorCode":null,"errorMessage":"{message_if_exception}","messagePattern":"\\{message_if_exception\\}","errorType":"exception","errorClass":"InvalidOperation","httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-utils/src/functions/base.rs","lineNumber":813,"sourceCode":"/// Example:\n/// ```jinja\n/// {% set result = try_or_compiler_error(\"Error\", my_function, arg1, arg2, kwarg1=\"value1\", kwarg2=\"value2\") %}\n/// ```\npub fn try_or_compiler_error_fn()\n-> impl Fn(&State<'_, '_>, &[Value], Kwargs) -> Result<Value, Error> {\n    move |state: &State<'_, '_>, args: &[Value], kwargs: Kwargs| -> Result<Value, Error> {\n        let mut args = ArgParser::new(args, Some(kwargs));\n        let message_if_exception = args.get::<String>(\"message_if_exception\")?;\n        let func = args.get::<Value>(\"func\")?;\n        let mut remaining_args = args.get_args_as_vec_of_values();\n        let drained_kwargs = args.drain_kwargs();\n        let remaining_kwargs = Kwargs::from_iter(drained_kwargs);\n        remaining_args.push(remaining_kwargs.into());\n\n        match func.call(state, &remaining_args, &[]) {\n            Ok(result) => Ok(result),\n            // TODO: we need to raise CompilationError(message_if_exception, self.model)\n            Err(_) => Err(Error::new(\n                ErrorKind::InvalidOperation,\n                message_if_exception,\n            )),\n        }\n    }\n}\n\n/// Return an iterator of tuples where each tuple contains the i-th element from each of the input iterables.\n/// dbt's zip also supports a custom kwarg `fillvalue` (default=None) to match the longest iterable.\n///\n/// Args:\n///     *iterables: Two or more iterables\n///     fillvalue: (optional) Value to fill in shorter iterables, default=None\n///\n/// Example:\n/// ```jinja\n/// {% set list1 = [1, 2] %}\n/// {% set list2 = ['a', 'b', 'c'] %}","sourceCodeStart":795,"sourceCodeEnd":831,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-utils/src/functions/base.rs#L795-L831","documentation":"try_or_compiler_error(message_if_exception, fn, ...) invokes a wrapped dbt Jinja function and, if the call fails, raises an InvalidOperation error whose message is the caller-supplied message_if_exception string. The thrown message is dynamic — it is whatever you passed as the first argument — so this error text reflects your own template's message, not a fixed library string.","triggerScenarios":"Any invocation where the wrapped function call (func.call) returns Err: wrong arguments to the wrapped function, non-iterable inputs, or any failure of the inner dbt function being guarded. The outer try_or_compiler_error converts it into your message.","commonSituations":"Using try_or_compiler_error around functions like zip/set during model compilation and hitting failures on bad inputs; relying on the wrapper during migration debugging and needing the real underlying cause, which is swallowed by this message.","solutions":["Inspect the wrapped function's inputs — the wrapper hides the underlying error, so validate arguments before the call","Pass a descriptive message_if_exception so the thrown CompilationError pinpoints the model and inputs","Temporarily replace try_or_compiler_error with a direct call to see the real underlying error","Fix the inner call so it succeeds rather than relying on the guard"],"exampleFix":"// before\n{% set x = try_or_compiler_error('bad input', zip(1, 2)) %}\n// after\n{% if 1 is sequence and 2 is sequence %}\n  {% set x = try_or_compiler_error('zip args must be lists: ' ~ this, zip([1], [2])) %}\n{% endif %}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"{# try_or_compiler_error swallows the inner error; validate inputs first #}\n{% if a is sequence and b is sequence %}\n  {% set x = try_or_compiler_error('zip failed in ' ~ this, zip(a, b)) %}\n{% else %}\n  {{ exceptions.raise_compiler_error('zip inputs must be lists in ' ~ this) }}\n{% endif %}","preventionTips":["Validate the wrapped function's arguments before the call — the wrapper hides root causes","Include model context ('this') in message_if_exception for debuggability","Temporarily unwrap the call to see the real error during debugging","Keep message_if_exception descriptive and specific"],"tags":["jinja","dbt","wrapped-error"],"backgroundTag":"invalid-argument","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}