示例#1
0
文件: api.go 项目: app-kit/go-appkit
func ServerErrorMiddleware(registry kit.Registry, r kit.Request, response kit.Response) (kit.Response, bool) {
	err := response.GetError()
	if err == nil {
		return nil, false
	}

	status := 500

	// If the error is an apperror, and it contains a status,
	// set it as the http status of the response.
	if apperr, ok := err.(apperror.Error); ok {
		if apperr.GetStatus() != 0 {
			status = apperr.GetStatus()
		}
	}

	response.SetHttpStatus(status)

	if response.GetRawData() != nil || response.GetRawDataReader() != nil {
		return nil, false
	}

	httpRequest := r.GetHttpRequest()
	apiPrefix := "/" + registry.Config().UString("api.prefix", "api")
	isApiRequest := strings.HasPrefix(httpRequest.URL.Path, apiPrefix)

	if isApiRequest {
		return nil, false
	}

	data := map[string]interface{}{"errors": []error{response.GetError()}}

	tpl := defaultErrorTpl()

	tplPath := registry.Config().UString("frontend.errorTemplate")
	if tplPath != "" {
		t, err := template.ParseFiles(tplPath)
		if err != nil {
			registry.Logger().Fatalf("Could not parse error template at '%v': %v", tplPath, err)
		} else {
			tpl = t
		}
	}

	var buffer *bytes.Buffer
	if err := tpl.Execute(buffer, data); err != nil {
		registry.Logger().Fatalf("Could not render error template: %v\n", err)
		response.SetRawData([]byte("Server error"))
	} else {
		response.SetRawData(buffer.Bytes())
	}

	return nil, false
}
示例#2
0
// SerializeResponse converts a response with model data into the target format.
func (s *Serializer) SerializeResponse(response kit.Response) (interface{}, apperror.Error) {
	transData := response.GetTransferData()
	if transData == nil {
		transData = &kit.AppTransferData{}

		data := response.GetData()
		if model, ok := data.(kit.Model); ok {
			transData.SetModels([]kit.Model{model})
		} else if models, ok := data.([]kit.Model); ok {
			transData.SetModels(models)
		} else {
			transData.SetData(data)
		}
	}

	// Append response metadata.
	if meta := response.GetMeta(); meta != nil {
		transMeta := transData.GetMeta()
		if transMeta != nil {
			for key, val := range meta {
				transMeta[key] = val
			}
		} else {
			transData.SetMeta(meta)
		}
	}

	// If a response error is defined, prepend it to all errors that might have been in
	// TransferData.
	if err := response.GetError(); err != nil {
		oldErrs := transData.GetErrors()
		transData.SetErrors(append([]apperror.Error{err}, oldErrs...))
	}

	data, err := s.SerializeTransferData(transData)
	if err != nil {
		return nil, err
	}

	return data, nil
}
示例#3
0
文件: wamp.go 项目: app-kit/go-appkit
func convertResponse(response kit.Response) *turnpike.CallResult {
	result := &turnpike.CallResult{}

	if response.GetError() != nil {
		result.Err = turnpike.URI(response.GetError().GetCode())
	}

	result.Kwargs = map[string]interface{}{
		"data":   response.GetData(),
		"meta":   response.GetMeta(),
		"errors": []error{response.GetError()},
	}

	return result
}
示例#4
0
func RequestLoggerMiddleware(registry kit.Registry, r kit.Request, response kit.Response) (kit.Response, bool) {

	// Calculate time taken.
	rawStarted, ok1 := r.GetContext().Get("startTime")
	rawFinished, ok2 := r.GetContext().Get("endTime")

	timeTaken := int64(-1)
	if ok1 && ok2 {
		started := rawStarted.(time.Time)
		finished := rawFinished.(time.Time)
		timeTaken = int64(finished.Sub(started) / time.Millisecond)
	}

	// Log the request.
	method := r.GetHttpMethod()
	path := r.GetPath()
	if response.GetError() != nil {
		registry.Logger().WithFields(logrus.Fields{
			"frontend":     r.GetFrontend(),
			"action":       "request",
			"method":       method,
			"path":         path,
			"status":       response.GetHttpStatus(),
			"err":          response.GetError(),
			"milliseconds": timeTaken,
		}).Errorf("%v: %v - %v - %v", response.GetHttpStatus(), method, path, response.GetError())
	} else {
		registry.Logger().WithFields(logrus.Fields{
			"frontend":     r.GetFrontend(),
			"action":       "request",
			"method":       method,
			"path":         path,
			"status":       response.GetHttpStatus(),
			"milliseconds": timeTaken,
		}).Debugf("%v: %v - %v", response.GetHttpStatus(), method, path)
	}

	return nil, false
}
示例#5
0
文件: api.go 项目: app-kit/go-appkit
func RespondWithJson(w http.ResponseWriter, response kit.Response) {
	code := 200
	respData := map[string]interface{}{
		"data": response.GetData(),
	}

	if response.GetError() != nil {
		errs := []error{response.GetError()}

		additionalErrs := response.GetError().GetErrors()
		if additionalErrs != nil {
			for _, err := range additionalErrs {
				if apiErr, ok := err.(apperror.Error); ok && apiErr.IsPublic() {
					errs = append(errs, apiErr)
				}
			}
		}

		respData["errors"] = errs
		code = 500
	}

	output, err2 := json.Marshal(respData)
	if err2 != nil {
		code = 500
		respData = map[string]interface{}{
			"errors": []error{
				apperror.Wrap(err2, "json_encode_error"),
			},
		}
		output, _ = json.Marshal(respData)
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(code)
	w.Write(output)
}