mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
API: Return 400 on invalid Annotation requests (#32429)
Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
parent
636be0ed22
commit
e1458391bb
@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/api/dtos"
|
"github.com/grafana/grafana/pkg/api/dtos"
|
||||||
@ -59,7 +60,7 @@ func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) response.
|
|||||||
|
|
||||||
if cmd.Text == "" {
|
if cmd.Text == "" {
|
||||||
err := &CreateAnnotationError{"text field should not be empty"}
|
err := &CreateAnnotationError{"text field should not be empty"}
|
||||||
return response.Error(500, "Failed to save annotation", err)
|
return response.Error(400, "Failed to save annotation", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
item := annotations.Item{
|
item := annotations.Item{
|
||||||
@ -75,6 +76,9 @@ func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) response.
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := repo.Save(&item); err != nil {
|
if err := repo.Save(&item); err != nil {
|
||||||
|
if errors.Is(err, annotations.ErrTimerangeMissing) {
|
||||||
|
return response.Error(400, "Failed to save annotation", err)
|
||||||
|
}
|
||||||
return response.Error(500, "Failed to save annotation", err)
|
return response.Error(500, "Failed to save annotation", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +103,7 @@ func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotatio
|
|||||||
|
|
||||||
if cmd.What == "" {
|
if cmd.What == "" {
|
||||||
err := &CreateAnnotationError{"what field should not be empty"}
|
err := &CreateAnnotationError{"what field should not be empty"}
|
||||||
return response.Error(500, "Failed to save Graphite annotation", err)
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
text := formatGraphiteAnnotation(cmd.What, cmd.Data)
|
text := formatGraphiteAnnotation(cmd.What, cmd.Data)
|
||||||
@ -119,12 +123,12 @@ func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotatio
|
|||||||
tagsArray = append(tagsArray, tagStr)
|
tagsArray = append(tagsArray, tagStr)
|
||||||
} else {
|
} else {
|
||||||
err := &CreateAnnotationError{"tag should be a string"}
|
err := &CreateAnnotationError{"tag should be a string"}
|
||||||
return response.Error(500, "Failed to save Graphite annotation", err)
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
err := &CreateAnnotationError{"unsupported tags format"}
|
err := &CreateAnnotationError{"unsupported tags format"}
|
||||||
return response.Error(500, "Failed to save Graphite annotation", err)
|
return response.Error(400, "Failed to save Graphite annotation", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
item := annotations.Item{
|
item := annotations.Item{
|
||||||
|
@ -2,11 +2,16 @@ package annotations
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrTimerangeMissing = errors.New("missing timerange")
|
||||||
|
)
|
||||||
|
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
Save(item *Item) error
|
Save(item *Item) error
|
||||||
Update(item *Item) error
|
Update(item *Item) error
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
func validateTimeRange(item *annotations.Item) error {
|
func validateTimeRange(item *annotations.Item) error {
|
||||||
if item.EpochEnd == 0 {
|
if item.EpochEnd == 0 {
|
||||||
if item.Epoch == 0 {
|
if item.Epoch == 0 {
|
||||||
return errors.New("missing time range")
|
return annotations.ErrTimerangeMissing
|
||||||
}
|
}
|
||||||
item.EpochEnd = item.Epoch
|
item.EpochEnd = item.Epoch
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user