API连接错误提示更加详细,以便于快速发现问题

This commit is contained in:
GoEdgeLab
2022-06-08 15:17:00 +08:00
parent 441dd3b84a
commit 2ffe7a3fa5
6 changed files with 86 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
package actionutils package actionutils
// 子菜单定义 // Menu 子菜单定义
type Menu struct { type Menu struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@@ -11,14 +11,14 @@ type Menu struct {
CountNormalItems int `json:"countNormalItems"` CountNormalItems int `json:"countNormalItems"`
} }
// 获取新对象 // NewMenu 获取新对象
func NewMenu() *Menu { func NewMenu() *Menu {
return &Menu{ return &Menu{
Items: []*MenuItem{}, Items: []*MenuItem{},
} }
} }
// 添加菜单项 // Add 添加菜单项
func (this *Menu) Add(name string, subName string, url string, isActive bool) *MenuItem { func (this *Menu) Add(name string, subName string, url string, isActive bool) *MenuItem {
item := &MenuItem{ item := &MenuItem{
Name: name, Name: name,
@@ -36,7 +36,7 @@ func (this *Menu) Add(name string, subName string, url string, isActive bool) *M
return item return item
} }
// 添加特殊菜单项,不计数 // AddSpecial 添加特殊菜单项,不计数
func (this *Menu) AddSpecial(name string, subName string, url string, isActive bool) *MenuItem { func (this *Menu) AddSpecial(name string, subName string, url string, isActive bool) *MenuItem {
item := this.Add(name, subName, url, isActive) item := this.Add(name, subName, url, isActive)
this.CountNormalItems-- this.CountNormalItems--

View File

@@ -5,20 +5,20 @@ import (
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
) )
// 菜单分组 // MenuGroup 菜单分组
type MenuGroup struct { type MenuGroup struct {
Menus []*Menu `json:"menus"` Menus []*Menu `json:"menus"`
AlwaysMenu *Menu `json:"alwaysMenu"` AlwaysMenu *Menu `json:"alwaysMenu"`
} }
// 获取新菜单分组对象 // NewMenuGroup 获取新菜单分组对象
func NewMenuGroup() *MenuGroup { func NewMenuGroup() *MenuGroup {
return &MenuGroup{ return &MenuGroup{
Menus: []*Menu{}, Menus: []*Menu{},
} }
} }
// 查找菜单,如果找不到则自动创建 // FindMenu 查找菜单,如果找不到则自动创建
func (this *MenuGroup) FindMenu(menuId string, menuName string) *Menu { func (this *MenuGroup) FindMenu(menuId string, menuName string) *Menu {
for _, m := range this.Menus { for _, m := range this.Menus {
if m.Id == menuId { if m.Id == menuId {
@@ -33,7 +33,7 @@ func (this *MenuGroup) FindMenu(menuId string, menuName string) *Menu {
return menu return menu
} }
// 排序 // Sort 排序
func (this *MenuGroup) Sort() { func (this *MenuGroup) Sort() {
lists.Sort(this.Menus, func(i int, j int) bool { lists.Sort(this.Menus, func(i int, j int) bool {
menu1 := this.Menus[i] menu1 := this.Menus[i]
@@ -42,7 +42,7 @@ func (this *MenuGroup) Sort() {
}) })
} }
// 设置子菜单 // SetSubMenu 设置子菜单
func SetSubMenu(action actions.ActionWrapper, menu *MenuGroup) { func SetSubMenu(action actions.ActionWrapper, menu *MenuGroup) {
action.Object().Data["teaSubMenus"] = menu action.Object().Data["teaSubMenus"] = menu
} }

View File

@@ -1,6 +1,6 @@
package actionutils package actionutils
// 菜单项 // MenuItem 菜单项
type MenuItem struct { type MenuItem struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`

View File

@@ -127,7 +127,7 @@ func (this *Page) AsHTML() string {
return `<div class="page">` + strings.Join(result, "") + `</div>` return `<div class="page">` + strings.Join(result, "") + `</div>`
} }
// 判断是否为最后一页 // IsLastPage 判断是否为最后一页
func (this *Page) IsLastPage() bool { func (this *Page) IsLastPage() bool {
return this.Current == this.Max return this.Current == this.Max
} }

View File

@@ -5,19 +5,19 @@ import (
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
) )
// Tabbar定义 // Tabbar Tabbar定义
type Tabbar struct { type Tabbar struct {
items []maps.Map items []maps.Map
} }
// 获取新对象 // NewTabbar 获取新对象
func NewTabbar() *Tabbar { func NewTabbar() *Tabbar {
return &Tabbar{ return &Tabbar{
items: []maps.Map{}, items: []maps.Map{},
} }
} }
// 添加菜单项 // Add 添加菜单项
func (this *Tabbar) Add(name string, subName string, url string, icon string, active bool) maps.Map { func (this *Tabbar) Add(name string, subName string, url string, icon string, active bool) maps.Map {
m := maps.Map{ m := maps.Map{
"name": name, "name": name,
@@ -31,12 +31,12 @@ func (this *Tabbar) Add(name string, subName string, url string, icon string, ac
return m return m
} }
// 取得所有的Items // Items 取得所有的Items
func (this *Tabbar) Items() []maps.Map { func (this *Tabbar) Items() []maps.Map {
return this.items return this.items
} }
// 设置子菜单 // SetTabbar 设置子菜单
func SetTabbar(action actions.ActionWrapper, tabbar *Tabbar) { func SetTabbar(action actions.ActionWrapper, tabbar *Tabbar) {
action.Object().Data["teaTabbar"] = tabbar.Items() action.Object().Data["teaTabbar"] = tabbar.Items()
} }

View File

@@ -1,11 +1,18 @@
package actionutils package actionutils
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/configs"
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const" teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
rpcerrors "github.com/TeaOSLab/EdgeCommon/pkg/rpc/errors" rpcerrors "github.com/TeaOSLab/EdgeCommon/pkg/rpc/errors"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/maps"
"io/ioutil"
"net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@@ -24,24 +31,78 @@ func Fail(action actions.ActionWrapper, err error) {
// FailPage 提示页面错误信息 // FailPage 提示页面错误信息
func FailPage(action actions.ActionWrapper, err error) { func FailPage(action actions.ActionWrapper, err error) {
if err != nil { if err == nil {
logs.Println("[" + reflect.TypeOf(action).String() + "]" + findStack(err.Error())) err = errors.New("unknown error")
} }
err = rpcerrors.HumanError(err)
logs.Println("[" + reflect.TypeOf(action).String() + "]" + findStack(err.Error()))
// 当前API终端地址
var apiEndpoints = []string{}
apiConfig, apiConfigErr := configs.LoadAPIConfig()
if apiConfigErr == nil && apiConfig != nil {
apiEndpoints = append(apiEndpoints, apiConfig.RPC.Endpoints...)
}
var isRPCConnError bool
err, isRPCConnError = rpcerrors.HumanError(err, apiEndpoints, Tea.ConfigFile("api.yaml"))
action.Object().ResponseWriter.WriteHeader(http.StatusInternalServerError) action.Object().ResponseWriter.WriteHeader(http.StatusInternalServerError)
if len(action.Object().Request.Header.Get("X-Requested-With")) > 0 { if len(action.Object().Request.Header.Get("X-Requested-With")) > 0 {
action.Object().WriteString(teaconst.ErrServer) action.Object().WriteString(teaconst.ErrServer)
} else { } else {
action.Object().WriteString(`<!DOCTYPE html> // 本地的一些错误提示
var isLocalAPI = false
if isRPCConnError {
host, _, hostErr := net.SplitHostPort(action.Object().Request.Host)
if hostErr == nil {
for _, endpoint := range apiEndpoints {
if strings.HasPrefix(endpoint, "http://"+host) || strings.HasPrefix(endpoint, "https://"+host) || strings.HasPrefix(endpoint, host) {
isLocalAPI = true
break
}
}
}
}
var issuesHTML = ""
if isLocalAPI {
// 读取本地API节点的issues
issuesData, issuesErr := ioutil.ReadFile(Tea.Root + "/edge-api/logs/issues.log")
if issuesErr == nil {
var issueMaps = []maps.Map{}
issuesErr = json.Unmarshal(issuesData, &issueMaps)
if issuesErr == nil && len(issueMaps) > 0 {
var issueMap = issueMaps[0]
issuesHTML = "本地API节点启动错误" + issueMap.GetString("message") + ",处理建议:" + issueMap.GetString("suggestion")
}
}
}
var html = `<!DOCTYPE html>
<html> <html>
<head></head> <head>
<title>有系统错误需要处理</title>
<meta charset="UTF-8"/>
<style type="text/css">
hr { border-top: 1px #ccc solid; }
.red { color: red; }
</style>
</head>
<body> <body>
<div style="background: #eee; border: 1px #ccc solid; padding: 10px; font-size: 12px; line-height: 1.8"> <div style="background: #eee; border: 1px #ccc solid; padding: 10px; font-size: 12px; line-height: 1.8">
` + teaconst.ErrServer + ` ` + teaconst.ErrServer + `
<div>可以通过查看 <strong><em>$安装目录/logs/run.log</em></strong> 日志文件查看具体的错误提示。</div> <div>可以通过查看 <strong><em>$安装目录/logs/run.log</em></strong> 日志文件查看具体的错误提示。</div>
<hr style="border-top: 1px #ccc solid"/> <hr/>
<div style="color: red">Error: ` + err.Error() + `</pre> <div class="red">Error: ` + err.Error() + `</div>`
</div>
if len(issuesHTML) > 0 {
html += ` <hr/>
<div class="red">` + issuesHTML + `</div>`
}
action.Object().WriteString(html + `
</div>
</body> </body>
</html>`) </html>`)
} }