Skip to content

Commit f9d4190

Browse files
committed
improved warning messages detailed while throwing a dedicated error when environment variable is not resolved successfully
1 parent 77bb2df commit f9d4190

3 files changed

Lines changed: 16 additions & 16 deletions

File tree

backend/infrahub/webhook/models.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,26 @@ def from_event(cls, event_id: str, event_type: str, event_occured_at: str, event
117117
)
118118

119119

120+
class WebhookHeaderResolutionError(Exception):
121+
pass
122+
123+
120124
class WebhookHeader(BaseModel):
121125
key: str
122126
value: str
123127
kind: Literal["static", "environment"]
124128

125-
def resolve(self) -> str | None:
129+
def resolve(self) -> str:
126130
"""Resolve the header value based on its kind.
127131
128-
Returns None if the value cannot be resolved (e.g. missing environment variable).
132+
Raises WebhookHeaderResolutionError if the value cannot be resolved.
129133
"""
130134
if self.kind == "static":
131135
return self.value
132136

133137
resolved = os.environ.get(self.value)
134138
if resolved is None:
135-
logger.warning("Environment variable '%s' not found, skipping header '%s'", self.value, self.key)
136-
return None
139+
raise WebhookHeaderResolutionError(f"Environment variable '{self.value}' not found")
137140
return resolved
138141

139142

@@ -158,9 +161,10 @@ def _assign_headers(self, uuid: UUID | None = None, at: Timestamp | None = None)
158161
}
159162

160163
for header in self.custom_headers:
161-
resolved = header.resolve()
162-
if resolved is not None:
163-
self._headers[header.key] = resolved
164+
try:
165+
self._headers[header.key] = header.resolve()
166+
except WebhookHeaderResolutionError as exc:
167+
logger.warning("Webhook '%s': %s, skipping header '%s'", self.name, exc, header.key)
164168

165169
if self.shared_key:
166170
message_id = f"msg_{uuid.hex}" if uuid else f"msg_{uuid4().hex}"

backend/tests/unit/webhook/test_models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,4 @@ def test_assign_headers_skips_missing_environment_variable(caplog: pytest.LogCap
140140
assert webhook._headers["X-Source"] == "infrahub"
141141
assert "MISSING_VAR" in caplog.text
142142
assert "X-API-Key" in caplog.text
143+
assert "test" in caplog.text # webhook name included in warning

backend/tests/unit/webhook/test_webhook_header.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import logging
21
from unittest.mock import patch
32

43
import pytest
54

6-
from infrahub.webhook.models import WebhookHeader
5+
from infrahub.webhook.models import WebhookHeader, WebhookHeaderResolutionError
76

87

98
def test_webhook_header_model() -> None:
@@ -29,11 +28,7 @@ def test_webhook_header_resolve_environment() -> None:
2928
assert header.resolve() == "secret123"
3029

3130

32-
def test_webhook_header_resolve_missing_environment(caplog: pytest.LogCaptureFixture) -> None:
31+
def test_webhook_header_resolve_missing_environment() -> None:
3332
header = WebhookHeader(key="X-API-Key", value="MISSING_VAR", kind="environment")
34-
with patch.dict("os.environ", {}, clear=True), caplog.at_level(logging.WARNING, logger="infrahub.webhook.models"):
35-
result = header.resolve()
36-
37-
assert result is None
38-
assert "MISSING_VAR" in caplog.text
39-
assert "X-API-Key" in caplog.text
33+
with patch.dict("os.environ", {}, clear=True), pytest.raises(WebhookHeaderResolutionError, match="MISSING_VAR"):
34+
header.resolve()

0 commit comments

Comments
 (0)