grafana/pkg/api/routing/routing.go
Hugo Häggmark 3d41267fc4
Chore: Moves common and response into separate packages (#30298)
* Chore: moves common and response into separate packages

* Chore: moves common and response into separate packages

* Update pkg/api/utils/common.go

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Chore: changes after PR comments

* Chore: move wrap to routing package

* Chore: move functions in common to response package

* Chore: move functions in common to response package

* Chore: formats imports

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
2021-01-15 14:43:20 +01:00

28 lines
557 B
Go

package routing
import (
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/models"
"gopkg.in/macaron.v1"
)
var (
ServerError = func(err error) response.Response {
return response.Error(500, "Server error", err)
}
)
func Wrap(action interface{}) macaron.Handler {
return func(c *models.ReqContext) {
var res response.Response
val, err := c.Invoke(action)
if err == nil && val != nil && len(val) > 0 {
res = val[0].Interface().(response.Response)
} else {
res = ServerError(err)
}
res.WriteTo(c)
}
}