Merge pull request #16424 from ioito/hotfix/qx-request-time-rfc3339

fix(common): show request time with rfc3339
This commit is contained in:
Zexi Li
2023-04-11 21:14:58 +08:00
committed by GitHub
2 changed files with 12 additions and 6 deletions
+9 -6
View File
@@ -36,6 +36,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
@@ -52,10 +56,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 {
@@ -100,9 +103,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 {
+3
View File
@@ -21,6 +21,7 @@ import (
"net/url"
"regexp"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -278,11 +279,13 @@ func (this *BaseManager) _submit(session *mcclient.ClientSession, method httputi
func SubmitResults2JSON(results []printutils.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()