示例#1
0
func (req *Request) RenderJSON(code int, obj interface{}) {
	req.Response.WriteHeader(code)

	if obj != nil {
		if err := json.NewEncoder(req.Response).Encode(obj); err != nil {
			req.Response.WriteHeader(http.StatusInternalServerError)
			logger.Errorf("Could not write JSON response: %s", err.Error())
		}
	}
}
示例#2
0
func (req *Request) Error(e error) {
	if req == nil {
		return
	}

	err, casted := e.(*apierror.ApiError)
	if !casted {
		err = apierror.NewServerError(e.Error()).(*apierror.ApiError)
	}

	switch err.Code() {
	case http.StatusInternalServerError:
		logger.Errorf("%s - %s", err.Error(), req)
		http.Error(req.Response, `{"error":"Something went wrong"}`, http.StatusInternalServerError)
	default:
		if app.GetContext().Params.Debug {
			logger.Errorf("%s - %s", err.Error(), req)
		}
		http.Error(req.Response, fmt.Sprintf(`{"error":"%s"}`, err.Error()), err.Code())
	}
}
示例#3
0
func (req *Request) String() string {
	if req == nil {
		return ""
	}

	dump, err := json.Marshal(req)
	if err != nil {
		logger.Errorf(err.Error())
		return "failed to parse the request"
	}

	return string(dump)
}