ShortURL: Use new Error type (#50859)

This commit is contained in:
Emil Tullstedt
2022-06-15 15:11:36 +02:00
committed by GitHub
parent f82264c2b1
commit f1834163ec
5 changed files with 40 additions and 25 deletions
+3 -17
View File
@@ -1,10 +1,8 @@
package api
import (
"errors"
"fmt"
"net/http"
"path"
"strings"
"github.com/grafana/grafana/pkg/api/dtos"
@@ -19,24 +17,12 @@ import (
func (hs *HTTPServer) createShortURL(c *models.ReqContext) response.Response {
cmd := dtos.CreateShortURLCmd{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
return response.Err(models.ErrShortURLBadRequest.Errorf("bad request data: %w", err))
}
hs.log.Debug("Received request to create short URL", "path", cmd.Path)
cmd.Path = strings.TrimSpace(cmd.Path)
if path.IsAbs(cmd.Path) {
hs.log.Error("Invalid short URL path", "path", cmd.Path)
return response.Error(400, "Path should be relative", nil)
}
if strings.Contains(cmd.Path, "../") {
hs.log.Error("Invalid short URL path", "path", cmd.Path)
return response.Error(400, "Invalid path", nil)
}
shortURL, err := hs.ShortURLService.CreateShortURL(c.Req.Context(), c.SignedInUser, cmd.Path)
if err != nil {
return response.Error(500, "Failed to create short URL", err)
return response.Err(err)
}
url := fmt.Sprintf("%s/goto/%s?orgId=%d", strings.TrimSuffix(setting.AppUrl, "/"), shortURL.Uid, c.OrgId)
@@ -59,7 +45,7 @@ func (hs *HTTPServer) redirectFromShortURL(c *models.ReqContext) {
shortURL, err := hs.ShortURLService.GetShortURLByUID(c.Req.Context(), c.SignedInUser, shortURLUID)
if err != nil {
if errors.Is(err, models.ErrShortURLNotFound) {
if models.ErrShortURLNotFound.Is(err) {
hs.log.Debug("Not redirecting short URL since not found")
return
}