This commit is contained in:
2025-05-24 09:24:19 +04:00
parent e60b9f62f1
commit 8bc1759dcb
14 changed files with 1380 additions and 1 deletions

View File

@@ -1,8 +1,12 @@
package ui
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"git.ourworld.tf/herocode/heroagent/pkg/servers/ui/routes" // Import the routes package
"github.com/gofiber/fiber/v2"
@@ -54,9 +58,86 @@ func NewApp(config AppConfig) *fiber.App {
// Enable template reloading for development
engine.Reload(true)
// Create a new Fiber app with the configured Jet engine
// No custom functions for now
// Create a new Fiber app with the configured Jet engine and enhanced error handling
app := fiber.New(fiber.Config{
Views: engine,
ErrorHandler: func(c *fiber.Ctx, err error) error {
// Log the detailed error
log.Printf("ERROR: %v", err)
// Check if it's a template rendering error
if err.Error() != "" && (c.Route().Path != "" && c.Method() == "GET") {
// Extract template name and line number from error message
errorMsg := err.Error()
templateInfo := "Unknown template"
lineInfo := "Unknown line"
variableInfo := "Unknown variable"
// Try to extract template name and line number
if strings.Contains(errorMsg, "Jet Runtime Error") {
// Extract template and line number
templateLineRegex := regexp.MustCompile(`"([^"]+)":(\d+)`)
templateMatches := templateLineRegex.FindStringSubmatch(errorMsg)
if len(templateMatches) >= 3 {
templateInfo = templateMatches[1]
lineInfo = templateMatches[2]
}
// Extract variable name
varRegex := regexp.MustCompile(`there is no field or method '([^']+)'`)
varMatches := varRegex.FindStringSubmatch(errorMsg)
if len(varMatches) >= 2 {
variableInfo = varMatches[1]
}
// Log more detailed information
log.Printf("Template Error Details - Template: %s, Line: %s, Variable: %s",
templateInfo, lineInfo, variableInfo)
}
// Create a more detailed error page
errorHTML := fmt.Sprintf(`
<html>
<head>
<title>Template Error</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.error-box { background-color: #f8d7da; border: 1px solid #f5c6cb; padding: 15px; border-radius: 5px; }
.error-details { background-color: #f8f9fa; border: 1px solid #dee2e6; padding: 15px; border-radius: 5px; margin-top: 20px; }
.code-context { background-color: #f0f0f0; padding: 10px; border-radius: 5px; font-family: monospace; }
pre { white-space: pre-wrap; }
.highlight { background-color: #ffeb3b; font-weight: bold; }
</style>
</head>
<body>
<h1>Template Error</h1>
<div class="error-box">
<h3>Error Details:</h3>
<p><strong>Template:</strong> %s</p>
<p><strong>Line:</strong> %s</p>
<p><strong>Missing Variable:</strong> <span class="highlight">%s</span></p>
<pre>%s</pre>
</div>
<div class="error-details">
<h3>Debugging Tips:</h3>
<p>1. Check if the variable <span class="highlight">%s</span> is passed to the template</p>
<p>2. Visit <a href="/debug">/debug</a> to see available template variables</p>
<p>3. Check for typos in variable names</p>
<p>4. Ensure the variable is of the expected type</p>
<p>5. Check the controller that renders this template to ensure all required data is provided</p>
</div>
</body>
</html>
`, templateInfo, lineInfo, variableInfo, errorMsg, variableInfo)
return c.Status(fiber.StatusInternalServerError).Type("html").SendString(errorHTML)
}
// For other errors, use the default error handler
return fiber.DefaultErrorHandler(c, err)
},
})
// Setup static file serving