diff --git a/airflow/api_connexion/endpoints/variable_endpoint.py b/airflow/api_connexion/endpoints/variable_endpoint.py index ee29e38d7ff9..54d5ac744b6c 100644 --- a/airflow/api_connexion/endpoints/variable_endpoint.py +++ b/airflow/api_connexion/endpoints/variable_endpoint.py @@ -63,7 +63,7 @@ def get_variable(*, variable_key: str, session: Session = NEW_SESSION) -> Respon """Get a variable by key.""" var = session.scalar(select(Variable).where(Variable.key == variable_key).limit(1)) if not var: - raise NotFound("Variable not found") + raise NotFound("Variable not found", detail="Variable does not exist") return variable_schema.dump(var) @@ -116,6 +116,8 @@ def patch_variable( raise BadRequest("Invalid post body", detail="key from request body doesn't match uri parameter") non_update_fields = ["key"] variable = session.scalar(select(Variable).filter_by(key=variable_key).limit(1)) + if not variable: + raise NotFound("Variable not found", detail="Variable does not exist") if update_mask: data = extract_update_mask_data(update_mask, non_update_fields, data) for key, val in data.items(): diff --git a/tests/api_connexion/endpoints/test_variable_endpoint.py b/tests/api_connexion/endpoints/test_variable_endpoint.py index 7c6c55d78397..c622e0d673ce 100644 --- a/tests/api_connexion/endpoints/test_variable_endpoint.py +++ b/tests/api_connexion/endpoints/test_variable_endpoint.py @@ -244,6 +244,21 @@ def test_should_update_variable_with_mask(self, session): _check_last_log(session, dag_id=None, event="variable.edit", execution_date=None) def test_should_reject_invalid_update(self): + response = self.client.patch( + "/api/v1/variables/var1", + json={ + "key": "var1", + "value": "foo", + }, + environ_overrides={"REMOTE_USER": "test"}, + ) + assert response.status_code == 404 + assert response.json == { + "title": "Variable not found", + "status": 404, + "type": EXCEPTIONS_LINK_MAP[404], + "detail": "Variable does not exist", + } Variable.set("var1", "foo") response = self.client.patch( "/api/v1/variables/var1",