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

fix json marshal

parent 26f9864a
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
......@@ -984,7 +985,7 @@ func main() {
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))
......@@ -1277,10 +1278,10 @@ func syncModeF(c *fiber.Ctx, taskid string) error {
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.
// 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
// 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{
Task: ResponseTask{
......@@ -1349,22 +1350,35 @@ func Return(c *fiber.Ctx, resAsPb pbUpstream.TaskResponse) error {
TaskFee: resAsPb.TaskFee,
IsSuccess: resAsPb.TaskIsSucceed,
TaskError: resAsPb.TaskError,
ExecCode: fmt.Sprintf("%d",resAsPb.TaskResultCode), //resAsPb.TaskResultCode,
ExecCode: fmt.Sprintf("%d", resAsPb.TaskResultCode), //resAsPb.TaskResultCode,
//TaskExecuteError
ExecError: resAsPb.TaskExecuteError,
},
Output: resAsPb.GetTaskResultBody(),
}
// resAsJson := ResponseJson{
// TaskId: resAsPb.TaskId,
// TaskResult: resAsPb.GetTaskResultBody(),
// TaskUid: resAsPb.TaskUid,
// TaskFee: resAsPb.TaskFee,
// IsSuccess: resAsPb.TaskIsSucceed,
// TaskError: resAsPb.TaskError,
// //TaskResultCode: resAsPb.TaskResultCode,
// }
resbuffer := &bytes.Buffer{}
encoder := json.NewEncoder(resbuffer)
encoder.SetEscapeHTML(false)
//fileAsJsonEscapeHTML,err := EncodeJsonEscapeHTML(fileAsJsonBytes)
if err := encoder.Encode(resAsJson); err != nil {
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)
......
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