replace shorturl errors with generated errors
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/models/errs"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
@@ -17,7 +18,7 @@ import (
|
||||
func (hs *HTTPServer) createShortURL(c *models.ReqContext) response.Response {
|
||||
cmd := dtos.CreateShortURLCmd{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Err(models.ErrShortURLBadRequest.Errorf("bad request data: %w", err))
|
||||
return response.Err(errs.ErrShorturlBadRequest.Errorf("bad request data: %w", err))
|
||||
}
|
||||
hs.log.Debug("Received request to create short URL", "path", cmd.Path)
|
||||
shortURL, err := hs.ShortURLService.CreateShortURL(c.Req.Context(), c.SignedInUser, cmd.Path)
|
||||
@@ -45,7 +46,7 @@ func (hs *HTTPServer) redirectFromShortURL(c *models.ReqContext) {
|
||||
|
||||
shortURL, err := hs.ShortURLService.GetShortURLByUID(c.Req.Context(), c.SignedInUser, shortURLUID)
|
||||
if err != nil {
|
||||
if models.ErrShortURLNotFound.Is(err) {
|
||||
if errs.ErrShorturlNotFound.Is(err) {
|
||||
hs.log.Debug("Not redirecting short URL since not found")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,16 +2,6 @@ package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrShortURLBadRequest = errutil.NewBase(errutil.StatusBadRequest, "shorturl.bad-request")
|
||||
ErrShortURLNotFound = errutil.NewBase(errutil.StatusNotFound, "shorturl.not-found")
|
||||
ErrShortURLAbsolutePath = errutil.NewBase(errutil.StatusValidationFailed, "shorturl.absolute-path", errutil.WithPublicMessage("Path should be relative"))
|
||||
ErrShortURLInvalidPath = errutil.NewBase(errutil.StatusValidationFailed, "shorturl.invalid-path", errutil.WithPublicMessage("Invalid short URL path"))
|
||||
ErrShortURLInternal = errutil.NewBase(errutil.StatusInternal, "shorturl.internal")
|
||||
)
|
||||
|
||||
type ShortUrl struct {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/models/errs"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
@@ -39,7 +40,7 @@ func (s ShortURLService) GetShortURLByUID(ctx context.Context, user *user.Signed
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return models.ErrShortURLNotFound.Errorf("short URL not found")
|
||||
return errs.ErrShorturlNotFound.Errorf("short URL not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -67,10 +68,10 @@ func (s ShortURLService) CreateShortURL(ctx context.Context, user *user.SignedIn
|
||||
relPath = strings.TrimSpace(relPath)
|
||||
|
||||
if path.IsAbs(relPath) {
|
||||
return nil, models.ErrShortURLAbsolutePath.Errorf("expected relative path: %s", relPath)
|
||||
return nil, errs.ErrShorturlAbsolutePath.Errorf("expected relative path: %s", relPath)
|
||||
}
|
||||
if strings.Contains(relPath, "../") {
|
||||
return nil, models.ErrShortURLInvalidPath.Errorf("path cannot contain '../': %s", relPath)
|
||||
return nil, errs.ErrShorturlInvalidPath.Errorf("path cannot contain '../': %s", relPath)
|
||||
}
|
||||
|
||||
now := time.Now().Unix()
|
||||
@@ -87,7 +88,7 @@ func (s ShortURLService) CreateShortURL(ctx context.Context, user *user.SignedIn
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, models.ErrShortURLInternal.Errorf("failed to insert shorturl: %w", err)
|
||||
return nil, errs.ErrShorturlInternal.Errorf("failed to insert shorturl: %w", err)
|
||||
}
|
||||
|
||||
return &shortURL, nil
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models/errs"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
@@ -81,7 +83,7 @@ func TestShortURLService(t *testing.T) {
|
||||
|
||||
shortURL, err := service.GetShortURLByUID(context.Background(), user, "testnotfounduid")
|
||||
require.Error(t, err)
|
||||
require.True(t, models.ErrShortURLNotFound.Is(err))
|
||||
require.True(t, errs.ErrShorturlNotFound.Is(err))
|
||||
require.Nil(t, shortURL)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user