Skip to content

Commit

Permalink
change more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wkloucek committed Nov 5, 2021
1 parent 5ccf369 commit bc23bf8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
13 changes: 8 additions & 5 deletions internal/grpc/services/gateway/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ func (s *svc) openLocalResources(ctx context.Context, ri *storageprovider.Resour

provider, err := s.findAppProvider(ctx, ri, app)
if err != nil {
err = errors.Wrap(err, "gateway: error calling findAppProvider")

var st *rpc.Status
if _, ok := err.(errtypes.IsNotFound); ok {
st = status.NewNotFound(ctx, "app provider not found")
st = status.NewNotFound(ctx, err.Error())
} else {
st = status.NewInternal(ctx, err, "error searching for app provider")
st = status.NewInternal(ctx, errors.Wrap(err, "gateway: error calling findAppProvider"), "error searching for app provider")
}
return &providerpb.OpenInAppResponse{
Status: st,
Expand Down Expand Up @@ -250,7 +250,7 @@ func (s *svc) findAppProvider(ctx context.Context, ri *storageprovider.ResourceI

// we did not find a default provider
if res.Status.Code == rpc.Code_CODE_NOT_FOUND {
err := errtypes.NotFound(fmt.Sprintf("gateway: default app rovider for mime type:%s not found", ri.MimeType))
err := errtypes.NotFound(fmt.Sprintf("gateway: default app provider for mime type:%s not found", ri.MimeType))
return nil, err
}

Expand Down Expand Up @@ -293,7 +293,10 @@ func (s *svc) findAppProvider(ctx context.Context, ri *storageprovider.ResourceI
}
res.Providers = filteredProviders

// if we only have one app provider we verify that it matches the requested app name
if len(res.Providers) == 0 {
return nil, errtypes.NotFound(fmt.Sprintf("app '%s' not found", app))
}

if len(res.Providers) == 1 {
return res.Providers[0], nil
}
Expand Down
81 changes: 55 additions & 26 deletions internal/http/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package appprovider

import (
"context"
"encoding/json"
"net/http"
"path"
Expand All @@ -40,7 +39,6 @@ import (
"github.com/cs3org/reva/pkg/utils/resourceid"
ua "github.com/mileusna/useragent"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -177,6 +175,11 @@ func (s *svc) handleNew(w http.ResponseWriter, r *http.Request) {
return
}

if parentContainer.Info.Type != provider.ResourceType_RESOURCE_TYPE_CONTAINER {
writeError(w, r, appErrorInvalidParameter, "the parent container id does not point to a container", nil)
return
}

fileRef := &provider.Reference{
ResourceId: parentContainer.Info.Id,
Path: utils.MakeRelativePath(filename),
Expand Down Expand Up @@ -254,13 +257,27 @@ func (s *svc) handleNew(w http.ResponseWriter, r *http.Request) {
}

// Stat the newly created file
statRes, ref, err := statFileRef(ctx, *fileRef, client)
statRes, err := client.Stat(ctx, statFileReq)
if err != nil {
writeError(w, r, ref, "statting the create file failed", err)
writeError(w, r, appErrorServerError, "statting the created file failed", err)
return
}

if statRes.Status.Code != rpc.Code_CODE_OK {
writeError(w, r, appErrorServerError, "statting the created file failed", nil)
return
}

if statRes.Info.Type != provider.ResourceType_RESOURCE_TYPE_FILE {
writeError(w, r, appErrorInvalidParameter, "the given file id does not point to a file", nil)
return
}

js, err := json.Marshal(map[string]interface{}{"file_id": resourceid.OwnCloudResourceIDWrap(statRes.Id)})
js, err := json.Marshal(
map[string]interface{}{
"file_id": resourceid.OwnCloudResourceIDWrap(statRes.Info.Id),
},
)
if err != nil {
writeError(w, r, appErrorServerError, "error marshalling JSON response", err)
return
Expand Down Expand Up @@ -321,21 +338,44 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
return
}

res := resourceid.OwnCloudResourceIDUnwrap(fileID)
if res == nil {
resourceID := resourceid.OwnCloudResourceIDUnwrap(fileID)
if resourceID == nil {
writeError(w, r, appErrorInvalidParameter, "invalid file ID", nil)
return
}

info, errCode, err := statFileRef(ctx, provider.Reference{ResourceId: res}, client)
fileRef := &provider.Reference{
ResourceId: resourceID,
}

statRes, err := client.Stat(ctx, &provider.StatRequest{Ref: fileRef})
if err != nil {
writeError(w, r, errCode, "error statting file", err)
writeError(w, r, appErrorServerError, "statting the file failed", err)
return
}

if statRes.Status.Code == rpc.Code_CODE_NOT_FOUND {
writeError(w, r, appErrorNotFound, "file does not exist", nil)
return
} else if statRes.Status.Code != rpc.Code_CODE_OK {
writeError(w, r, appErrorServerError, "failed to stat the file", nil)
return
}

if statRes.Info.Type != provider.ResourceType_RESOURCE_TYPE_FILE {
writeError(w, r, appErrorInvalidParameter, "the given file id does not point to a file", nil)
return
}

viewMode := getViewMode(statRes.Info, r.URL.Query().Get("view_mode"))
if viewMode == gateway.OpenInAppRequest_VIEW_MODE_INVALID {
writeError(w, r, appErrorInvalidParameter, "invalid view mode", err)
return
}

openReq := gateway.OpenInAppRequest{
Ref: &provider.Reference{ResourceId: info.Id},
ViewMode: getViewMode(info, r.URL.Query().Get("view_mode")),
Ref: fileRef,
ViewMode: viewMode,
App: r.URL.Query().Get("app_name"),
}
openRes, err := client.OpenInApp(ctx, &openReq)
Expand All @@ -344,6 +384,10 @@ func (s *svc) handleOpen(w http.ResponseWriter, r *http.Request) {
return
}
if openRes.Status.Code != rpc.Code_CODE_OK {
if openRes.Status.Code == rpc.Code_CODE_NOT_FOUND {
writeError(w, r, appErrorNotFound, openRes.Status.Message, nil)
return
}
writeError(w, r, appErrorServerError, openRes.Status.Message,
status.NewErrorFromCode(openRes.Status.Code, "error calling OpenInApp"))
return
Expand Down Expand Up @@ -383,21 +427,6 @@ func filterAppsByUserAgent(mimeTypes []*appregistry.MimeTypeInfo, userAgent stri
return res
}

func statFileRef(ctx context.Context, ref provider.Reference, client gateway.GatewayAPIClient) (*provider.ResourceInfo, appErrorCode, error) {
statReq := provider.StatRequest{Ref: &ref}
statRes, err := client.Stat(ctx, &statReq)
if err != nil {
return nil, appErrorServerError, err
}
if statRes.Status.Code != rpc.Code_CODE_OK {
return nil, appErrorServerError, status.NewErrorFromCode(statRes.Status.Code, "appprovider")
}
if statRes.Info.Type != provider.ResourceType_RESOURCE_TYPE_FILE {
return nil, appErrorServerError, errors.New("unsupported resource type")
}
return statRes.Info, appErrorCode(""), nil
}

func getViewMode(res *provider.ResourceInfo, vm string) gateway.OpenInAppRequest_ViewMode {
if vm != "" {
return utils.GetViewMode(vm)
Expand Down

0 comments on commit bc23bf8

Please sign in to comment.