-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should data collections throw if data files are invalid? #1736
Comments
GPT-4o response on the matterIntroducing error handling by throwing exceptions for invalid data in the HydePHP's file-based collections feature could indeed improve the developer experience (DX). However, it's essential to balance robustness with usability. Here are some considerations and potential improvements: Considerations for Throwing Exceptions
Potential Downsides
Suggested ImprovementsTo enhance the file-based collections feature while maintaining great DX, consider implementing a hybrid approach that offers both strict error handling and flexibility. Here are some specific suggestions:
Example Usage and Error HandlingHere's how the usage might look with configurable error handling: use Hyde\Support\DataCollection;
// In strict mode, this will throw an exception if there's an invalid JSON file
try {
$collection = DataCollection::json('testimonials');
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}
// In warning or silent mode, it will log the error (if configured) and continue
$collection = DataCollection::json('testimonials');
// Handle null entries if necessary
foreach ($collection as $file => $data) {
if ($data === null) {
echo "Warning: Invalid data in file $file\n";
} else {
// Process valid data
}
} ConclusionBy providing configurable error handling options, HydePHP can cater to both novice and experienced developers, offering immediate feedback during development while maintaining flexibility. This approach ensures a balance between robustness and usability, enhancing the overall developer experience. Final RecommendationBased on your goals of clear feedback and maintaining simplicity, I recommend proceeding with throwing exceptions for invalid data, accompanied by logging detailed error messages. This approach will provide the immediate and clear feedback you desire without introducing significant complexity. |
In response to GPT: We could add a build warning, and I have considered previously adding a "strict" mode to control these things (though this is not the place I would first implement this), and we could add it to site validations. But given that there is an inconsistency we should fix ( |
Additionally, consider the impact of a syntax error in a data file, worst case empty data is shipped to production, or the developer is confused. We solve both of this by letting them know that something is wrong. |
I'm considering if empty files should be syntax errors, |
Note that the Symfony YAML parser throws We may want to add a custom throw if the file is empty, to match |
GPT 4-o response for "Should an empty file be considered invalid?"Deciding whether an empty file should be considered invalid depends on the context and intended usage within HydePHP. Here are some considerations to help you determine the best approach: Considerations
RecommendationsGiven the goals of providing clear feedback and maintaining simplicity, here are two approaches you can consider: 1. Treat Empty Files as InvalidPros:
Cons:
Implementation: class DataCollection
{
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public static function json(string $name, bool $asArray = false)
{
try {
$filePath = "resources/collections/{$name}.json";
$content = file_get_contents($filePath);
// Check if the file is empty
if (empty($content)) {
throw new InvalidDataException('File is empty');
}
// Load and parse JSON file
$data = json_decode($content, $asArray);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidDataException('Invalid JSON syntax');
}
return $data;
} catch (\Exception $e) {
// Log detailed error message
$this->logger->error("Error loading JSON collection '{$name}': " . $e->getMessage());
// Throw exception for immediate feedback
throw $e;
}
}
} 2. Allow Empty Files with WarningsPros:
Cons:
Implementation: class DataCollection
{
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public static function json(string $name, bool $asArray = false)
{
try {
$filePath = "resources/collections/{$name}.json";
$content = file_get_contents($filePath);
// Check if the file is empty and log a warning
if (empty($content)) {
$this->logger->warning("Warning: File '{$filePath}' is empty.");
return null;
}
// Load and parse JSON file
$data = json_decode($content, $asArray);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidDataException('Invalid JSON syntax');
}
return $data;
} catch (\Exception $e) {
// Log detailed error message
$this->logger->error("Error loading JSON collection '{$name}': " . $e->getMessage());
// Throw exception for immediate feedback
throw $e;
}
}
} Final RecommendationGiven the importance of clear feedback and the potential interruptions during development, a hybrid approach might be most beneficial. Treat empty files as invalid by default, but consider logging a warning instead of throwing an exception initially. This provides flexibility while still notifying developers of potential issues. This balanced approach ensures that data integrity is maintained without significantly disrupting the development workflow. You can gather feedback from developers and adjust the approach as needed based on their experiences. |
This better matches the JSON exception throwing. See #1736
Should we throw if the wrong collection type is called? For example, if "foo" only has ".md" files, but Edit: Actually just having it be empty is prob sufficient here |
Fixed in #1732 |
Exploring ways to improve the file-based data collections feature for 2.x. One of these changes is to throw exceptions if data is invalid.
For example: Currently, a JSON file with bad syntax is returned as null, while YAML collections will throw an error, which is inconsistent. The main goal with HydePHP is to have great developer experience (DX), so I'm thinking if introducing exceptions for all invalid data types is the right approach.
My main advantage is direct and clear feedback on what has gone wrong, however a drawback is being interrupted when developing a data file.
The text was updated successfully, but these errors were encountered: