mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-07 04:39:25 -05:00
[server] Streamline errors
This commit is contained in:
@@ -2,7 +2,6 @@ package fs
|
||||
|
||||
import (
|
||||
"io"
|
||||
iofs "io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -14,14 +13,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
errInsufficientPermissions = errors.New(http.StatusBadRequest, "insufficient_permissions", "Insufficient Permissions")
|
||||
errResourceIDInvalid = errors.New(http.StatusBadRequest, "resource_id_invalid", "Invalid UUID")
|
||||
errResourceIDConflict = errors.New(http.StatusBadRequest, "resource_id_conflict", "ID already in use")
|
||||
errResourceNotFound = errors.New(http.StatusNotFound, "resource_not_found", "Resource Not Found")
|
||||
errResourceNotDirectory = errors.New(http.StatusMethodNotAllowed, "resource_not_directory", "Resource Not Directory")
|
||||
errInvalidParams = errors.New(http.StatusBadRequest, "invalid_parameters", "Invalid Request Parameters")
|
||||
errResourceNameConflict = errors.New(http.StatusConflict, "name_conflict", "Resource Name Conflict")
|
||||
errMoveTargetSubdirectory = errors.New(http.StatusBadRequest, "move_target_subdirectory", "Move Target Subdirectory")
|
||||
errResourceIDInvalid = errors.New(http.StatusBadRequest, "resource_id_invalid", "Invalid UUID")
|
||||
errInvalidParams = errors.New(http.StatusBadRequest, "invalid_parameters", "Invalid Request Parameters")
|
||||
)
|
||||
|
||||
type resourceMkdirParams struct {
|
||||
@@ -56,9 +49,6 @@ func handleResourceMetadataRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
resource, err := fs.ResourceByID(resourceId)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -74,17 +64,11 @@ func handleResourceLsRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
resource, err := fs.ResourceByID(resourceId)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
children, err := fs.ReadDir(resource)
|
||||
if err != nil {
|
||||
if errors.Is(err, core.ErrResourceNotCollection) {
|
||||
panic(errResourceNotDirectory)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
c.JSON(200, children)
|
||||
@@ -99,9 +83,6 @@ func handleResourceDiskUsageRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
resource, err := fs.ResourceByID(resourceId)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -125,9 +106,6 @@ func handleResourceDetailsRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
r, err := fs.ResourceByID(resourceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -143,9 +121,6 @@ func handleResourceContentsRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
r, err := fs.ResourceByID(resourceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
serve.ServeResource(c.Writer, c.Request, fs, r)
|
||||
@@ -166,26 +141,11 @@ func handleResourceMkdirRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
parent, err := fs.ResourceByID(params.ParentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
r, err := fs.CreateMemberResource(parent, resourceID, params.Name, true)
|
||||
if err != nil {
|
||||
if errors.Is(err, core.ErrInsufficientPermissions) {
|
||||
panic(errInsufficientPermissions)
|
||||
}
|
||||
if errors.Is(err, core.ErrResourceNotCollection) {
|
||||
panic(errResourceNotDirectory)
|
||||
}
|
||||
if errors.Is(err, core.ErrResourceNameConflict) {
|
||||
panic(errResourceNameConflict)
|
||||
}
|
||||
if errors.Is(err, core.ErrResourceIDConflict) {
|
||||
panic(errResourceIDConflict)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -207,22 +167,10 @@ func handleResourceMoveRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
r, err := fs.ResourceByID(resourceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
panic(errResourceNotFound)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if r, err := fs.Move(r, params.Name, params.ParentID); err != nil {
|
||||
if errors.Is(err, core.ErrInsufficientPermissions) {
|
||||
panic(errInsufficientPermissions)
|
||||
}
|
||||
if errors.Is(err, core.ErrResourceNameConflict) {
|
||||
panic(errResourceNameConflict)
|
||||
}
|
||||
if errors.Is(err, core.ErrMoveTargetSubdirectory) {
|
||||
panic(errMoveTargetSubdirectory)
|
||||
}
|
||||
panic(err)
|
||||
} else {
|
||||
c.JSON(200, r)
|
||||
@@ -247,26 +195,11 @@ func handleResourceUploadRoute(c *gin.Context) {
|
||||
err = f.RunInTx(func(fs core.FileSystem) error {
|
||||
parent, err := fs.ResourceByID(parentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
return errResourceNotFound
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := fs.CreateMemberResource(parent, resourceID, name, false)
|
||||
if err != nil {
|
||||
if errors.Is(err, core.ErrInsufficientPermissions) {
|
||||
panic(errInsufficientPermissions)
|
||||
}
|
||||
if errors.Is(err, core.ErrResourceNotCollection) {
|
||||
panic(errResourceNotDirectory)
|
||||
}
|
||||
if errors.Is(err, core.ErrResourceNameConflict) {
|
||||
panic(errResourceNameConflict)
|
||||
}
|
||||
if errors.Is(err, core.ErrResourceIDConflict) {
|
||||
panic(errResourceIDConflict)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -314,9 +247,6 @@ func handleResourceDeleteRoute(c *gin.Context) {
|
||||
fs := auth.GetFileSystem(c)
|
||||
r, err := fs.ResourceByID(id)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
err = errResourceNotFound
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
ids, err := fs.DeleteRecursive(r, false)
|
||||
|
||||
@@ -27,9 +27,6 @@ func handleMyHomeRoute(c *gin.Context) {
|
||||
f := auth.GetFileSystem(c)
|
||||
r, err := f.ResourceByID(user.Home)
|
||||
if err != nil {
|
||||
// if errors.Is(err, iofs.ErrNotExist) {
|
||||
// err = errResourceNotFound
|
||||
// }
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
iofs "io/fs"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -79,7 +78,7 @@ func setupResourceRmCommand() *cobra.Command {
|
||||
path := args[0]
|
||||
r, err := fs.ResourceByPath(path)
|
||||
if err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
if errors.Is(err, core.ErrResourceNotFound) {
|
||||
logrus.Fatal("Resource not found: " + path)
|
||||
} else {
|
||||
logrus.Fatal(err.Error())
|
||||
@@ -124,7 +123,7 @@ func setupResourceLsCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
if r, err = fs.ResourceByID(id); err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
if errors.Is(err, core.ErrResourceNotFound) {
|
||||
logrus.Fatal("Resource not found: " + path)
|
||||
} else {
|
||||
logrus.Fatal(err.Error())
|
||||
@@ -133,7 +132,7 @@ func setupResourceLsCommand() *cobra.Command {
|
||||
} else {
|
||||
var err error
|
||||
if r, err = fs.ResourceByPath(path); err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
if errors.Is(err, core.ErrResourceNotFound) {
|
||||
logrus.Fatal("Resource not found: " + path)
|
||||
} else {
|
||||
logrus.Fatal(err.Error())
|
||||
@@ -187,7 +186,7 @@ func setupResourceChpermCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
if r, err = fs.ResourceByID(id); err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
if errors.Is(err, core.ErrResourceNotFound) {
|
||||
logrus.Fatal("Resource not found: " + path)
|
||||
} else {
|
||||
logrus.Fatal(err.Error())
|
||||
@@ -196,7 +195,7 @@ func setupResourceChpermCommand() *cobra.Command {
|
||||
} else {
|
||||
var err error
|
||||
if r, err = fs.ResourceByPath(path); err != nil {
|
||||
if errors.Is(err, iofs.ErrNotExist) {
|
||||
if errors.Is(err, core.ErrResourceNotFound) {
|
||||
logrus.Fatal("Resource not found: " + path)
|
||||
} else {
|
||||
logrus.Fatal(err.Error())
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/api/errors"
|
||||
)
|
||||
|
||||
var ErrInsufficientPermissions = errors.New(http.StatusUnauthorized, "insufficient_permissions", "")
|
||||
|
||||
var ErrUserNotFound = errors.New(http.StatusNotFound, "user_not_found", "")
|
||||
|
||||
var (
|
||||
ErrResourceNotFound = errors.New(http.StatusNotFound, "resource_not_found", "")
|
||||
ErrResourceNotCollection = errors.New(http.StatusMethodNotAllowed, "cannot add member to non-collection resource", "")
|
||||
ErrResourceCollection = errors.New(http.StatusMethodNotAllowed, "cannot write to collection resource", "")
|
||||
ErrResourceIDConflict = errors.New(http.StatusConflict, "resource_id_conflict", "")
|
||||
ErrResourceNameInvalid = errors.New(http.StatusBadRequest, "resource_name_invalid", "")
|
||||
ErrResourceNameConflict = errors.New(http.StatusConflict, "resource_name_conflict", "")
|
||||
ErrResourceMoveTargetSubdirectory = errors.New(http.StatusConflict, "move target subdirectory", "")
|
||||
)
|
||||
@@ -3,9 +3,7 @@ package core
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -17,17 +15,6 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInsufficientPermissions = errors.New("insufficient permissions")
|
||||
ErrCannotGrantOwnerPermission = errors.New("cannot grant owner permission")
|
||||
ErrResourceNotCollection = errors.New("cannot add member to non-collection resource")
|
||||
ErrIDNotSpecified = errors.New("resource id not specified")
|
||||
ErrNameInvalid = errors.New("name invalid")
|
||||
ErrResourceNameConflict = errors.New("name conflict")
|
||||
ErrResourceIDConflict = errors.New("id conflict")
|
||||
ErrMoveTargetSubdirectory = errors.New("move target subdirectory")
|
||||
)
|
||||
|
||||
type DiskUsageInfo struct {
|
||||
TotalSize int64
|
||||
Entities int64
|
||||
@@ -99,7 +86,7 @@ func (f filesystem) ResourceByPath(path string) (Resource, error) {
|
||||
|
||||
res, err := f.db.ResourceByPath(f.ctx, db.ResourceByPathParams{Root: f.rootID, Search: segments})
|
||||
if err == pgx.ErrNoRows {
|
||||
err = fs.ErrNotExist
|
||||
err = ErrResourceNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return Resource{}, err
|
||||
@@ -111,7 +98,7 @@ func (f filesystem) ResourceByID(id uuid.UUID) (Resource, error) {
|
||||
res, err := f.db.ResourceByID(f.ctx, db.ResourceByIDParams{Root: f.rootID, ResourceID: id, Username: f.username})
|
||||
// TODO: verify found
|
||||
if err == pgx.ErrNoRows || !res.Found || res.UserPermission == 0 {
|
||||
err = fs.ErrNotExist
|
||||
err = ErrResourceNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return Resource{}, err
|
||||
@@ -199,11 +186,8 @@ func (f filesystem) CreateMemberResource(r Resource, id uuid.UUID, name string,
|
||||
if r.UserPermissions < PermissionReadWrite {
|
||||
return Resource{}, ErrInsufficientPermissions
|
||||
}
|
||||
if id == uuid.Nil {
|
||||
return Resource{}, ErrIDNotSpecified
|
||||
}
|
||||
if name == "" || checkNameInvalid(name) {
|
||||
return Resource{}, ErrNameInvalid
|
||||
return Resource{}, ErrResourceNameInvalid
|
||||
}
|
||||
var result db.Resource
|
||||
err := f.db.WithTx(f.ctx, func(d *db.DbHandler) error {
|
||||
@@ -224,17 +208,17 @@ func (f filesystem) CreateMemberResource(r Resource, id uuid.UUID, name string,
|
||||
return Resource{}, err
|
||||
}
|
||||
return Resource{
|
||||
ID: id,
|
||||
ParentID: result.Parent,
|
||||
Name: result.Name,
|
||||
Dir: dir,
|
||||
Size: 0,
|
||||
Modified: result.Modified.Time,
|
||||
Deleted: nil,
|
||||
ETag: "",
|
||||
Permissions: string(result.Permissions),
|
||||
ID: id,
|
||||
ParentID: result.Parent,
|
||||
Name: result.Name,
|
||||
Dir: dir,
|
||||
Size: 0,
|
||||
Modified: result.Modified.Time,
|
||||
Deleted: nil,
|
||||
ETag: "",
|
||||
Permissions: string(result.Permissions),
|
||||
UserPermissions: r.UserPermissions,
|
||||
// Not Needed
|
||||
// UserPermissions: r.UserPermissions,
|
||||
// InheritedPermissions: "",
|
||||
}, nil
|
||||
}
|
||||
@@ -305,7 +289,7 @@ func (f filesystem) Move(r Resource, name string, parent *uuid.UUID) (Resource,
|
||||
name = ""
|
||||
}
|
||||
if checkNameInvalid(name) {
|
||||
return Resource{}, ErrNameInvalid
|
||||
return Resource{}, ErrResourceNameInvalid
|
||||
}
|
||||
if parent == r.ParentID {
|
||||
parent = nil
|
||||
@@ -331,7 +315,7 @@ func (f filesystem) Move(r Resource, name string, parent *uuid.UUID) (Resource,
|
||||
}
|
||||
|
||||
if res, err := f.db.ResourceByID(f.ctx, db.ResourceByIDParams{Root: r.ID, ResourceID: *parent, Username: f.username}); err != nil || res.Found {
|
||||
return Resource{}, ErrMoveTargetSubdirectory
|
||||
return Resource{}, ErrResourceMoveTargetSubdirectory
|
||||
}
|
||||
}
|
||||
if r, err := f.db.UpdateResourceNameParent(f.ctx, db.UpdateResourceNameParentParams{ID: r.ID, Name: name, Parent: parent}); err != nil {
|
||||
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/db"
|
||||
)
|
||||
|
||||
var ErrUserNotFound = errors.New("user not found")
|
||||
|
||||
func (a App) CreateUser(ctx context.Context, username, displayName, password string, root *uuid.UUID) error {
|
||||
var rootID = a.Rootfs.RootID()
|
||||
if root != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package webdav
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
@@ -75,14 +74,14 @@ func (a adapter) OpenWrite(ctx context.Context, name string) (io.WriteCloser, er
|
||||
resource, err := a.Stat(ctx, name)
|
||||
|
||||
if err != nil {
|
||||
if err == fs.ErrNotExist {
|
||||
if err == core.ErrResourceNotFound {
|
||||
// Try to create the resource if the parent collection exists
|
||||
name = strings.TrimRight(name, "/")
|
||||
index := strings.LastIndex(name, "/")
|
||||
parentPath := name[0:index]
|
||||
parent, err := a.Stat(ctx, parentPath)
|
||||
if err != nil {
|
||||
return nil, fs.ErrNotExist
|
||||
return nil, err
|
||||
}
|
||||
resourceName := name[index+1:]
|
||||
resourceId := uuid.New()
|
||||
@@ -95,7 +94,7 @@ func (a adapter) OpenWrite(ctx context.Context, name string) (io.WriteCloser, er
|
||||
return nil, err
|
||||
}
|
||||
if resource.Dir {
|
||||
return nil, errors.New("cannot open collection for write")
|
||||
return nil, core.ErrResourceCollection
|
||||
}
|
||||
return a.fs.OpenWrite(resource)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user