Commit 8a18f5dc authored by Cloud User's avatar Cloud User

fix json marshal

parent 26f9864a
package main package main
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
...@@ -598,7 +599,7 @@ func batchToQuestDb(done chan interface{}, reqStream chan pbUpstream.TaskContent ...@@ -598,7 +599,7 @@ func batchToQuestDb(done chan interface{}, reqStream chan pbUpstream.TaskContent
type ResponseJson struct { type ResponseJson struct {
Task ResponseTask `json:"task"` Task ResponseTask `json:"task"`
Output json.RawMessage `json:"output"` Output json.RawMessage `json:"output"`
} }
type ResponseTask struct { type ResponseTask struct {
...@@ -984,7 +985,7 @@ func main() { ...@@ -984,7 +985,7 @@ func main() {
slog.LogAttrs(c.UserContext(), slog.LevelInfo, "getAsyncRes", newAttributes...) slog.LogAttrs(c.UserContext(), slog.LevelInfo, "getAsyncRes", newAttributes...)
return syncOrAsyncReturn(c, res, taskId) return syncOrAsyncReturn(c, res, taskId, baseAttributes)
} }
return c.SendString(fmt.Sprintf("can not find out the task id %s in result cache.", taskId)) return c.SendString(fmt.Sprintf("can not find out the task id %s in result cache.", taskId))
...@@ -1277,10 +1278,10 @@ func syncModeF(c *fiber.Ctx, taskid string) error { ...@@ -1277,10 +1278,10 @@ func syncModeF(c *fiber.Ctx, taskid string) error {
slog.LogAttrs(c.UserContext(), slog.LevelInfo, "syncModeF", newAttributes...) slog.LogAttrs(c.UserContext(), slog.LevelInfo, "syncModeF", newAttributes...)
return syncOrAsyncReturn(c, resAsPb, taskid) return syncOrAsyncReturn(c, resAsPb, taskid, baseAttributes)
} }
func syncOrAsyncReturn(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse, reqTaskId string) error { func syncOrAsyncReturn(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse, reqTaskId string, baseAttributes []slog.Attr) error {
// 303 (See Other) responses always lead to the use of a GET method. // 303 (See Other) responses always lead to the use of a GET method.
// 307 (Temporary Redirect) and 308 (Permanent Redirect) don't change the method used in the original request. // 307 (Temporary Redirect) and 308 (Permanent Redirect) don't change the method used in the original request.
...@@ -1336,11 +1337,11 @@ func syncOrAsyncReturn(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse, reqTaskId ...@@ -1336,11 +1337,11 @@ func syncOrAsyncReturn(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse, reqTaskId
// return redirectReturn(c, resAsPb) // return redirectReturn(c, resAsPb)
// } // }
return Return(c, resAsPb) return Return(c, resAsPb, baseAttributes)
} }
func Return(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse) error { func Return(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse, baseAttributes []slog.Attr) error {
resAsJson := ResponseJson{ resAsJson := ResponseJson{
Task: ResponseTask{ Task: ResponseTask{
...@@ -1349,22 +1350,35 @@ func Return(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse) error { ...@@ -1349,22 +1350,35 @@ func Return(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse) error {
TaskFee: resAsPb.TaskFee, TaskFee: resAsPb.TaskFee,
IsSuccess: resAsPb.TaskIsSucceed, IsSuccess: resAsPb.TaskIsSucceed,
TaskError: resAsPb.TaskError, TaskError: resAsPb.TaskError,
ExecCode: fmt.Sprintf("%d",resAsPb.TaskResultCode), //resAsPb.TaskResultCode, ExecCode: fmt.Sprintf("%d", resAsPb.TaskResultCode), //resAsPb.TaskResultCode,
//TaskExecuteError //TaskExecuteError
ExecError: resAsPb.TaskExecuteError, ExecError: resAsPb.TaskExecuteError,
}, },
Output: resAsPb.GetTaskResultBody(), Output: resAsPb.GetTaskResultBody(),
} }
// resAsJson := ResponseJson{ resbuffer := &bytes.Buffer{}
// TaskId: resAsPb.TaskId, encoder := json.NewEncoder(resbuffer)
// TaskResult: resAsPb.GetTaskResultBody(), encoder.SetEscapeHTML(false)
// TaskUid: resAsPb.TaskUid,
// TaskFee: resAsPb.TaskFee, //fileAsJsonEscapeHTML,err := EncodeJsonEscapeHTML(fileAsJsonBytes)
// IsSuccess: resAsPb.TaskIsSucceed,
// TaskError: resAsPb.TaskError, if err := encoder.Encode(resAsJson); err != nil {
// //TaskResultCode: resAsPb.TaskResultCode, newAttributes := append(baseAttributes, slog.String("err", err.Error()))
// } slog.LogAttrs(c.UserContext(), slog.LevelError, "encoder.Encode(resAsJson)", newAttributes...)
}
newAttributes := append(baseAttributes,
slog.String("TaskId", resAsJson.Task.TaskId),
slog.String("TaskUid", resAsJson.Task.TaskUid),
slog.String("TaskFee", resAsJson.Task.TaskFee),
slog.Bool("IsSuccess", resAsJson.Task.IsSuccess),
slog.String("TaskError", resAsJson.Task.TaskError),
slog.String("ExecCode", resAsJson.Task.ExecCode),
slog.String("ExecError", resAsJson.Task.ExecError),
)
slog.LogAttrs(c.UserContext(), slog.LevelError, "http return", newAttributes...)
return c.JSON(resAsJson) return c.JSON(resAsJson)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment