Skip to content

Commit

Permalink
Merge pull request #155 from yaacov/use-errors-in-api-server
Browse files Browse the repository at this point in the history
Use server errors in api
  • Loading branch information
yaacov authored Jan 10, 2018
2 parents 7d5885d + 2354a71 commit 32809dc
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/server/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,14 @@ func (h APIHhandler) GetAlerts(w http.ResponseWriter, r *http.Request, argv map[
// GetTenants return a list of metrics tenants
func (h APIHhandler) GetTenants(w http.ResponseWriter, r *http.Request, argv map[string]string) error {
var res []storage.Tenant
var resJSON []byte
var err error

res, _ = h.Storage.GetTenants()
resJSON, err := json.Marshal(res)
if err != nil {
return err
if res, err = h.Storage.GetTenants(); err == nil {
resJSON, err = json.Marshal(res)
if err != nil {
return err
}
}

fmt.Fprintln(w, string(resJSON))
Expand All @@ -111,9 +114,10 @@ func (h APIHhandler) GetTenants(w http.ResponseWriter, r *http.Request, argv map
// GetMetrics return a list of metrics definitions
func (h APIHhandler) GetMetrics(w http.ResponseWriter, r *http.Request, argv map[string]string) error {
var res []storage.Item
var err error

// get data from the form arguments
if err := r.ParseForm(); err != nil {
if err = r.ParseForm(); err != nil {
return err
}

Expand All @@ -132,13 +136,15 @@ func (h APIHhandler) GetMetrics(w http.ResponseWriter, r *http.Request, argv map
if !validTags(tags) {
return errBadMetricID
}
res, _ = h.Storage.GetItemList(tenant, tags)
res, err = h.Storage.GetItemList(tenant, tags)
} else {
res, _ = h.Storage.GetItemList(tenant, map[string]string{})
res, err = h.Storage.GetItemList(tenant, map[string]string{})
}
resJSON, err := json.Marshal(res)

if err == nil {
fmt.Fprintln(w, string(resJSON))
if resJSON, err := json.Marshal(res); err == nil {
fmt.Fprintln(w, string(resJSON))
}
}

return err
Expand Down Expand Up @@ -213,11 +219,14 @@ func (h APIHhandler) DeleteData(w http.ResponseWriter, r *http.Request, argv map

// call storage for data
if start < end {
h.Storage.DeleteData(tenant, id, end, start)
err = h.Storage.DeleteData(tenant, id, end, start)

// output to client
fmt.Fprintf(w, "{\"message\":\"Deleted %s@%s [%d-%d]\"}", tenant, id, end, start)
return nil
if err == nil {
fmt.Fprintf(w, "{\"message\":\"Deleted %s@%s [%d-%d]\"}", tenant, id, end, start)
}

return err
}

return errors.New("Can't delete time range")
Expand All @@ -239,7 +248,7 @@ func (h APIHhandler) PostMQuery(w http.ResponseWriter, r *http.Request, argv map
fmt.Fprintf(w, "\"%s\":", id)

// call storage for data, and send it to writer
if err := h.getData(w, tenant, id, end, start, limit, order, bucketDuration); err != nil {
if err = h.getData(w, tenant, id, end, start, limit, order, bucketDuration); err != nil {
return err
}

Expand Down Expand Up @@ -309,7 +318,10 @@ func (h APIHhandler) PostData(w http.ResponseWriter, r *http.Request, argv map[s
if h.Verbose {
log.Printf("Tenant: %s, ID: %+v {timestamp: %+v, value: %+v}\n", tenant, id, timestamp, value)
}
h.Storage.PostRawData(tenant, id, timestamp, value)

if err := h.Storage.PostRawData(tenant, id, timestamp, value); err != nil {
return err
}
}
}

Expand All @@ -336,7 +348,10 @@ func (h APIHhandler) PutTags(w http.ResponseWriter, r *http.Request, argv map[st
if h.Verbose {
log.Printf("Tenant: %s, ID: %+v {tags: %+v}\n", tenant, id, tags)
}
h.Storage.PutTags(tenant, id, tags)

if err := h.Storage.PutTags(tenant, id, tags); err != nil {
return err
}

fmt.Fprintf(w, "{\"message\":\"Updated tags for %s@%s\"}", tenant, id)
return nil
Expand Down Expand Up @@ -364,7 +379,9 @@ func (h APIHhandler) PutMultiTags(w http.ResponseWriter, r *http.Request, argv m
if h.Verbose {
log.Printf("Tenant: %s, ID: %+v {tags: %+v}\n", tenant, id, item.Tags)
}
h.Storage.PutTags(tenant, id, item.Tags)
if err := h.Storage.PutTags(tenant, id, item.Tags); err != nil {
return err
}
}
}

Expand All @@ -385,7 +402,9 @@ func (h APIHhandler) DeleteTags(w http.ResponseWriter, r *http.Request, argv map
// get tenant
tenant := parseTenant(r)

h.Storage.DeleteTags(tenant, id, tags)
if err := h.Storage.DeleteTags(tenant, id, tags); err != nil {
return err
}

fmt.Fprintf(w, "{\"message\":\"Deleted tags for %s@%s\"}", tenant, id)
return nil
Expand Down Expand Up @@ -494,11 +513,13 @@ func (h APIHhandler) getData(w http.ResponseWriter, tenant string, id string, en

// call storage for data
if bucketDuration == 0 {
res, _ := h.Storage.GetRawData(tenant, id, end, start, limit, order)
resJSON, err = json.Marshal(res)
if res, errQuery := h.Storage.GetRawData(tenant, id, end, start, limit, order); errQuery == nil {
resJSON, err = json.Marshal(res)
}
} else {
res, _ := h.Storage.GetStatData(tenant, id, end, start, limit, order, bucketDuration)
resJSON, err = json.Marshal(res)
if res, errQuery := h.Storage.GetStatData(tenant, id, end, start, limit, order, bucketDuration); errQuery == nil {
resJSON, err = json.Marshal(res)
}
}
if err == nil {
fmt.Fprintf(w, string(resJSON))
Expand Down

0 comments on commit 32809dc

Please sign in to comment.