Johannes Batzill b7b9f53b0d Improve error handling to match go standards - don't wrap and rethrow, but log and return. Also adds some more validations for path creation and resource moving. Add accesslogging for git and api router (#14)
This commit contains the following:
- Improve and simplify error handling (remove unnecessary wrappers, make it feel like go)
- Add extra validation for path creation and resource moving (path has to be within same top space, no top space alias allowed)
- Add access logging for rest api and git api
2022-09-09 22:08:46 -07:00

29 lines
659 B
Go

package platform
import (
"encoding/json"
"net/http"
"github.com/harness/gitness/internal/api/render"
)
// RenderResource is a helper function that renders a single
// resource, wrapped in the harness payload envelope.
func RenderResource(w http.ResponseWriter, code int, v interface{}) {
payload := new(wrapper)
payload.Status = "SUCCESS"
payload.Data, _ = json.Marshal(v)
if code > 399 {
payload.Status = "ERROR"
} else if code > 299 {
payload.Status = "FAILURE"
}
render.JSON(w, code, payload)
}
// wrapper defines the payload wrapper.
type wrapper struct {
Status string `json:"status"`
Data json.RawMessage `json:"data"`
}