enhancement: use error codes in the drive item api

This commit is contained in:
Florian Schade
2024-04-24 11:59:04 +02:00
committed by Ralf Haferkamp
parent 4841240bff
commit 5b4e83c9bf
2 changed files with 55 additions and 58 deletions

View File

@@ -33,41 +33,37 @@ var (
// ErrNoUpdater is returned when no updater is provided
ErrNoUpdater = errors.New("no updater")
// ErrNoShares is returned when no shares are found
ErrNoShares = errors.New("no shares found")
// ErrNoShare is returned when no share is found
ErrNoShare = errors.New("no shares found")
// ErrAbsoluteNamePath is returned when the name is an absolute path
ErrAbsoluteNamePath = errors.New("name cannot be an absolute path")
// ErrCode errors
// ErrNotAShareJail is returned when the driveID does not belong to a share jail
ErrNotAShareJail = errors.New("id does not belong to a share jail")
ErrNotAShareJail = errorcode.New(errorcode.InvalidRequest, "id does not belong to a share jail")
// ErrInvalidDriveIDOrItemID is returned when the driveID or itemID is invalid
ErrInvalidDriveIDOrItemID = errors.New("invalid driveID or itemID")
ErrInvalidDriveIDOrItemID = errorcode.New(errorcode.InvalidRequest, "invalid driveID or itemID")
// ErrInvalidRequestBody is returned when the request body is invalid
ErrInvalidRequestBody = errors.New("invalid request body")
ErrInvalidRequestBody = errorcode.New(errorcode.InvalidRequest, "invalid request body")
// ErrUnmountShare is returned when unmounting a share fails
ErrUnmountShare = errors.New("unmounting share failed")
ErrUnmountShare = errorcode.New(errorcode.InvalidRequest, "unmounting share failed")
// ErrMountShare is returned when mounting a share fails
ErrMountShare = errors.New("mounting share failed")
// ErrGetShareAndSiblings is returned when getting the share and siblings fails
ErrGetShareAndSiblings = errors.New("failed to get share and siblings")
ErrMountShare = errorcode.New(errorcode.InvalidRequest, "mounting share failed")
// ErrUpdateShares is returned when updating shares fails
ErrUpdateShares = errors.New("failed to update share")
ErrUpdateShares = errorcode.New(errorcode.InvalidRequest, "failed to update share")
// ErrInvalidID is returned when the id is invalid
ErrInvalidID = errors.New("invalid id")
ErrInvalidID = errorcode.New(errorcode.InvalidRequest, "invalid id")
// ErrDriveItemConversion is returned when converting to drive items fails
ErrDriveItemConversion = errors.New("converting to drive items failed")
ErrDriveItemConversion = errorcode.New(errorcode.InvalidRequest, "converting to drive items failed")
// ErrNoShares is returned when no shares are found
ErrNoShares = errorcode.New(errorcode.ItemNotFound, "no shares found")
)
type (
@@ -320,20 +316,20 @@ func (api DrivesDriveItemApi) DeleteDriveItem(w http.ResponseWriter, r *http.Req
driveID, itemID, err := GetDriveAndItemIDParam(r, &api.logger)
if err != nil {
api.logger.Debug().Err(err).Msg(ErrInvalidDriveIDOrItemID.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrInvalidDriveIDOrItemID.Error())
ErrInvalidDriveIDOrItemID.Render(w, r)
return
}
if !IsShareJail(driveID) {
api.logger.Debug().Interface("driveID", driveID).Msg(ErrNotAShareJail.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrNotAShareJail.Error())
ErrNotAShareJail.Render(w, r)
return
}
shareID := ExtractShareIdFromResourceId(itemID)
if err := api.drivesDriveItemService.UnmountShare(ctx, shareID); err != nil {
api.logger.Debug().Err(err).Msg(ErrUnmountShare.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusFailedDependency, ErrUnmountShare.Error())
ErrUnmountShare.Render(w, r)
return
}
@@ -346,13 +342,13 @@ func (api DrivesDriveItemApi) UpdateDriveItem(w http.ResponseWriter, r *http.Req
driveID, itemID, err := GetDriveAndItemIDParam(r, &api.logger)
if err != nil {
api.logger.Debug().Err(err).Msg(ErrInvalidDriveIDOrItemID.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrInvalidDriveIDOrItemID.Error())
ErrInvalidDriveIDOrItemID.Render(w, r)
return
}
if !IsShareJail(driveID) {
api.logger.Debug().Interface("driveID", driveID).Msg(ErrNotAShareJail.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrNotAShareJail.Error())
ErrNotAShareJail.Render(w, r)
return
}
@@ -360,21 +356,21 @@ func (api DrivesDriveItemApi) UpdateDriveItem(w http.ResponseWriter, r *http.Req
requestDriveItem := libregraph.DriveItem{}
if err := StrictJSONUnmarshal(r.Body, &requestDriveItem); err != nil {
api.logger.Debug().Err(err).Msg(ErrInvalidRequestBody.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrInvalidRequestBody.Error())
ErrInvalidRequestBody.Render(w, r)
return
}
share, err := api.drivesDriveItemService.GetShare(r.Context(), shareID)
if err != nil {
api.logger.Debug().Err(err).Msg(ErrNoShare.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusFailedDependency, ErrNoShare.Error())
api.logger.Debug().Err(err).Msg(ErrNoShares.Error())
ErrNoShares.Render(w, r)
return
}
availableShares, err := api.drivesDriveItemService.GetShares(r.Context(), share.GetShare().GetResourceId(), nil)
if err != nil {
api.logger.Debug().Err(err).Msg(ErrGetShareAndSiblings.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusFailedDependency, ErrGetShareAndSiblings.Error())
api.logger.Debug().Err(err).Msg(ErrNoShares.Error())
ErrNoShares.Render(w, r)
return
}
@@ -394,7 +390,7 @@ func (api DrivesDriveItemApi) UpdateDriveItem(w http.ResponseWriter, r *http.Req
}
if err != nil {
api.logger.Debug().Err(err).Msg(ErrUpdateShares.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusFailedDependency, ErrUpdateShares.Error())
ErrUpdateShares.Render(w, r)
return
}
@@ -408,28 +404,29 @@ func (api DrivesDriveItemApi) CreateDriveItem(w http.ResponseWriter, r *http.Req
driveID, err := parseIDParam(r, "driveID")
if err != nil {
api.logger.Debug().Err(err).Msg(ErrInvalidDriveIDOrItemID.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrInvalidDriveIDOrItemID.Error())
ErrInvalidDriveIDOrItemID.Render(w, r)
return
}
if !IsShareJail(driveID) {
api.logger.Debug().Interface("driveID", driveID).Msg(ErrNotAShareJail.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrNotAShareJail.Error())
ErrNotAShareJail.Render(w, r)
return
}
requestDriveItem := libregraph.DriveItem{}
if err := StrictJSONUnmarshal(r.Body, &requestDriveItem); err != nil {
api.logger.Debug().Err(err).Msg(ErrInvalidRequestBody.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusUnprocessableEntity, ErrInvalidRequestBody.Error())
ErrInvalidRequestBody.Render(w, r)
return
}
remoteItem := requestDriveItem.GetRemoteItem()
resourceId, err := storagespace.ParseID(remoteItem.GetId())
if err != nil {
api.logger.Debug().Err(err).Msg(ErrInvalidID.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, ErrInvalidID.Error())
ErrInvalidID.Render(w, r)
return
}
@@ -437,7 +434,7 @@ func (api DrivesDriveItemApi) CreateDriveItem(w http.ResponseWriter, r *http.Req
MountShare(ctx, &resourceId, requestDriveItem.GetName())
if err != nil {
api.logger.Debug().Err(err).Msg(ErrMountShare.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, ErrMountShare.Error())
ErrMountShare.Render(w, r)
return
}
@@ -450,7 +447,7 @@ func (api DrivesDriveItemApi) CreateDriveItem(w http.ResponseWriter, r *http.Req
}
if err != nil {
api.logger.Debug().Err(err).Msg(ErrDriveItemConversion.Error())
errorcode.InvalidRequest.Render(w, r, http.StatusFailedDependency, ErrDriveItemConversion.Error())
ErrDriveItemConversion.Render(w, r)
return
}

View File

@@ -470,10 +470,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
context.WithValue(context.Background(), chi.RouteCtxKey, rCTX),
)
handler(w, r)
Expect(w.Code).To(Equal(http.StatusUnprocessableEntity))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrInvalidDriveIDOrItemID.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrInvalidDriveIDOrItemID.Error()))
})
}
@@ -488,10 +488,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
context.WithValue(context.Background(), chi.RouteCtxKey, rCTX),
)
handler(w, r)
Expect(w.Code).To(Equal(http.StatusUnprocessableEntity))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrInvalidRequestBody.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrInvalidRequestBody.Error()))
})
}
@@ -505,10 +505,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
context.WithValue(context.Background(), chi.RouteCtxKey, rCTX),
)
handler(w, r)
Expect(w.Code).To(Equal(http.StatusUnprocessableEntity))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrNotAShareJail.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrNotAShareJail.Error()))
})
}
@@ -533,10 +533,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
Once()
drivesDriveItemApi.DeleteDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusFailedDependency))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrUnmountShare.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrUnmountShare.Error()))
})
It("successfully unmounts the share", func() {
@@ -587,10 +587,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
Once()
drivesDriveItemApi.UpdateDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusFailedDependency))
Expect(w.Code).To(Equal(http.StatusNotFound))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrNoShare.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrNoShares.Error()))
})
It("fails if retrieving the shares fail", func() {
@@ -620,10 +620,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
Once()
drivesDriveItemApi.UpdateDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusFailedDependency))
Expect(w.Code).To(Equal(http.StatusNotFound))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrGetShareAndSiblings.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrNoShares.Error()))
})
It("fails if updating the share fails", func() {
@@ -659,10 +659,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
Once()
drivesDriveItemApi.UpdateDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusFailedDependency))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrUpdateShares.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrUpdateShares.Error()))
})
It("fails if no shares are updated", func() {
@@ -700,10 +700,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
Once()
drivesDriveItemApi.UpdateDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusFailedDependency))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrUpdateShares.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrUpdateShares.Error()))
})
It("successfully updates the share", func() {
@@ -778,10 +778,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
context.WithValue(context.Background(), chi.RouteCtxKey, rCTX),
)
drivesDriveItemApi.CreateDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusUnprocessableEntity))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrInvalidDriveIDOrItemID.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrInvalidDriveIDOrItemID.Error()))
})
failOnNonShareJailDriveID(drivesDriveItemApi.CreateDriveItem)
@@ -806,7 +806,7 @@ var _ = Describe("DrivesDriveItemApi", func() {
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrInvalidID.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrInvalidID.Error()))
})
It("fails if mounting the share fails", func() {
@@ -837,7 +837,7 @@ var _ = Describe("DrivesDriveItemApi", func() {
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrMountShare.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrMountShare.Error()))
})
It("fails if drive item conversion fails", func() {
@@ -870,10 +870,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
)
drivesDriveItemApi.CreateDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusFailedDependency))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData := gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrDriveItemConversion.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrDriveItemConversion.Error()))
//
baseGraphProvider.
@@ -888,10 +888,10 @@ var _ = Describe("DrivesDriveItemApi", func() {
)
drivesDriveItemApi.CreateDriveItem(w, r)
Expect(w.Code).To(Equal(http.StatusFailedDependency))
Expect(w.Code).To(Equal(http.StatusBadRequest))
jsonData = gjson.Get(w.Body.String(), "error")
Expect(jsonData.Get("message").String()).To(Equal(svc.ErrDriveItemConversion.Error()))
Expect(jsonData.Get("code").String() + ": " + jsonData.Get("message").String()).To(Equal(svc.ErrDriveItemConversion.Error()))
})
It("successfully creates the drive item", func() {