Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:recycle: Refactor error codes for consistency in kernel plugin, broadcast and utility functions (#17770)

Yingyi / 颖逸ff1b0bc65835
diff --git a/app/src/layout/dock/util.ts b/app/src/layout/dock/util.ts
index 78b2a59e00..5cfc2ac27f 100644
--- a/app/src/layout/dock/util.ts
+++ b/app/src/layout/dock/util.ts
@@ -36,7 +36,7 @@ export const openBacklink = async (options: {
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
if (!options.rootId) {
- const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
+ const response = await fetchSyncPost("/api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
@@ -44,7 +44,7 @@ export const openBacklink = async (options: {
options.useBlockId = response.data.rootID !== response.data.id;
options.title = response.data.name || window.siyuan.languages.untitled;
} else if (!options.title) {
- const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
+ const response = await fetchSyncPost("/api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
@@ -92,7 +92,7 @@ export const openGraph = async (options: {
wnd = getWndByLayout(window.siyuan.layout.centerLayout);
}
if (!options.rootId) {
- const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
+ const response = await fetchSyncPost("/api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
@@ -100,7 +100,7 @@ export const openGraph = async (options: {
options.useBlockId = response.data.rootID !== response.data.id;
options.title = response.data.name || window.siyuan.languages.untitled;
} else if (!options.title) {
- const response = await fetchSyncPost("api/block/getDocInfo", {id: options.blockId});
+ const response = await fetchSyncPost("/api/block/getDocInfo", {id: options.blockId});
if (response.code === -1) {
return;
}
@@ -148,7 +148,7 @@ export const openOutline = async (options: {
const newWnd = wnd.split("lr", false);
if (!options.title) {
- const response = await fetchSyncPost("api/block/getDocInfo", {id: options.rootId});
+ const response = await fetchSyncPost("/api/block/getDocInfo", {id: options.rootId});
options.title = response.data.name || window.siyuan.languages.untitled;
}
newWnd.element.style.width = "200px";
diff --git a/app/src/plugin/API.ts b/app/src/plugin/API.ts
index e268d543b0..fe7593c797 100644
--- a/app/src/plugin/API.ts
+++ b/app/src/plugin/API.ts
@@ -299,7 +299,7 @@ export const expandDocTree = async (options: {
if (isNotebook) {
liElement = file.element.querySelector(`.b3-list[data-url="${options.id}"]`)?.firstElementChild as HTMLElement;
} else {
- const response = await fetchSyncPost("api/block/getBlockInfo", {id: options.id});
+ const response = await fetchSyncPost("/api/block/getBlockInfo", {id: options.id});
if (response.code === -1) {
return;
}
diff --git a/app/src/plugin/kernel.ts b/app/src/plugin/kernel.ts
index fe01be6721..c6c2cc2cfd 100644
--- a/app/src/plugin/kernel.ts
+++ b/app/src/plugin/kernel.ts
@@ -156,7 +156,7 @@ export class Kernel implements IKernelPlugin {
}
async #initState() {
- const response = await fetchSyncPost("api/plugin/getLoadedPlugin", { name: this.#name });
+ const response = await fetchSyncPost("/api/plugin/getLoadedPlugin", { name: this.#name });
if (this.state.code === -1 && response.data?.stateCode != null) {
this.state.code = response.data.stateCode;
}
diff --git a/app/src/protyle/render/av/calc.ts b/app/src/protyle/render/av/calc.ts
index 06d5c82333..60b5d02432 100644
--- a/app/src/protyle/render/av/calc.ts
+++ b/app/src/protyle/render/av/calc.ts
@@ -287,7 +287,7 @@ export const openCalcMenu = async (protyle: IProtyle, calcElement: HTMLElement,
let keyID: string;
let avData = panelData?.data;
if (!avData) {
- const avResponse = await fetchSyncPost("api/av/renderAttributeView", {id: avId});
+ const avResponse = await fetchSyncPost("/api/av/renderAttributeView", {id: avId});
avData = avResponse.data;
}
@@ -307,7 +307,7 @@ export const openCalcMenu = async (protyle: IProtyle, calcElement: HTMLElement,
}
});
if (relationAvId) {
- const colResponse = await fetchSyncPost("api/av/getAttributeView", {id: relationAvId});
+ const colResponse = await fetchSyncPost("/api/av/getAttributeView", {id: relationAvId});
colResponse.data.av.keyValues.find((item: { key: { id: string, name: string, type: TAVCol } }) => {
if (item.key.id === keyID) {
rollupIsNumber = item.key.type === "number";
diff --git a/kernel/api/broadcast.go b/kernel/api/broadcast.go
index 9ccd49b568..490d79ebb9 100644
--- a/kernel/api/broadcast.go
+++ b/kernel/api/broadcast.go
@@ -582,7 +582,7 @@ func broadcastPublish(c *gin.Context) {
// Multipart form
form, err := c.MultipartForm()
if err != nil {
- ret.Code = -2
+ ret.Code = 1
ret.Msg = err.Error()
return
}
@@ -620,7 +620,7 @@ func broadcastPublish(c *gin.Context) {
_, err := broadcastChannel.BroadcastString(value)
if err != nil {
logging.LogErrorf("broadcast message failed: %s", err)
- result.Code = -2
+ result.Code = 2
result.Msg = err.Error()
continue
}
@@ -661,7 +661,7 @@ func broadcastPublish(c *gin.Context) {
value, err := file.Open()
if err != nil {
logging.LogErrorf("open multipart form file [%s] failed: %s", file.Filename, err)
- result.Code = -4
+ result.Code = 3
result.Msg = err.Error()
continue
}
@@ -669,14 +669,14 @@ func broadcastPublish(c *gin.Context) {
content := make([]byte, file.Size)
if _, err := value.Read(content); err != nil {
logging.LogErrorf("read multipart form file [%s] failed: %s", file.Filename, err)
- result.Code = -3
+ result.Code = 4
result.Msg = err.Error()
continue
}
if _, err := broadcastChannel.BroadcastBinary(content); err != nil {
logging.LogErrorf("broadcast binary message failed: %s", err)
- result.Code = -2
+ result.Code = 5
result.Msg = err.Error()
continue
}
@@ -740,7 +740,7 @@ func postMessage(c *gin.Context) {
if _, err := broadcastChannel.BroadcastString(message); err != nil {
logging.LogErrorf("broadcast message failed: %s", err)
- ret.Code = -2
+ ret.Code = 1
ret.Msg = err.Error()
return
}
diff --git a/kernel/api/network.go b/kernel/api/network.go
index 8af8ba6258..2e3a00a98f 100644
--- a/kernel/api/network.go
+++ b/kernel/api/network.go
@@ -164,13 +164,13 @@ func forwardProxy(c *gin.Context) {
}
u, e := url.ParseRequestURI(destURL)
if nil != e {
- ret.Code = -1
+ ret.Code = 1
ret.Msg = "invalid [url]"
return
}
if u.Scheme != "http" && u.Scheme != "https" {
- ret.Code = -1
+ ret.Code = 2
ret.Msg = "only http/https is allowed"
return
}
@@ -215,7 +215,7 @@ func forwardProxy(c *gin.Context) {
fallthrough
case "base64-std":
if payload, err := base64.StdEncoding.DecodeString(arg["payload"].(string)); err != nil {
- ret.Code = -2
+ ret.Code = 3
ret.Msg = "decode base64-std payload failed: " + err.Error()
return
} else {
@@ -223,7 +223,7 @@ func forwardProxy(c *gin.Context) {
}
case "base64-url":
if payload, err := base64.URLEncoding.DecodeString(arg["payload"].(string)); err != nil {
- ret.Code = -2
+ ret.Code = 4
ret.Msg = "decode base64-url payload failed: " + err.Error()
return
} else {
@@ -233,7 +233,7 @@ func forwardProxy(c *gin.Context) {
fallthrough
case "base32-std":
if payload, err := base32.StdEncoding.DecodeString(arg["payload"].(string)); err != nil {
- ret.Code = -2
+ ret.Code = 5
ret.Msg = "decode base32-std payload failed: " + err.Error()
return
} else {
@@ -241,7 +241,7 @@ func forwardProxy(c *gin.Context) {
}
case "base32-hex":
if payload, err := base32.HexEncoding.DecodeString(arg["payload"].(string)); err != nil {
- ret.Code = -2
+ ret.Code = 6
ret.Msg = "decode base32-hex payload failed: " + err.Error()
return
} else {
@@ -249,7 +249,7 @@ func forwardProxy(c *gin.Context) {
}
case "hex":
if payload, err := hex.DecodeString(arg["payload"].(string)); err != nil {
- ret.Code = -2
+ ret.Code = 7
ret.Msg = "decode hex payload failed: " + err.Error()
return
} else {
@@ -263,14 +263,14 @@ func forwardProxy(c *gin.Context) {
started := time.Now()
resp, err := request.Send(method, destURL)
if err != nil {
- ret.Code = -1
+ ret.Code = 8
ret.Msg = "forward request failed: " + err.Error()
return
}
bodyData, err := io.ReadAll(resp.Body)
if err != nil {
- ret.Code = -1
+ ret.Code = 9
ret.Msg = "read response body failed: " + err.Error()
return
}
diff --git a/kernel/api/plugin.go b/kernel/api/plugin.go
index 6c7385fc77..a8bcb6c00d 100644
--- a/kernel/api/plugin.go
+++ b/kernel/api/plugin.go
@@ -37,7 +37,7 @@ func getPluginName(c *gin.Context, ret *gulu.Result) (name string) {
name = util.GetRequestStringParam(c, "name", ret)
if name == "" {
if ret.Code == 0 {
- ret.Code = -10
+ ret.Code = 3
ret.Msg = "Plugin name is required"
}
}
@@ -55,7 +55,7 @@ func getLoadedPlugin(c *gin.Context) {
pluginInfo, found := plugin.GetManager().GetLoadedPlugin(pluginName)
if !found {
- ret.Code = -11
+ ret.Code = 4
ret.Msg = fmt.Sprintf("Plugin [%s] not loaded", pluginName)
return
}
diff --git a/kernel/util/net.go b/kernel/util/net.go
index 6c924b3f53..43e914b2a2 100644
--- a/kernel/util/net.go
+++ b/kernel/util/net.go
@@ -248,14 +248,14 @@ func GetRequestStringParam(c *gin.Context, key string, result *gulu.Result) stri
return ""
}
if arg[key] == nil {
- result.Code = -2
+ result.Code = 1
result.Msg = fmt.Sprintf("Request body prop [%s] does not exist", key)
return ""
}
value, ok := arg[key].(string)
if !ok {
- result.Code = -3
+ result.Code = 2
result.Msg = fmt.Sprintf("Request body prop [%s] is not a string", key)
return ""
}