[Common] Improve system initialization and error handling#2464
Open
Sn0w3y wants to merge 3 commits intoIceWhaleTech:mainfrom
Open
[Common] Improve system initialization and error handling#2464Sn0w3y wants to merge 3 commits intoIceWhaleTech:mainfrom
Sn0w3y wants to merge 3 commits intoIceWhaleTech:mainfrom
Conversation
This commit enhances the initialization process and error handling across various components of the system. Changes include the addition of error checks during the initialization phase in main.go, ensuring that the system halts with an appropriate error message if any part of the setup (config, database, services) fails. Additionally, the logging mechanism has been improved to use structured logging for better clarity and debugging. The config initialization now returns an error if it fails, promoting better error handling practices. Similarly, database connection errors are now properly handled, and the system will return a specific error if the connection fails. These changes aim to make the system more robust by ensuring that startup errors are clearly reported and logged, facilitating easier troubleshooting and enhancing overall system reliability.
This commit introduces several key changes aimed at optimizing file handling and improving code readability across various components. Notably, it includes the following modifications: 1. In `main.go`, a comment was added to clarify the purpose of the `embed` import, enhancing code understandability. 2. `pkg/config/init.go` saw a significant refactor in the configuration file's initialization process. The method now uses `os.OpenFile` with `O_EXCL` flag for atomic creation, reducing the risk of race conditions and simplifying error handling. 3. `pkg/utils/file/file.go` received multiple updates, including the removal of duplicate imports, introduction of a new function `readDirInfo` for efficient directory reading, and various optimizations in file and directory handling functions to improve performance and readability. 4. Adjustments in `pkg/utils/httper/zerotier.go`, `route/v1/file.go`, and `route/v1/system.go` were made to simplify parameter passing and enhance consistency in function signatures. 5. General code cleanup was performed, including the removal of unused imports and redundant code, and transitioning to `io.ReadAll` from `ioutil.ReadAll` following the deprecation of `ioutil` in Go 1.16. These changes collectively aim to make the codebase more efficient, readable, and maintainable.
This commit introduces several key changes to both the file handling logic and the HTTP header management for file downloads. In `pkg/utils/file/file.go`, the `CopyDir` function has been renamed to more accurately reflect its purpose, and its implementation has been refactored for clarity and efficiency. A new helper function, `copyDirContents`, has been added to streamline the process of copying the contents of a directory. Additionally, `copyEntry` has been introduced to handle the copying of individual directory entries, whether they are files or directories, simplifying the logic within `CopyDir` and `copyDirContents`. In `route/v1/file.go`, constants for HTTP headers related to file downloads have been defined to replace hard-coded strings, improving code readability and maintainability. This change ensures that the header keys and values are consistent and centrally managed, reducing the likelihood of errors and making the codebase easier to understand and modify.
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This commit enhances the initialization process and error handling across various components of the system. Changes include the addition of error checks during the initialization phase in main.go, ensuring that the system halts with an appropriate error message if any part of the setup (config, database, services) fails. Additionally, the logging mechanism has been improved to use structured logging for better clarity and debugging. The config initialization now returns an error if it fails, promoting better error handling practices. Similarly, database connection errors are now properly handled, and the system will return a specific error if the connection fails. These changes aim to make the system more robust by ensuring that startup errors are clearly reported and logged, facilitating easier troubleshooting and enhancing overall system reliability.