diff --git a/router/admin.go b/router/admin.go index b695f249..dd9741b7 100644 --- a/router/admin.go +++ b/router/admin.go @@ -6,12 +6,14 @@ import ( "github.com/google/uuid" "github.com/labstack/echo/v4" "github.com/traPtitech/Jomon/ent" + "go.uber.org/zap" ) func (h Handlers) GetAdmins(c echo.Context) error { ctx := c.Request().Context() admins, err := h.Repository.GetAdmins(ctx) if err != nil { + h.Logger.Error("failed to get admins from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -26,6 +28,7 @@ func (h Handlers) GetAdmins(c echo.Context) error { func (h Handlers) PostAdmins(c echo.Context) error { var admin []uuid.UUID if err := c.Bind(&admin); err != nil { + h.Logger.Info("failed to get admin id from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } @@ -33,8 +36,10 @@ func (h Handlers) PostAdmins(c echo.Context) error { err := h.Repository.AddAdmins(ctx, admin) if err != nil { if ent.IsConstraintError(err) { + h.Logger.Info("constraint error while adding admin in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } + h.Logger.Error("failed to add admin in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -44,12 +49,14 @@ func (h Handlers) PostAdmins(c echo.Context) error { func (h Handlers) DeleteAdmins(c echo.Context) error { var admin []uuid.UUID if err := c.Bind(&admin); err != nil { + h.Logger.Info("failed to get admin id from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() err := h.Repository.DeleteAdmins(ctx, admin) if err != nil { + h.Logger.Error("failed to delete admin from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } diff --git a/router/auth.go b/router/auth.go index a0345688..2b4fdaec 100644 --- a/router/auth.go +++ b/router/auth.go @@ -15,6 +15,7 @@ import ( "github.com/traPtitech/Jomon/ent" "github.com/traPtitech/Jomon/model" "github.com/traPtitech/Jomon/service" + "go.uber.org/zap" ) const ( @@ -36,6 +37,7 @@ func (h Handlers) AuthCallback(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -52,11 +54,13 @@ func (h Handlers) AuthCallback(c echo.Context) error { res, err := service.RequestAccessToken(code, codeVerifier) if err != nil { + h.Logger.Error("failed to get access token", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } u, err := service.FetchTraQUserInfo(res.AccessToken) if err != nil { + h.Logger.Error("failed to fetch traQ user info", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -66,9 +70,11 @@ func (h Handlers) AuthCallback(c echo.Context) error { if ent.IsNotFound(err) { modelUser, err = h.Repository.CreateUser(c.Request().Context(), u.Name, u.DisplayName, false) if err != nil { + h.Logger.Error("failed to create user", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } } else { + h.Logger.Error("failed to get user by name", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } } @@ -83,6 +89,7 @@ func (h Handlers) AuthCallback(c echo.Context) error { sess.Values[sessionUserKey] = user if err = sess.Save(c.Request(), c.Response()); err != nil { + h.Logger.Error("failed to save session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -92,6 +99,7 @@ func (h Handlers) AuthCallback(c echo.Context) error { func (h Handlers) GeneratePKCE(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -111,6 +119,7 @@ func (h Handlers) GeneratePKCE(c echo.Context) error { err = sess.Save(c.Request(), c.Response()) if err != nil { + h.Logger.Error("failed to save session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } diff --git a/router/file.go b/router/file.go index cd878b0a..01dc6174 100644 --- a/router/file.go +++ b/router/file.go @@ -9,6 +9,7 @@ import ( "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" "github.com/traPtitech/Jomon/ent" + "go.uber.org/zap" ) type FileResponse struct { @@ -36,34 +37,41 @@ var acceptedMimeTypes = map[string]bool{ func (h Handlers) PostFile(c echo.Context) error { form, err := c.MultipartForm() if err != nil { + h.Logger.Error("failed to parse request as multipart/form-data", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } files, ok := form.File["file"] if !ok || len(files) != 1 { + h.Logger.Info("could not find field `file` in request, or its length is not 1") return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid file")) } reqfile := files[0] names, ok := form.Value["name"] if !ok || len(names) != 1 { + h.Logger.Info("could not find field `name` in request, or its length is not 1") return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid file name")) } name := names[0] requestIDs, ok := form.Value["request_id"] if !ok || len(requestIDs) != 1 { + h.Logger.Info("could not find field `request_id` in request, or its length is not 1") return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid file request id")) } requestID, err := uuid.Parse(requestIDs[0]) if err != nil { + h.Logger.Info("could not parse request_id as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } mimetype := reqfile.Header.Get(echo.HeaderContentType) if !acceptedMimeTypes[mimetype] { + h.Logger.Info("requested unsupported mime type", zap.String("mime-type", mimetype)) return echo.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Errorf("unsupported media type")) } src, err := reqfile.Open() if err != nil { + h.Logger.Error("failed to open requested file", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } defer src.Close() @@ -71,22 +79,26 @@ func (h Handlers) PostFile(c echo.Context) error { // get create user sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, ok := sess.Values[sessionUserKey].(User) if !ok { + h.Logger.Error("failed to parse stored session as user info") return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("invalid user")) } ctx := c.Request().Context() file, err := h.Repository.CreateFile(ctx, name, mimetype, requestID, user.ID) if err != nil { + h.Logger.Error("failed to create file in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } err = h.Storage.Save(file.ID.String(), src) if err != nil { + h.Logger.Error("failed to save file id in storage", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -96,6 +108,7 @@ func (h Handlers) PostFile(c echo.Context) error { func (h Handlers) GetFile(c echo.Context) error { fileID, err := uuid.Parse(c.Param("fileID")) if err != nil { + h.Logger.Error("could not parse query parameter `fileID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } @@ -103,8 +116,10 @@ func (h Handlers) GetFile(c echo.Context) error { file, err := h.Repository.GetFile(ctx, fileID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find file in repository", zap.String("ID", fileID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get file from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -114,15 +129,24 @@ func (h Handlers) GetFile(c echo.Context) error { if im != "" { imt, err := http.ParseTime(im) if err != nil { + h.Logger.Info("could not parse time in request header", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if modifiedAt.Before(imt) || modifiedAt.Equal(imt) { + h.Logger.Info( + "content is not modified since the last request", + zap.String("ID", fileID.String()), + zap.Time("If-Modified-Since", imt)) return c.NoContent(http.StatusNotModified) } } f, err := h.Storage.Open(fileID.String()) if err != nil { + h.Logger.Error( + "failed to open file in storage", + zap.String("ID", fileID.String()), + zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } defer f.Close() @@ -136,6 +160,7 @@ func (h Handlers) GetFile(c echo.Context) error { func (h Handlers) GetFileMeta(c echo.Context) error { fileID, err := uuid.Parse(c.Param("fileID")) if err != nil { + h.Logger.Info("could not parse query parameter `fileID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } @@ -143,8 +168,10 @@ func (h Handlers) GetFileMeta(c echo.Context) error { file, err := h.Repository.GetFile(ctx, fileID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find file in repository", zap.String("ID", fileID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get file from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -160,6 +187,7 @@ func (h Handlers) GetFileMeta(c echo.Context) error { func (h Handlers) DeleteFile(c echo.Context) error { fileID, err := uuid.Parse(c.Param("fileID")) if err != nil { + h.Logger.Info("could not parse query parameter `fileID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } @@ -167,13 +195,16 @@ func (h Handlers) DeleteFile(c echo.Context) error { err = h.Repository.DeleteFile(ctx, fileID) if err != nil { if ent.IsConstraintError(err) { + h.Logger.Info("constraint error while deleting file", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } + h.Logger.Error("failed to delete file in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } err = h.Storage.Delete(fileID.String()) if err != nil { + h.Logger.Error("failed to delete file in storage", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } diff --git a/router/group.go b/router/group.go index 69f5cf4b..b468f4ba 100644 --- a/router/group.go +++ b/router/group.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" "github.com/labstack/echo/v4" "github.com/traPtitech/Jomon/ent" + "go.uber.org/zap" ) type Group struct { @@ -56,6 +57,7 @@ func (h Handlers) GetGroups(c echo.Context) error { ctx := c.Request().Context() groups, err := h.Repository.GetGroups(ctx) if err != nil { + h.Logger.Error("failed to get groups from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -78,12 +80,14 @@ func (h Handlers) GetGroups(c echo.Context) error { func (h Handlers) PostGroup(c echo.Context) error { var group Group if err := c.Bind(&group); err != nil { + h.Logger.Info("failed to get group from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() created, err := h.Repository.CreateGroup(ctx, group.Name, group.Description, group.Budget) if err != nil { + h.Logger.Error("failed to create group in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -103,9 +107,11 @@ func (h Handlers) PostGroup(c echo.Context) error { func (h Handlers) GetGroupDetail(c echo.Context) error { groupID, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse query parameter `groupID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if groupID == uuid.Nil { + h.Logger.Info("received invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } @@ -113,8 +119,13 @@ func (h Handlers) GetGroupDetail(c echo.Context) error { group, err := h.Repository.GetGroup(ctx, groupID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info( + "could not fin group in repository", + zap.String("ID", groupID.String()), + zap.Error(err)) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get group from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } res := GroupDetail{ @@ -129,6 +140,7 @@ func (h Handlers) GetGroupDetail(c echo.Context) error { } owners, err := h.Repository.GetOwners(ctx, groupID) if err != nil { + h.Logger.Error("failed to get owners from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } for _, owner := range owners { @@ -136,6 +148,7 @@ func (h Handlers) GetGroupDetail(c echo.Context) error { } members, err := h.Repository.GetMembers(ctx, groupID) if err != nil { + h.Logger.Error("failed to get members from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } for _, member := range members { @@ -149,11 +162,13 @@ func (h Handlers) GetGroupDetail(c echo.Context) error { func (h Handlers) PutGroup(c echo.Context) error { var group Group if err := c.Bind(&group); err != nil { + h.Logger.Info("could not get group from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } groupID, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse query parameter `groupID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if groupID == uuid.Nil { @@ -163,6 +178,7 @@ func (h Handlers) PutGroup(c echo.Context) error { ctx := c.Request().Context() updated, err := h.Repository.UpdateGroup(ctx, groupID, group.Name, group.Description, group.Budget) if err != nil { + h.Logger.Error("failed to update group in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -182,15 +198,18 @@ func (h Handlers) PutGroup(c echo.Context) error { func (h Handlers) DeleteGroup(c echo.Context) error { groupID, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse query parameter `groupID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if groupID == uuid.Nil { + h.Logger.Info("received invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } ctx := c.Request().Context() err = h.Repository.DeleteGroup(ctx, groupID) if err != nil { + h.Logger.Error("failed to delete group from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -202,17 +221,21 @@ func (h Handlers) PostMember(c echo.Context) error { ctx := c.Request().Context() groupID, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse query parameter `groupID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if groupID == uuid.Nil { + h.Logger.Info("received invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } var member []uuid.UUID if err := c.Bind(&member); err != nil { + h.Logger.Info("could not get member id from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } added, err := h.Repository.AddMembers(ctx, groupID, member) if err != nil { + h.Logger.Error("failed to add member in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } res := []*uuid.UUID{} @@ -227,17 +250,21 @@ func (h Handlers) DeleteMember(c echo.Context) error { ctx := c.Request().Context() groupID, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse query parameter `groupID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if groupID == uuid.Nil { + h.Logger.Info("received invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } var member []uuid.UUID if err := c.Bind(&member); err != nil { + h.Logger.Info("could not get member id from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } err = h.Repository.DeleteMembers(ctx, groupID, member) if err != nil { + h.Logger.Error("failed to delete member from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } return c.NoContent(http.StatusOK) @@ -249,19 +276,24 @@ func (h Handlers) PostOwner(c echo.Context) error { var owners []uuid.UUID groupID, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse query parameter `groupID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if groupID == uuid.Nil { + h.Logger.Info("received invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } if err := c.Bind(&owners); err != nil { + h.Logger.Info("could not get owner id from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } added, err := h.Repository.AddOwners(ctx, groupID, owners) if err != nil { if ent.IsConstraintError(err) { + h.Logger.Info("constraint error while adding owner in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } + h.Logger.Error("failed to add owner in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -278,21 +310,26 @@ func (h Handlers) DeleteOwner(c echo.Context) error { ctx := c.Request().Context() groupID, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse query parameter `groupID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if groupID == uuid.Nil { + h.Logger.Info("received invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } var ownerIDs []uuid.UUID if err := c.Bind(&ownerIDs); err != nil { + h.Logger.Info("could not get owner id from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } err = h.Repository.DeleteOwners(ctx, groupID, ownerIDs) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find owner in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to delete owner from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } diff --git a/router/middleware.go b/router/middleware.go index a0ae9156..ff3cc2b7 100644 --- a/router/middleware.go +++ b/router/middleware.go @@ -69,6 +69,7 @@ func (h Handlers) CheckLoginMiddleware(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -85,11 +86,13 @@ func (h Handlers) CheckAdminMiddleware(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, err := getUserInfo(sess) if err != nil { + h.Logger.Error("failed to get user info", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -105,11 +108,13 @@ func (h Handlers) CheckRequestCreatorMiddleware(next echo.HandlerFunc) echo.Hand return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, err := getUserInfo(sess) if err != nil { + h.Logger.Error("failed to get user info", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -126,11 +131,13 @@ func (h Handlers) CheckAdminOrRequestCreatorMiddleware(next echo.HandlerFunc) ec return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, err := getUserInfo(sess) if err != nil { + h.Logger.Error("failed to get user info", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -147,16 +154,19 @@ func (h Handlers) CheckAdminOrGroupOwnerMiddleware(next echo.HandlerFunc) echo.H return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, err := getUserInfo(sess) if err != nil { + h.Logger.Error("failed to get user info", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } owners, ok := sess.Values[sessionOwnerKey].([]*model.Owner) if !ok { + h.Logger.Error("failed to get group owner", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, "session owner key is not set") } @@ -178,11 +188,13 @@ func (h Handlers) CheckFileCreatorMiddleware(next echo.HandlerFunc) echo.Handler return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, err := getUserInfo(sess) if err != nil { + h.Logger.Error("failed to get user info", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -221,22 +233,26 @@ func (h Handlers) RetrieveGroupOwner() echo.MiddlewareFunc { return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } id, err := uuid.Parse(c.Param("groupID")) if err != nil { + h.Logger.Info("could not parse group_id as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() owners, err := h.Repository.GetOwners(ctx, id) if err != nil { + h.Logger.Error("failed to get owners from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } sess.Values[sessionOwnerKey] = owners if err = sess.Save(c.Request(), c.Response()); err != nil { + h.Logger.Error("failed to save session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -250,22 +266,26 @@ func (h Handlers) RetrieveRequestCreator() echo.MiddlewareFunc { return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } id, err := uuid.Parse(c.Param("requestID")) if err != nil { + h.Logger.Info("could not parse request_id as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() request, err := h.Repository.GetRequest(ctx, id) if err != nil { + h.Logger.Error("failed to get request from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } sess.Values[sessionRequestCreatorKey] = request.CreatedBy if err = sess.Save(c.Request(), c.Response()); err != nil { + h.Logger.Error("failed to save session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -279,22 +299,26 @@ func (h Handlers) RetrieveFileCreator() echo.MiddlewareFunc { return func(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } id, err := uuid.Parse(c.Param("fileID")) if err != nil { + h.Logger.Info("could not parse file_id as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() file, err := h.Repository.GetFile(ctx, id) if err != nil { + h.Logger.Error("failed to get file from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } sess.Values[sessionFileCreatorKey] = file.CreatedBy if err = sess.Save(c.Request(), c.Response()); err != nil { + h.Logger.Error("failed to save session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } diff --git a/router/request.go b/router/request.go index 6cfbb247..0ef214e4 100644 --- a/router/request.go +++ b/router/request.go @@ -12,6 +12,7 @@ import ( "github.com/traPtitech/Jomon/ent" "github.com/traPtitech/Jomon/model" "github.com/traPtitech/Jomon/service" + "go.uber.org/zap" ) type Status string @@ -132,6 +133,7 @@ func (h Handlers) GetRequests(c echo.Context) error { if c.QueryParam("target") != "" { t, err := uuid.Parse(c.QueryParam("target")) if err != nil { + h.Logger.Info("could not parse query parameter `target` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } target = &t @@ -140,6 +142,7 @@ func (h Handlers) GetRequests(c echo.Context) error { if c.QueryParam("since") != "" { s, err := service.StrToDate(c.QueryParam("since")) if err != nil { + h.Logger.Info("could not parse query parameter `since` as time.Time", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } since = &s @@ -148,6 +151,7 @@ func (h Handlers) GetRequests(c echo.Context) error { if c.QueryParam("until") != "" { u, err := service.StrToDate(c.QueryParam("until")) if err != nil { + h.Logger.Info("could not parse query parameter `until` as time.Time", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } until = &u @@ -174,6 +178,7 @@ func (h Handlers) GetRequests(c echo.Context) error { modelrequests, err := h.Repository.GetRequests(ctx, query) if err != nil { + h.Logger.Error("failed to get requests from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -243,8 +248,10 @@ func (h Handlers) PostRequest(c echo.Context) error { tag, err := h.Repository.GetTag(ctx, *tagID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find tag in repository", zap.String("ID", tagID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get tag from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } tags = append(tags, tag) @@ -261,16 +268,20 @@ func (h Handlers) PostRequest(c echo.Context) error { group, err = h.Repository.GetGroup(ctx, *req.Group) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find group in repository", zap.String("ID", req.Group.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get group from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } } request, err := h.Repository.CreateRequest(ctx, req.Title, req.Content, tags, targets, group, req.CreatedBy) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find request in repository", zap.String("ID", req.CreatedBy.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to create request in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } var resgroup *GroupOverview @@ -331,9 +342,11 @@ func (h Handlers) PostRequest(c echo.Context) error { func (h Handlers) GetRequest(c echo.Context) error { requestID, err := uuid.Parse(c.Param("requestID")) if err != nil { + h.Logger.Info("could not parse query parameter `requestID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if requestID == uuid.Nil { + h.Logger.Info("invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } @@ -341,12 +354,18 @@ func (h Handlers) GetRequest(c echo.Context) error { request, err := h.Repository.GetRequest(ctx, requestID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info( + "could not find request in repository", + zap.String("ID", requestID.String()), + zap.Error(err)) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get request from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } modelcomments, err := h.Repository.GetComments(ctx, requestID) if err != nil { + h.Logger.Error("failed to get comments from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } comments := []*CommentDetail{} @@ -421,13 +440,16 @@ func (h Handlers) PutRequest(c echo.Context) error { var err error requestID, err := uuid.Parse(c.Param("requestID")) if err != nil { + h.Logger.Info("could not parse query parameter `requestID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if requestID == uuid.Nil { + h.Logger.Info("invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid UUID")) } if err = c.Bind(&req); err != nil { + h.Logger.Info("failed to get request from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } tags := []*model.Tag{} @@ -435,8 +457,10 @@ func (h Handlers) PutRequest(c echo.Context) error { tag, err := h.Repository.GetTag(ctx, *tagID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find tag in repository", zap.String("ID", tagID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get tag from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } tags = append(tags, tag) @@ -453,20 +477,25 @@ func (h Handlers) PutRequest(c echo.Context) error { group, err = h.Repository.GetGroup(ctx, *req.Group) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find group in repository", zap.String("ID", req.Group.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get group from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } } request, err := h.Repository.UpdateRequest(ctx, requestID, req.Title, req.Content, tags, targets, group) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find request in repository", zap.String("ID", requestID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to update request in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } modelcomments, err := h.Repository.GetComments(ctx, requestID) if err != nil { + h.Logger.Error("failed to get comments from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } var comments []*CommentDetail @@ -539,23 +568,28 @@ func (h Handlers) PutRequest(c echo.Context) error { func (h Handlers) PostComment(c echo.Context) error { requestID, err := uuid.Parse(c.Param("requestID")) if err != nil { + h.Logger.Info("could not parse query parameter `requestID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if requestID == uuid.Nil { + h.Logger.Info("invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, err) } var req Comment if err := c.Bind(&req); err != nil { + h.Logger.Info("failed to get comment from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, ok := sess.Values[sessionUserKey].(User) if !ok { + h.Logger.Info("could not find use in session") return echo.NewHTTPError(http.StatusUnauthorized, errors.New("sessionUser not found")) } @@ -563,8 +597,10 @@ func (h Handlers) PostComment(c echo.Context) error { comment, err := h.Repository.CreateComment(ctx, req.Comment, requestID, user.ID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find request in repository", zap.String("ID", requestID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to create comment in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } res := &CommentDetail{ @@ -582,21 +618,26 @@ func (h Handlers) PutStatus(c echo.Context) error { var err error requestID, err := uuid.Parse(c.Param("requestID")) if err != nil { + h.Logger.Info("could not parse query parameter `requestID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if requestID == uuid.Nil { + h.Logger.Info("invalid UUID") return echo.NewHTTPError(http.StatusBadRequest, err) } sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, ok := sess.Values[sessionUserKey].(User) if !ok { + h.Logger.Info("could not find use in session") return echo.NewHTTPError(http.StatusForbidden, errors.New("sessionUser not found")) } if err = c.Bind(&req); err != nil { + h.Logger.Info("could not get status from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } @@ -604,8 +645,10 @@ func (h Handlers) PutStatus(c echo.Context) error { request, err := h.Repository.GetRequest(ctx, requestID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find request in repository", zap.String("ID", requestID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get request from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -622,17 +665,21 @@ func (h Handlers) PutStatus(c echo.Context) error { u, err := h.Repository.GetUserByID(ctx, user.ID) if err != nil { if ent.IsNotFound(err) { + h.Logger.Info("could not find user in repository", zap.String("ID", user.ID.String())) return echo.NewHTTPError(http.StatusNotFound, err) } + h.Logger.Error("failed to get user from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } if u.Admin { if !IsAbleAdminChangeState(req.Status, request.Status) { + h.Logger.Info("admin unable to change status") return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("admin unable to change %v to %v", request.Status.String(), req.Status.String())) } if req.Status == model.Submitted && request.Status == model.Accepted { targets, err := h.Repository.GetRequestTargets(ctx, requestID) if err != nil { + h.Logger.Error("failed to get request targets from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } var paid bool @@ -643,6 +690,7 @@ func (h Handlers) PutStatus(c echo.Context) error { } } if paid { + h.Logger.Info("someone already paid") return echo.NewHTTPError(http.StatusBadRequest, errors.New("someone already paid")) } } @@ -650,23 +698,27 @@ func (h Handlers) PutStatus(c echo.Context) error { if !u.Admin && user.ID == request.CreatedBy { if !IsAbleCreatorChangeStatus(req.Status, request.Status) { + h.Logger.Info("creator unable to change status") return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("creator unable to change %v to %v", request.Status.String(), req.Status.String())) } } if user.ID != request.CreatedBy && !u.Admin { + h.Logger.Info("use is not creator or admin") return echo.NewHTTPError(http.StatusForbidden) } // create status and comment: keep the two in this order created, err := h.Repository.CreateStatus(ctx, requestID, user.ID, req.Status) if err != nil { + h.Logger.Error("failed to create status in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } var resComment CommentDetail if req.Comment != "" { comment, err := h.Repository.CreateComment(ctx, req.Comment, request.ID, user.ID) if err != nil { + h.Logger.Error("failed to create comment in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } resComment = CommentDetail{ diff --git a/router/tag.go b/router/tag.go index 5e8751cb..34c88ee5 100644 --- a/router/tag.go +++ b/router/tag.go @@ -7,6 +7,7 @@ import ( "github.com/google/uuid" "github.com/labstack/echo/v4" + "go.uber.org/zap" ) type Tag struct { @@ -25,6 +26,7 @@ func (h Handlers) GetTags(c echo.Context) error { ctx := c.Request().Context() tags, err := h.Repository.GetTags(ctx) if err != nil { + h.Logger.Error("failed to get tags from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -44,12 +46,14 @@ func (h Handlers) GetTags(c echo.Context) error { func (h Handlers) PostTag(c echo.Context) error { var tag Tag if err := c.Bind(&tag); err != nil { + h.Logger.Info("could not get tag from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() created, err := h.Repository.CreateTag(ctx, tag.Name) if err != nil { + h.Logger.Error("failed to create tag in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -66,19 +70,23 @@ func (h Handlers) PostTag(c echo.Context) error { func (h Handlers) PutTag(c echo.Context) error { tagID, err := uuid.Parse(c.Param("tagID")) if err != nil { + h.Logger.Info("could not parse query parameter `tagID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if tagID == uuid.Nil { + h.Logger.Info("invalid tag ID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid tag ID")) } var req Tag if err := c.Bind(&req); err != nil { + h.Logger.Info("could not get tag from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() tag, err := h.Repository.UpdateTag(ctx, tagID, req.Name) if err != nil { + h.Logger.Error("failed to update tag in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -95,15 +103,18 @@ func (h Handlers) PutTag(c echo.Context) error { func (h Handlers) DeleteTag(c echo.Context) error { tagID, err := uuid.Parse(c.Param("tagID")) if err != nil { + h.Logger.Info("could not parse query parameter `tagID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } if tagID == uuid.Nil { + h.Logger.Info("invalid tag ID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, errors.New("invalid tag ID")) } ctx := c.Request().Context() err = h.Repository.DeleteTag(ctx, tagID) if err != nil { + h.Logger.Error("failed to delete tag in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } diff --git a/router/transaction.go b/router/transaction.go index 5f186c78..20c2d394 100644 --- a/router/transaction.go +++ b/router/transaction.go @@ -8,6 +8,7 @@ import ( "github.com/labstack/echo/v4" "github.com/traPtitech/Jomon/model" "github.com/traPtitech/Jomon/service" + "go.uber.org/zap" ) type Transaction struct { @@ -54,6 +55,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { var err error s, err := service.StrToDate(c.QueryParam("since")) if err != nil { + h.Logger.Info("could not parse since as time.Time", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } since = &s @@ -63,6 +65,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { var err error u, err := service.StrToDate(c.QueryParam("until")) if err != nil { + h.Logger.Info("could not parse until as time.Time", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } until = &u @@ -82,6 +85,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { var r uuid.UUID r, err := uuid.Parse(c.QueryParam("request")) if err != nil { + h.Logger.Info("could not parse request as uuid.UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } request = &r @@ -141,6 +145,7 @@ func (h Handlers) GetTransactions(c echo.Context) error { func (h Handlers) PostTransaction(c echo.Context) error { var tx *TransactionOverview if err := c.Bind(&tx); err != nil { + h.Logger.Info("could not get transaction overview from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } @@ -148,10 +153,12 @@ func (h Handlers) PostTransaction(c echo.Context) error { ctx := c.Request().Context() for _, target := range tx.Targets { if target == nil { + h.Logger.Info("target is nil") return echo.NewHTTPError(http.StatusBadRequest, "target is nil") } created, err := h.Repository.CreateTransaction(ctx, tx.Amount, *target, tx.Tags, tx.Group, tx.Request) if err != nil { + h.Logger.Error("failed to create transaction in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -194,12 +201,14 @@ func (h Handlers) PostTransaction(c echo.Context) error { func (h Handlers) GetTransaction(c echo.Context) error { txID, err := uuid.Parse(c.Param("transactionID")) if err != nil { + h.Logger.Info("could not parse query parameter `transactionID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() tx, err := h.Repository.GetTransaction(ctx, txID) if err != nil { + h.Logger.Error("failed to get transaction from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -240,17 +249,20 @@ func (h Handlers) GetTransaction(c echo.Context) error { func (h Handlers) PutTransaction(c echo.Context) error { txID, err := uuid.Parse(c.Param("transactionID")) if err != nil { + h.Logger.Info("could not parse query parameter `transactionID` as UUID", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } var tx *TransactionOverviewWithOneTarget if err := c.Bind(&tx); err != nil { + h.Logger.Info("could not get transaction overview with one target from request", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } ctx := c.Request().Context() updated, err := h.Repository.UpdateTransaction(ctx, txID, tx.Amount, tx.Target, tx.Tags, tx.Group, tx.Request) if err != nil { + h.Logger.Error("failed to update transaction in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } diff --git a/router/user.go b/router/user.go index e048319b..eb5095e5 100644 --- a/router/user.go +++ b/router/user.go @@ -7,6 +7,7 @@ import ( "github.com/google/uuid" "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" + "go.uber.org/zap" ) type User struct { @@ -22,6 +23,7 @@ type User struct { func (h Handlers) GetUsers(c echo.Context) error { users, err := h.Repository.GetUsers(c.Request().Context()) if err != nil { + h.Logger.Error("failed to get users from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -50,16 +52,19 @@ type PutUserRequest struct { func (h Handlers) UpdateUserInfo(c echo.Context) error { var newUser PutUserRequest if err := c.Bind(&newUser); err != nil { + h.Logger.Info("could not get user info from request", zap.Error(err)) return c.NoContent(http.StatusBadRequest) } user, err := h.Repository.GetUserByName(c.Request().Context(), newUser.Name) if err != nil { + h.Logger.Error("failed to get user from repository", zap.Error(err)) return echo.NewHTTPError(http.StatusBadRequest, err) } updated, err := h.Repository.UpdateUser(c.Request().Context(), user.ID, newUser.Name, newUser.DisplayName, newUser.Admin) if err != nil { + h.Logger.Error("failed to update user in repository", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } @@ -74,10 +79,12 @@ func (h Handlers) UpdateUserInfo(c echo.Context) error { func (h Handlers) GetMe(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { + h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } user, ok := sess.Values[sessionUserKey].(User) if !ok { + h.Logger.Error("failed to parse stored session as user info") return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user info") }