Skip to content

Conversation

cnlangzi
Copy link
Member

@cnlangzi cnlangzi commented Apr 18, 2025

Changed

Fixed

  • fix(viewengine): continue to load other files even a file is failed to load

Added

Tests

Tasks to complete before merging PR:

  • Ensure unit tests are passing. If not run make unit-tests to check for any regressions 📋
  • Ensure lint tests are passing. if not run make lint to check for any issues
  • Ensure codecov/patch is passing for changes.

Summary by Sourcery

Modify view engine loading to continue processing files even when individual file loading fails, improving error handling and resilience

Bug Fixes:

  • Prevent view engine from stopping template loading when a single file fails to load

Enhancements:

  • Modify view engine methods to log errors instead of stopping the entire loading process
  • Update error handling to continue processing other files when one file fails

Copy link

sourcery-ai bot commented Apr 18, 2025

Reviewer's Guide by Sourcery

The pull request modifies the view engine loading process to continue loading files even if one file fails to load. This is achieved by changing the Load methods in the view engines to not return errors and instead log them. The individual load methods for components, layouts, pages, and views in the HTML view engine were also modified to log errors instead of returning them.

Sequence diagram for loading HTML templates with error handling

sequenceDiagram
  participant HtmlViewEngine
  participant fsys as FileSystem
  participant app as App

  HtmlViewEngine->>fsys: WalkDir("components", ...)
  loop For each component file
    fsys->>HtmlViewEngine: Returns path to file
    HtmlViewEngine->>HtmlViewEngine: loadTemplate(path)
    alt Error loading template
      HtmlViewEngine->>app: app.logger.Error("xun: load html", path, err)
    end
  end

  HtmlViewEngine->>fsys: WalkDir("layouts", ...)
  loop For each layout file
    fsys->>HtmlViewEngine: Returns path to file
    HtmlViewEngine->>HtmlViewEngine: loadTemplate(path)
    alt Error loading template
      HtmlViewEngine->>app: app.logger.Error("xun: load html", path, err)
    end
  end

  HtmlViewEngine->>fsys: WalkDir("pages", ...)
  loop For each page file
    fsys->>HtmlViewEngine: Returns path to file
    HtmlViewEngine->>HtmlViewEngine: loadPage(path)
    alt Error loading page
      HtmlViewEngine->>app: app.logger.Error("xun: load html", path, err)
    end
  end

  HtmlViewEngine->>fsys: WalkDir("views", ...)
  loop For each view file
    fsys->>HtmlViewEngine: Returns path to file
    HtmlViewEngine->>HtmlViewEngine: loadView(path)
    alt Error loading view
      HtmlViewEngine->>app: app.logger.Error("xun: load html", path, err)
    end
  end
Loading

Updated class diagram for TextViewEngine

classDiagram
  class TextViewEngine {
    -templates map[string]*TextTemplate
    -fsys fs.FS
    -app *App
    +Load(fsys fs.FS, app *App)
    -loadText(path string) error
  }
  note for TextViewEngine "Load method no longer returns an error. Errors are now logged internally."
Loading

Updated class diagram for StaticViewEngine

classDiagram
  class StaticViewEngine {
    +Load(fsys fs.FS, app *App)
    +FileChanged(fsys fs.FS, app *App, event fsnotify.Event) error
    -handle(fsys fs.FS, app *App, path string)
  }
  note for StaticViewEngine "Load method no longer returns an error."
Loading

File-Level Changes

Change Details Files
Modified view engine loading to continue processing files even when individual file loading fails, improving error handling and resilience.
  • Updated HtmlViewEngine.Load to not return an error.
  • Updated TextViewEngine.Load to not return an error.
  • Updated StaticViewEngine.Load to not return an error.
  • Updated mockViewEngine.Load to not return an error.
  • Removed error returns from the loadComponents, loadLayouts, loadPages, and loadViews methods in HtmlViewEngine.
  • Modified the loadComponents, loadLayouts, loadPages, loadViews and loadText methods to log errors instead of returning them.
  • Updated the main application loop to not return an error when loading views.
viewengine_html.go
viewengine_text.go
app_test.go
viewengine_static.go
app.go
app_watch_test.go
viewengine.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

deepsource-io bot commented Apr 18, 2025

Here's the code health analysis summary for commits 006d1eb..3029703. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Go LogoGo✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cnlangzi - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The Load methods no longer return errors, so the error variable in the walk functions is now unused and can be dropped.
  • Consider adding a recover statement to the goroutines to prevent panics from crashing the application.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@cnlangzi cnlangzi changed the title fix(viewengine): continue to load other files even a file is failed to load fix(viewengine): continue to load other files when one file fails Apr 18, 2025
Copy link

codecov bot commented Apr 18, 2025

Codecov Report

Attention: Patch coverage is 82.35294% with 6 lines in your changes missing coverage. Please review.

Project coverage is 91.28%. Comparing base (006d1eb) to head (3029703).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
viewengine_html.go 86.95% 2 Missing and 1 partial ⚠️
viewengine_text.go 57.14% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #69      +/-   ##
==========================================
+ Coverage   91.23%   91.28%   +0.04%     
==========================================
  Files          62       62              
  Lines        2545     2490      -55     
==========================================
- Hits         2322     2273      -49     
+ Misses        192      188       -4     
+ Partials       31       29       -2     
Flag Coverage Δ
Unit-Tests 91.28% <82.35%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cnlangzi cnlangzi requested a review from Copilot April 18, 2025 15:41
@cnlangzi
Copy link
Member Author

@sourcery-ai review

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modifies the view engine loading mechanism to ensure that a single file loading failure does not prevent the rest of the files from being processed. Key changes include:

  • Removing the error propagation from the Load functions in TextViewEngine, StaticViewEngine, and HtmlViewEngine.
  • Logging errors for individual file failures using slog instead of stopping the entire loading process.
  • Adjusting the ViewEngine interface and corresponding mock implementations in tests to reflect the new Load signature.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
viewengine_text.go Removed error returns from Load; now logs errors per file load.
viewengine_static.go Updated Load to remove error propagation and log errors.
viewengine_html.go Changed Load and its helper functions to log errors instead of returning them.
viewengine.go Updated the ViewEngine interface to use a void Load method.
app_watch_test.go Adjusted mock implementations to reflect updated Load signature.
app.go Updated engines loading to call the new void Load method.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cnlangzi - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The nolint comments should have a reason why the error is being ignored.
  • Consider adding a method to the ViewEngine interface to return the name of the engine for logging purposes.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@cnlangzi
Copy link
Member Author

@sourcery-ai review

@cnlangzi cnlangzi requested a review from Copilot April 18, 2025 15:48
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the resilience of the view engine by modifying the Load methods to log errors instead of stopping file processing when an individual file fails to load. Key changes include:

  • Removing error returns from Load methods in TextViewEngine, StaticViewEngine, and HtmlViewEngine.
  • Logging errors for file-load failures within WalkDir callbacks.
  • Updating test and interface definitions to align with the new error-handling design.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
viewengine_text.go Updated Load method to log errors instead of returning them.
viewengine_static.go Modified Load to ignore errors on file loading in the public folder.
viewengine_html.go Adjusted Load and its helper methods to internalize and log errors.
viewengine.go Updated the ViewEngine interface to remove the error return from Load.
app_watch_test.go Updated mock to reflect new Load signature.
app_test.go Tweaked HTTP transport DialContext to target a local address.
app.go Removed error handling from Load call for each view engine.
Comments suppressed due to low confidence (2)

viewengine_html.go:113

  • Ensure there are unit tests verifying that errors during layout loading are correctly logged since the function no longer returns an error.
fs.WalkDir(ve.fsys, "layouts", func(path string, d fs.DirEntry, _ error) error { // nolint: errcheck

viewengine.go:13

  • Add tests to verify that any file load errors are properly handled and logged by each view engine, given that the Load method no longer returns an error.
Load(fsys fs.FS, app *App)

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cnlangzi - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a comment to the fs.WalkDir anonymous functions to explain why the error is being ignored.
  • The FileChanged method on mockViewEngine should probably not return an unhandled error.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -1,8 +1,8 @@
package xun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider creating a common helper function to encapsulate the FS walk logic and error logging, reducing code duplication across the load functions for components, layouts, pages, and views, and improving maintainability and readability..

You’re repeating four nearly identical FS walks with custom callbacks. Consolidating them into a common helper would reduce duplication and overall complexity. For example, you can create a helper method to encapsulate the walking logic and error logging, then pass a function to process matching files. For instance:

func (ve *HtmlViewEngine) walkAndProcess(dir string, process func(path string) error) {
    fs.WalkDir(ve.fsys, dir, func(path string, d fs.DirEntry, _ error) error { // nolint: errcheck
        if !strings.EqualFold(filepath.Ext(path), ".html") {
            return nil
        }
        if err := process(path); err != nil {
            ve.app.logger.Error("xun: load html", slog.String("path", path), slog.Any("err", err))
        }
        return nil
    })
}

Then update your functions like loadComponents, loadLayouts, loadViews, and even loadPages (with slight adjustments if needed) to use this helper:

func (ve *HtmlViewEngine) loadComponents() {
    ve.walkAndProcess("components", func(path string) error {
        _, err := ve.loadTemplate(path)
        return err
    })
}

func (ve *HtmlViewEngine) loadLayouts() {
    ve.walkAndProcess("layouts", func(path string) error {
        _, err := ve.loadTemplate(path)
        return err
    })
}

func (ve *HtmlViewEngine) loadViews() {
    ve.walkAndProcess("views", func(path string) error {
        return ve.loadView(path)
    })
}

// Adjust loadPages accordingly if needed:
func (ve *HtmlViewEngine) loadPages() {
    ve.walkAndProcess("pages", func(path string) error {
        return ve.loadPage(path)
    })
}

This refactoring removes repetitive error handling and inline logging while keeping all functionality intact.

cnlangzi and others added 3 commits April 18, 2025 23:49
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@cnlangzi cnlangzi merged commit 847eef3 into main Apr 19, 2025
6 of 7 checks passed
@cnlangzi cnlangzi deleted the fix/ve_load branch April 19, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant