mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-06-27 22:36:00 +00:00
♻️ Refactor error codes for consistency in kernel plugin, broadcast and utility functions (#17770)
* Refactor error codes in broadcast, network, plugin, and utility functions for consistency * Fix API endpoint paths in multiple functions for consistency
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user