Skip to content

Commit ca7d8bd

Browse files
committed
Simplify error handling for loading settings.
I now log a warning per-setting if they are not valid. This gets rid of a bunch of error combining logic. We may want to revert to that if we upgrade these to errors - if so, you can see the previous commit.
1 parent 5f8b28e commit ca7d8bd

File tree

1 file changed

+7
-24
lines changed

1 file changed

+7
-24
lines changed

src/labthings_fastapi/thing.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import json
1010
from typing import TYPE_CHECKING, Any, Optional
1111
from pydantic import ValidationError
12-
from pydantic_core import InitErrorDetails
1312
from typing_extensions import Self
1413
from collections.abc import Mapping
1514
import logging
@@ -217,50 +216,34 @@ def load_settings(self) -> None:
217216
settings = json.load(file_obj)
218217
if not isinstance(settings, Mapping):
219218
raise TypeError("The settings file must be a JSON object.")
220-
validation_errors: dict[str, ValidationError] = {}
221219
for name, value in settings:
222220
try:
223221
setting = self.settings[name]
224222
# Load the key from the JSON file using the setting's model
225223
model = setting.model.model_validate(value)
226224
setting.set_without_emit_from_model(model)
227-
except ValidationError as e:
228-
validation_errors[name] = e
225+
except ValidationError:
226+
self.logger.warning(
227+
f"Could not load setting {name} from settings file "
228+
f"because of a validation error.",
229+
exc_info=True,
230+
)
229231
except KeyError:
230232
self.logger.warning(
231233
f"An extra key {name} was found in the settings file. "
232234
"It will be deleted the next time settings are saved."
233235
)
234-
if validation_errors:
235-
# If one or more settings didn't validate, combine them into a single
236-
# Pydantic validation error.
237-
error_details: list[InitErrorDetails] = []
238-
for name, exc in validation_errors.items():
239-
for err in exc.errors():
240-
error_details.append(
241-
{
242-
"type": err["type"],
243-
"loc": (name,) + err["loc"],
244-
"input": err["input"],
245-
"ctx": err["ctx"] if "ctx" in err else {},
246-
}
247-
)
248-
raise ValidationError.from_exception_data(
249-
"Some settings were not valid.",
250-
error_details,
251-
)
252236
except (
253237
FileNotFoundError,
254238
JSONDecodeError,
255239
PermissionError,
256-
ValidationError,
257240
TypeError,
258241
):
259242
# Note that if the settings file is missing, we should already have returned
260243
# before attempting to load settings.
261244
self.logger.warning(
262245
"Error loading settings for %s. "
263-
"These settings will be reset to default.",
246+
"Settings for this Thing will be reset to default.",
264247
thing_name,
265248
)
266249
finally:

0 commit comments

Comments
 (0)