diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index c0784cda28c..47c77425843 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -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{ diff --git a/pkg/services/annotations/annotations.go b/pkg/services/annotations/annotations.go index 838eca98d3f..400bbff939b 100644 --- a/pkg/services/annotations/annotations.go +++ b/pkg/services/annotations/annotations.go @@ -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 diff --git a/pkg/services/sqlstore/annotation.go b/pkg/services/sqlstore/annotation.go index c481a09b7b4..0a752cd7a20 100644 --- a/pkg/services/sqlstore/annotation.go +++ b/pkg/services/sqlstore/annotation.go @@ -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 }