Repository: dbt-labs/dbt-core

Issue:
[CT-2834] [Bug] Unable to convert deprecation warnings to errors
### 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

`PASS`ing console output and a zero exit code instead of the expected behavior below.

### Expected Behavior

[`WARN_ERROR_OPTIONS`](https://docs.getdbt.com/reference/global-configs/warnings) gives a mechanism whereby users can promote these warnings to actual runtime errors, and I expected a red `FAIL` message in the console output and a non-zero exit code.

### Steps To Reproduce

`models/my_model.sql`
```sql
{{ config(materialized="table") }}

select 0 as id
```

`models/_models.yml`
```yaml
models:
  - name: my_model
    deprecation_date: 1999-01-01
```

I was hoping for both of these to error out and have a non-zero exit code:
```
dbt --warn-error run
echo $?

dbt --warn-error-options '{"include": ["DeprecatedModel"]}' run
echo $?
```

### Relevant log output

_No response_

### Environment

```markdown
- OS:
- Python:
- dbt: 1.6.0rc1
```


### Which database adapter are you using with dbt?

postgres

### Additional Context

_No response_


Discussion:

Resolving patch:
diff --git a/core/dbt/events/types.py b/core/dbt/events/types.py
--- a/core/dbt/events/types.py
+++ b/core/dbt/events/types.py
@@ -1153,10 +1153,11 @@ def code(self):
 
     def message(self) -> str:
         version = ".v" + self.model_version if self.model_version else ""
-        return (
+        msg = (
             f"Model {self.model_name}{version} has passed its deprecation date of {self.deprecation_date}. "
             "This model should be disabled or removed."
         )
+        return warning_tag(msg)
 
 
 class UpcomingReferenceDeprecation(WarnLevel):
@@ -1178,7 +1179,7 @@ def message(self) -> str:
             )
             msg = msg + coda
 
-        return msg
+        return warning_tag(msg)
 
 
 class DeprecatedReference(WarnLevel):
@@ -1200,7 +1201,7 @@ def message(self) -> str:
             )
             msg = msg + coda
 
-        return msg
+        return warning_tag(msg)
 
 
 class UnsupportedConstraintMaterialization(WarnLevel):
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
@@ -562,7 +562,7 @@ def check_for_model_deprecations(self):
                     node.deprecation_date
                     and node.deprecation_date < datetime.datetime.now().astimezone()
                 ):
-                    fire_event(
+                    warn_or_error(
                         DeprecatedModel(
                             model_name=node.name,
                             model_version=version_to_str(node.version),
@@ -581,7 +581,7 @@ def check_for_model_deprecations(self):
                         else:
                             event_cls = UpcomingReferenceDeprecation
 
-                        fire_event(
+                        warn_or_error(
                             event_cls(
                                 model_name=node.name,
                                 ref_model_package=resolved_ref.package_name,


Test patch:
diff --git a/tests/functional/deprecations/model_deprecations.py b/tests/functional/deprecations/model_deprecations.py
--- a/tests/functional/deprecations/model_deprecations.py
+++ b/tests/functional/deprecations/model_deprecations.py
@@ -1,6 +1,9 @@
 import pytest
 
+from dbt.exceptions import EventCompilationError
 from dbt.cli.main import dbtRunner
+from dbt.tests.util import run_dbt
+
 
 deprecated_model__yml = """
 version: 2
@@ -41,6 +44,14 @@ def test_deprecation_warning(self, project):
         assert len(matches) == 1
         assert matches[0].data.model_name == "my_model"
 
+    def test_deprecation_warning_error(self, project):
+        with pytest.raises(EventCompilationError):
+            run_dbt(["--warn-error", "parse"])
+
+    def test_deprecation_warning_error_options(self, project):
+        with pytest.raises(EventCompilationError):
+            run_dbt(["--warn-error-options", '{"include": ["DeprecatedModel"]}', "parse"])
+
 
 class TestReferenceDeprecatingWarning:
     @pytest.fixture(scope="class")
@@ -59,6 +70,16 @@ def test_deprecation_warning(self, project):
         assert matches[0].data.model_name == "my_dependant_model"
         assert matches[0].data.ref_model_name == "my_model"
 
+    def test_deprecation_warning_error(self, project):
+        with pytest.raises(EventCompilationError):
+            run_dbt(["--warn-error", "parse"])
+
+    def test_deprecation_warning_error_options(self, project):
+        with pytest.raises(EventCompilationError):
+            run_dbt(
+                ["--warn-error-options", '{"include": ["UpcomingReferenceDeprecation"]}', "parse"]
+            )
+
 
 class TestReferenceDeprecatedWarning:
     @pytest.fixture(scope="class")
@@ -76,3 +97,11 @@ def test_deprecation_warning(self, project):
         assert len(matches) == 1
         assert matches[0].data.model_name == "my_dependant_model"
         assert matches[0].data.ref_model_name == "my_model"
+
+    def test_deprecation_warning_error(self, project):
+        with pytest.raises(EventCompilationError):
+            run_dbt(["--warn-error", "parse"])
+
+    def test_deprecation_warning_error_options(self, project):
+        with pytest.raises(EventCompilationError):
+            run_dbt(["--warn-error-options", '{"include": ["DeprecatedReference"]}', "parse"])