-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implement new exception and remove redundant validation in dto
- Loading branch information
Showing
12 changed files
with
176 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/libecalc/domain/infrastructure/energy_components/component_validation_error.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
import yaml | ||
|
||
from libecalc.presentation.yaml.file_context import FileContext | ||
from libecalc.presentation.yaml.validation_errors import Location | ||
|
||
|
||
@dataclass | ||
class ModelValidationError: | ||
message: str | ||
name: Optional[str] = None | ||
location: Optional[Location] = None | ||
data: Optional[dict] = None | ||
file_context: Optional[FileContext] = None | ||
|
||
@property | ||
def yaml(self) -> Optional[str]: | ||
if self.data is None: | ||
return None | ||
|
||
return yaml.dump(self.data, sort_keys=False).strip() | ||
|
||
def error_message(self): | ||
msg = "" | ||
if self.file_context is not None: | ||
msg += f"Object starting on line {self.file_context.start.line_number}\n" | ||
yaml = self.yaml | ||
if yaml is not None: | ||
msg += "...\n" | ||
msg += yaml | ||
msg += "\n...\n\n" | ||
|
||
if self.location is not None and not self.location.is_empty(): | ||
msg += f"Location: {self.location.as_dot_separated()}\n" | ||
|
||
if self.name is not None: | ||
msg += f"Name: {self.name}\n" | ||
|
||
msg += f"Message: {self.message}\n" | ||
return msg | ||
|
||
def __str__(self): | ||
return self.error_message() | ||
|
||
|
||
class ComponentValidationException(Exception): | ||
def __init__(self, errors: list[ModelValidationError]): | ||
self._errors = errors | ||
|
||
def errors(self) -> list[ModelValidationError]: | ||
return self._errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.