API: Return 400 on invalid Annotation requests (#32429)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
Carl Bergquist 2021-03-29 15:47:16 +02:00 committed by GitHub
parent 636be0ed22
commit e1458391bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package api
import (
"errors"
"strings"
"github.com/grafana/grafana/pkg/api/dtos"
@ -59,7 +60,7 @@ func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) response.
if cmd.Text == "" {
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{
@ -75,6 +76,9 @@ func PostAnnotation(c *models.ReqContext, cmd dtos.PostAnnotationsCmd) response.
}
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)
}
@ -99,7 +103,7 @@ func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotatio
if cmd.What == "" {
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)
@ -119,12 +123,12 @@ func PostGraphiteAnnotation(c *models.ReqContext, cmd dtos.PostGraphiteAnnotatio
tagsArray = append(tagsArray, tagStr)
} else {
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:
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{

View File

@ -2,11 +2,16 @@ package annotations
import (
"context"
"errors"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/setting"
)
var (
ErrTimerangeMissing = errors.New("missing timerange")
)
type Repository interface {
Save(item *Item) error
Update(item *Item) error

View File

@ -15,7 +15,7 @@ import (
func validateTimeRange(item *annotations.Item) error {
if item.EpochEnd == 0 {
if item.Epoch == 0 {
return errors.New("missing time range")
return annotations.ErrTimerangeMissing
}
item.EpochEnd = item.Epoch
}