error.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package agi
  2. import (
  3. "html/template"
  4. "net/http"
  5. "os"
  6. "strconv"
  7. "time"
  8. )
  9. /*
  10. Error Template Rendering for AGI script error
  11. This script is used to handle a PHP-like error message for the user
  12. For any runtime error, please see the console for more information.
  13. */
  14. func (g *Gateway) RenderErrorTemplate(w http.ResponseWriter, errmsg string, scriptpath string) {
  15. templateFile, err := os.ReadFile("system/agi/error.html")
  16. if err != nil {
  17. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  18. return
  19. }
  20. // Define a template
  21. tmpl, err := template.New("errorTemplate").Parse(string(templateFile))
  22. if err != nil {
  23. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  24. return
  25. }
  26. // Define data to be used in the template
  27. data := map[string]interface{}{
  28. "error_msg": errmsg,
  29. "script_filepath": scriptpath,
  30. "timestamp": strconv.Itoa(int(time.Now().Unix())),
  31. "major_version": g.Option.BuildVersion,
  32. "minor_version": g.Option.InternalVersion,
  33. "agi_version": AgiVersion,
  34. }
  35. // Execute the template
  36. err = tmpl.Execute(w, data)
  37. if err != nil {
  38. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  39. return
  40. }
  41. // Set the HTTP status code
  42. w.WriteHeader(http.StatusInternalServerError)
  43. }