Repository: dbt-labs/dbt-core

Issue:
[CT-1312] [Bug] disabled flag not implemented for some metrics error messages
### Is this a new bug in dbt-core?

- [X] I believe this is a new bug in dbt-core
- [X] I have searched the existing issues, and I could not find an existing issue for this bug

### Current Behavior

`exceptions.py` has a lot of duplicated errors for `source_target_not_found`, `metric_target_not_found` and `exposure_target_not_found`. `exposure_target_not_found` is actually not needed since an exposure is never a target.

Additionally, `metric_target_not_found` was not passing in `disabled` so the error message would never explicitly say it was disabled, it instead included the less specific wording `was not found or is disabled`.

This was noticed while working on #5957.  It feels separate from that work, but related.  Doing as a separate PR to reduce PR size for #5957.  

### Expected Behavior

Exceptions are simpler produce specific messages.



Discussion:

Resolving patch:
diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py
--- a/core/dbt/context/providers.py
+++ b/core/dbt/context/providers.py
@@ -54,9 +54,8 @@
     ref_invalid_args,
     metric_invalid_args,
     ref_target_not_found,
-    metric_target_not_found,
+    target_not_found,
     ref_bad_context,
-    source_target_not_found,
     wrapped_exports,
     raise_parsing_error,
     disallow_secret_env_var,
@@ -542,10 +541,11 @@ def resolve(self, source_name: str, table_name: str):
         )
 
         if target_source is None or isinstance(target_source, Disabled):
-            source_target_not_found(
-                self.model,
-                source_name,
-                table_name,
+            target_not_found(
+                node=self.model,
+                target_name=f"{source_name}.{table_name}",
+                target_kind="source",
+                disabled=(isinstance(target_source, Disabled)),
             )
         return self.Relation.create_from_source(target_source)
 
@@ -568,11 +568,11 @@ def resolve(self, target_name: str, target_package: Optional[str] = None) -> Met
         )
 
         if target_metric is None or isinstance(target_metric, Disabled):
-            # TODO : Use a different exception!!
-            metric_target_not_found(
-                self.model,
-                target_name,
-                target_package,
+            target_not_found(
+                node=self.model,
+                target_name=target_name,
+                target_kind="metric",
+                target_package=target_package,
             )
 
         return ResolvedMetricReference(target_metric, self.manifest, self.Relation)
diff --git a/core/dbt/exceptions.py b/core/dbt/exceptions.py
--- a/core/dbt/exceptions.py
+++ b/core/dbt/exceptions.py
@@ -571,7 +571,9 @@ def doc_target_not_found(
 
 
 def _get_target_failure_msg(
-    model,
+    original_file_path,
+    unique_id,
+    resource_type_title,
     target_name: str,
     target_model_package: Optional[str],
     include_path: bool,
@@ -584,11 +586,11 @@ def _get_target_failure_msg(
 
     source_path_string = ""
     if include_path:
-        source_path_string = " ({})".format(model.original_file_path)
+        source_path_string = " ({})".format(original_file_path)
 
     return "{} '{}'{} depends on a {} named '{}' {}which {}".format(
-        model.resource_type.title(),
-        model.unique_id,
+        resource_type_title,
+        unique_id,
         source_path_string,
         target_kind,
         target_name,
@@ -598,9 +600,9 @@ def _get_target_failure_msg(
 
 
 def get_target_not_found_or_disabled_msg(
-    model,
-    target_model_name: str,
-    target_model_package: Optional[str],
+    node,
+    target_name: str,
+    target_package: Optional[str],
     disabled: Optional[bool] = None,
 ) -> str:
     if disabled is None:
@@ -610,9 +612,11 @@ def get_target_not_found_or_disabled_msg(
     else:
         reason = "was not found"
     return _get_target_failure_msg(
-        model,
-        target_model_name,
-        target_model_package,
+        node.original_file_path,
+        node.unique_id,
+        node.resource_type.title(),
+        target_name,
+        target_package,
         include_path=True,
         reason=reason,
         target_kind="node",
@@ -645,7 +649,9 @@ def get_not_found_or_disabled_msg(
     else:
         reason = "was not found"
     return _get_target_failure_msg(
-        node,
+        node.original_file_path,
+        node.unique_id,
+        node.resource_type.title(),
         target_name,
         target_package,
         include_path=True,
@@ -654,46 +660,22 @@ def get_not_found_or_disabled_msg(
     )
 
 
-def source_target_not_found(
-    model, target_name: str, target_table_name: str, disabled: Optional[bool] = None
-) -> NoReturn:
-    msg = get_not_found_or_disabled_msg(
-        node=model,
-        target_name=f"{target_name}.{target_table_name}",
-        target_kind="source",
-        disabled=disabled,
-    )
-    raise_compiler_error(msg, model)
-
-
-def metric_target_not_found(
-    metric, target_name: str, target_package: Optional[str], disabled: Optional[bool] = None
-) -> NoReturn:
-
-    msg = get_not_found_or_disabled_msg(
-        node=metric,
-        target_name=target_name,
-        target_kind="metric",
-        target_package=target_package,
-        disabled=disabled,
-    )
-
-    raise_compiler_error(msg, metric)
-
-
-def exposure_target_not_found(
-    exposure, target_name: str, target_package: Optional[str], disabled: Optional[bool] = None
+def target_not_found(
+    node,
+    target_name: str,
+    target_kind: str,
+    target_package: Optional[str] = None,
+    disabled: Optional[bool] = None,
 ) -> NoReturn:
-
     msg = get_not_found_or_disabled_msg(
-        node=exposure,
+        node=node,
         target_name=target_name,
-        target_kind="exposure",
+        target_kind=target_kind,
         target_package=target_package,
         disabled=disabled,
     )
 
-    raise_compiler_error(msg, exposure)
+    raise_compiler_error(msg, node)
 
 
 def dependency_not_found(model, target_model_name):
diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py
--- a/core/dbt/parser/manifest.py
+++ b/core/dbt/parser/manifest.py
@@ -73,9 +73,7 @@
 from dbt.exceptions import (
     ref_target_not_found,
     get_target_not_found_or_disabled_msg,
-    source_target_not_found,
-    metric_target_not_found,
-    exposure_target_not_found,
+    target_not_found,
     get_not_found_or_disabled_msg,
     warn_or_error,
 )
@@ -960,7 +958,10 @@ def invalid_ref_fail_unless_test(node, target_model_name, target_model_package,
 
     if node.resource_type == NodeType.Test:
         msg = get_target_not_found_or_disabled_msg(
-            node, target_model_name, target_model_package, disabled
+            node=node,
+            target_name=target_model_name,
+            target_package=target_model_package,
+            disabled=disabled,
         )
         if disabled:
             fire_event(InvalidRefInTestNode(msg=msg))
@@ -988,34 +989,31 @@ def invalid_source_fail_unless_test(node, target_name, target_table_name, disabl
         else:
             warn_or_error(msg, log_fmt=warning_tag("{}"))
     else:
-        source_target_not_found(node, target_name, target_table_name, disabled=disabled)
-
-
-def invalid_metric_fail_unless_test(node, target_metric_name, target_metric_package):
-
-    if node.resource_type == NodeType.Test:
-        msg = get_target_not_found_or_disabled_msg(node, target_metric_name, target_metric_package)
-        warn_or_error(msg, log_fmt=warning_tag("{}"))
-    else:
-        metric_target_not_found(
-            node,
-            target_metric_name,
-            target_metric_package,
+        target_not_found(
+            node=node,
+            target_name=f"{target_name}.{target_table_name}",
+            target_kind="source",
+            disabled=disabled,
         )
 
 
-def invalid_exposure_fail_unless_test(node, target_exposure_name, target_exposure_package):
+def invalid_metric_fail_unless_test(node, target_metric_name, target_metric_package, disabled):
 
     if node.resource_type == NodeType.Test:
         msg = get_target_not_found_or_disabled_msg(
-            node, target_exposure_name, target_exposure_package
+            node=node,
+            target_name=target_metric_name,
+            target_package=target_metric_package,
+            disabled=disabled,
         )
         warn_or_error(msg, log_fmt=warning_tag("{}"))
     else:
-        exposure_target_not_found(
-            node,
-            target_exposure_name,
-            target_exposure_package,
+        target_not_found(
+            node=node,
+            target_name=target_metric_name,
+            target_kind="metric",
+            target_package=target_metric_package,
+            disabled=disabled,
         )
 
 
@@ -1244,6 +1242,7 @@ def _process_metrics_for_node(
                 node,
                 target_metric_name,
                 target_metric_package,
+                disabled=(isinstance(target_metric, Disabled)),
             )
 
             continue


Test patch: