コード例 #1
0
ファイル: data.go プロジェクト: agoravoting/agora-http-go
func (e *Event) Marshal() ([]byte, error) {
	j, err := e.Json()
	if err != nil {
		return []byte(""), err
	}
	return util.JsonSortedMarshal(j)
}
コード例 #2
0
ファイル: data.go プロジェクト: agoravoting/agora-http-go
func (e *Event) Json() (ret map[string]interface{}, err error) {
	config, err := util.JsonSortedMarshal(e.AuthMethodConfig)
	if err != nil {
		return
	}

	ret = map[string]interface{}{
		"name":               e.Name,
		"auth_method":        e.AuthMethod,
		"auth_method_config": config,
	}
	return
}
コード例 #3
0
// ErrorWrap handles errors nicely and returns an standard httprouter.Handle.
func (ew *ErrorWrap) Do(handle ErrorHandler) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
		if err := handle(w, r, p); err != nil {
			// record strange or internal errors
			if err.Code >= http.StatusRequestTimeout && !util.IsNil(ew.Raven) {
				msg := fmt.Sprintf("Error: code=%d, message='%s', err=%v, stack=%v", err.Code, err.Message, err.Err, debug.Stack())
				ew.Raven.CaptureMessage(msg)
			}
			ew.Logger.Printf("Error: code=%d, message='%s', err=%v", err.Code, err.Message, err.Err)

			content, err2 := util.JsonSortedMarshal(&handledErrorJson{err.Message, err.CodedMessage})
			if err2 != nil {
				panic(err2)
			}
			http.Error(w, string(content), err.Code)
		}
	}
}
コード例 #4
0
func (rec *RecoveryJson) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	defer func() {
		if err := recover(); err != nil {
			w.Header().Set("Content-Type", JSON_MIMETYPE)
			w.WriteHeader(http.StatusInternalServerError)

			msg := fmt.Sprintf(PANIC_LOG_FMT, err, debug.Stack())
			rec.Logger.Print(msg)
			if rec.Raven != nil {
				rec.Raven.CaptureMessage(msg)
			}
			content, err := util.JsonSortedMarshal(newErrorJson(err))
			if err != nil {
				panic(err)
			}
			w.Write(content)
		}
	}()
	next(w, r)
}