Added helper function to get logger

This commit is contained in:
kolaente 2019-01-23 15:19:02 +01:00
parent 1c90eb2253
commit b4a943dba1
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
6 changed files with 11 additions and 7 deletions

View File

@ -18,7 +18,6 @@ package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"github.com/op/go-logging"
"net/http"
)
@ -46,7 +45,7 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
// Check rights
if !currentStruct.CanCreate(currentAuth) {
ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to create while not having the rights for it", currentAuth)
getLogger(ctx).Noticef("Tried to create while not having the rights for it", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}

View File

@ -44,7 +44,7 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError)
}
if !currentStruct.CanDelete(currentAuth) {
ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to delete while not having the rights for it", currentAuth)
getLogger(ctx).Noticef("Tried to delete while not having the rights for it", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}

View File

@ -40,6 +40,11 @@ func HandleHTTPError(err error, ctx echo.Context) *echo.HTTPError {
errDetails := a.HTTPError()
return echo.NewHTTPError(errDetails.HTTPCode, errDetails)
}
ctx.Get("LoggingProvider").(*logging.Logger).Error(err.Error())
getLogger(ctx).Error(err.Error())
return echo.NewHTTPError(http.StatusInternalServerError)
}
// Helper function to get the logger
func getLogger(ctx echo.Context) *logging.Logger {
return ctx.Get("LoggingProvider").(*logging.Logger)
}

View File

@ -46,7 +46,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
}
pageNumber, err := strconv.Atoi(page)
if err != nil {
ctx.Get("LoggingProvider").(*logging.Logger).Error(err.Error())
getLogger(ctx).Error(err.Error())
return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.")
}
if pageNumber < 0 {

View File

@ -46,7 +46,7 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !currentStruct.CanRead(currentAuth) {
ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to read one while not having the rights for it", currentAuth)
getLogger(ctx).Noticef("Tried to read one while not having the rights for it", currentAuth)
return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this")
}

View File

@ -45,7 +45,7 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !currentStruct.CanUpdate(currentAuth) {
ctx.Get("LoggingProvider").(*logging.Logger).Noticef("Tried to update while not having the rights for it", currentAuth)
getLogger(ctx).Noticef("Tried to update while not having the rights for it", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}