fix(common): show request time with rfc3339

This commit is contained in:
ioito
2023-04-11 20:54:43 +08:00
parent 47d9c3c053
commit da6e696b80
2 changed files with 13 additions and 6 deletions

View File

@@ -35,6 +35,10 @@ func init() {
timeZone = time.Local
}
func GetTimeZone() *time.Location {
return timeZone
}
func SetTimeZone(tzStr string) {
if tz, _ := time.LoadLocation(tzStr); tz != nil {
timeZone = tz
@@ -51,10 +55,9 @@ func SetHTTPRedirectLocationHeader(w http.ResponseWriter, location string) {
}
type Error struct {
Code int `json:"code,omitzero"`
Class string `json:"class,omitempty"`
Details string `json:"details,omitempty"`
Time time.Time `json:"time,omitempty"`
Code int `json:"code,omitzero"`
Class string `json:"class,omitempty"`
Details string `json:"details,omitempty"`
}
func NewErrorFromJCError(ctx context.Context, je *httputils.JSONClientError) Error {
@@ -99,9 +102,9 @@ func HTTPError(ctx context.Context, w http.ResponseWriter, msg string, statusCod
Code: statusCode,
Class: class,
Details: details,
Time: time.Now().In(timeZone),
}
body := jsonutils.Marshal(err)
body := jsonutils.Marshal(err).(*jsonutils.JSONDict)
body.Set("time", jsonutils.NewString(time.Now().In(timeZone).Format(time.RFC3339)))
w.Write([]byte(body.String()))
log.Errorf("Send error %s", details)
if statusCode >= 500 {

View File

@@ -20,9 +20,11 @@ import (
"net/http"
"net/url"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/httputils"
)
@@ -243,11 +245,13 @@ type SubmitResult struct {
func SubmitResults2JSON(results []SubmitResult) jsonutils.JSONObject {
arr := jsonutils.NewArray()
now := time.Now().In(httperrors.GetTimeZone())
for _, r := range results {
obj := jsonutils.NewDict()
obj.Add(jsonutils.NewInt(int64(r.Status)), "status")
obj.Add(jsonutils.Marshal(r.Id), "id")
obj.Add(r.Data, "data")
obj.Add(jsonutils.NewString(now.Format(time.RFC3339)), "data", "time")
arr.Add(obj)
}
body := jsonutils.NewDict()