Skip to content

Commit

Permalink
wrapping errors properly
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsgrill committed Oct 26, 2024
1 parent 8505410 commit f487289
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 49 deletions.
31 changes: 14 additions & 17 deletions bindings/proxy/client/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type file struct {

// Close implements afero.File.
func (f *file) Close() error {
return f.fs.client.Fclose(context.Background(), f.handle)
return eurap("close", f.fs.client.Fclose(context.Background(), f.handle))
}

// Name implements afero.File.
Expand All @@ -27,10 +27,7 @@ func (f *file) Name() string {
// Read implements afero.File.
func (f *file) Read(p []byte) (n int, err error) {
data, err := f.fs.client.Fread(context.Background(), f.handle, int64(len(p)))
if err != nil {
return 0, err
}
return copy(p, data), nil
return copy(p, data), eurap("read", err)
}

// ReadAt implements afero.File.
Expand All @@ -42,59 +39,59 @@ func (f *file) ReadAt(p []byte, off int64) (n int, err error) {
// Readdir implements afero.File.
func (f *file) Readdir(count int) ([]fs.FileInfo, error) {
dirs, err := f.fs.client.Freaddir(context.Background(), f.handle, int32(count))
if err != nil {
return nil, err
}

fileInfos := make([]fs.FileInfo, len(dirs))
for i, dir := range dirs {
fileInfos[i] = dir
}

return fileInfos, nil
return fileInfos, eurap("readdir", err)
}

// Readdirnames implements afero.File.
func (f *file) Readdirnames(n int) ([]string, error) {
return f.fs.client.Freaddirnames(context.Background(), f.handle, int32(n))
names, err := f.fs.client.Freaddirnames(context.Background(), f.handle, int32(n))
return names, eurap("readdirnames", err)
}

// Seek implements afero.File.
func (f *file) Seek(offset int64, whence int) (int64, error) {
return f.fs.client.Fseek(context.Background(), f.handle, offset, int32(whence))
pos, err := f.fs.client.Fseek(context.Background(), f.handle, offset, int32(whence))
return pos, eurap("seek", err)
}

// Stat implements afero.File.
func (f *file) Stat() (fs.FileInfo, error) {
return f.fs.client.Fstat(context.Background(), f.handle)
info, err := f.fs.client.Fstat(context.Background(), f.handle)
return info, eurap("stat", err)
}

// Sync implements afero.File.
func (f *file) Sync() error {
return f.fs.client.Fsync(context.Background(), f.handle)
return eurap("sync", f.fs.client.Fsync(context.Background(), f.handle))
}

// Truncate implements afero.File.
func (f *file) Truncate(size int64) error {
return f.fs.client.Ftruncate(context.Background(), f.handle, size)
return eurap("truncate", f.fs.client.Ftruncate(context.Background(), f.handle, size))
}

// Write implements afero.File.
func (f *file) Write(p []byte) (n int, err error) {
r, err := f.fs.client.Fwrite(context.Background(), f.handle, p)
return int(r), err
return int(r), eurap("write", err)
}

// WriteAt implements afero.File.
func (f *file) WriteAt(p []byte, off int64) (n int, err error) {
r, err := f.fs.client.FwriteAt(context.Background(), f.handle, p, off)
return int(r), err
return int(r), eurap("writeat", err)
}

// WriteString implements afero.File.
func (f *file) WriteString(s string) (ret int, err error) {
r, err := f.fs.client.FwriteString(context.Background(), f.handle, s)
return int(r), err
return int(r), eurap("writestring", err)
}

func toFile(fs *filesystemClient, handle proxy.FileHandle) afero.File {
Expand Down
22 changes: 11 additions & 11 deletions bindings/proxy/client/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,36 @@ func New(fs proxy.Filesystem) afero.Fs {

// Chmod implements afero.Fs.
func (f *filesystemClient) Chmod(name string, mode fs.FileMode) error {
return f.client.Chmod(context.Background(), name, proxy.FileMode(mode))
return eurap("chmod", f.client.Chmod(context.Background(), name, proxy.FileMode(mode)))
}

// Chown implements afero.Fs.
func (f *filesystemClient) Chown(name string, uid int, gid int) error {
return f.client.Chown(context.Background(), name, int32(uid), int32(gid))
return eurap("chown", f.client.Chown(context.Background(), name, int32(uid), int32(gid)))
}

// Chtimes implements afero.Fs.
func (f *filesystemClient) Chtimes(name string, atime time.Time, mtime time.Time) error {
return f.client.Chtimes(context.Background(), name, proxy.Timestamp(atime.UnixMicro()), proxy.Timestamp(mtime.UnixMicro()))
return eurap("chtimes", f.client.Chtimes(context.Background(), name, proxy.Timestamp(atime.UnixMicro()), proxy.Timestamp(mtime.UnixMicro())))
}

// Create implements afero.Fs.
func (f *filesystemClient) Create(name string) (afero.File, error) {
h, err := f.client.Create(context.Background(), name)
if err != nil {
return nil, err
return nil, eurap("create", err)
}
return toFile(f, h), nil
}

// Mkdir implements afero.Fs.
func (f *filesystemClient) Mkdir(name string, perm fs.FileMode) error {
return f.client.Mkdir(context.Background(), name, proxy.FileMode(perm))
return eurap("mkdir", f.client.Mkdir(context.Background(), name, proxy.FileMode(perm)))
}

// MkdirAll implements afero.Fs.
func (f *filesystemClient) MkdirAll(path string, perm fs.FileMode) error {
return f.client.MkdirAll(context.Background(), path, proxy.FileMode(perm))
return eurap("mkdirall", f.client.MkdirAll(context.Background(), path, proxy.FileMode(perm)))
}

// Name implements afero.Fs.
Expand All @@ -66,7 +66,7 @@ func (f *filesystemClient) Name() string {
func (f *filesystemClient) Open(name string) (afero.File, error) {
h, err := f.client.Open(context.Background(), name)
if err != nil {
return nil, err
return nil, eurap("open", err)
}
return toFile(f, h), nil
}
Expand All @@ -75,24 +75,24 @@ func (f *filesystemClient) Open(name string) (afero.File, error) {
func (f *filesystemClient) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error) {
h, err := f.client.OpenFile(context.Background(), name, int32(flag), proxy.FileMode(perm))
if err != nil {
return nil, err
return nil, eurap("openfile", err)
}
return toFile(f, h), nil
}

// Remove implements afero.Fs.
func (f *filesystemClient) Remove(name string) error {
return f.client.Remove(context.Background(), name)
return eurap("remove", f.client.Remove(context.Background(), name))
}

// RemoveAll implements afero.Fs.
func (f *filesystemClient) RemoveAll(path string) error {
return f.client.RemoveAll(context.Background(), path)
return eurap("removeall", f.client.RemoveAll(context.Background(), path))
}

// Rename implements afero.Fs.
func (f *filesystemClient) Rename(oldname string, newname string) error {
return f.client.Rename(context.Background(), oldname, newname)
return eurap("rename", f.client.Rename(context.Background(), oldname, newname))
}

// Stat implements afero.Fs.
Expand Down
24 changes: 13 additions & 11 deletions bindings/proxy/server/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (fs *FilesystemServer) Fclose(ctx context.Context, file proxy.FileHandle) (
}
err := f.Close()
if err != nil {
return err
return ewrap(err)
}
delete(fs.openfiles, file)
return nil
Expand All @@ -35,7 +35,7 @@ func (fs *FilesystemServer) Fread(ctx context.Context, file proxy.FileHandle, bu
}
buffer := make([]byte, bufferSize)
n, err := f.Read(buffer)
return buffer[:n], err
return buffer[:n], ewrap(err)
}

func (fs *FilesystemServer) FreadAt(ctx context.Context, file proxy.FileHandle, bufferSize int64, offset int64) (_r []byte, _err error) {
Expand All @@ -61,23 +61,25 @@ func (fs *FilesystemServer) Freaddir(ctx context.Context, file proxy.FileHandle,
wrappedInfos[i] = wrapFileInfo(info)
}

return wrappedInfos, err
return wrappedInfos, ewrap(err)
}

func (fs *FilesystemServer) Freaddirnames(ctx context.Context, file proxy.FileHandle, count int32) (_r []string, _err error) {
f, opened := fs.openfiles[file]
if !opened {
return nil, os.ErrInvalid
}
return f.Readdirnames(int(count))
names, err := f.Readdirnames(int(count))
return names, ewrap(err)
}

func (fs *FilesystemServer) Fseek(ctx context.Context, file proxy.FileHandle, offset int64, whence int32) (_r int64, _err error) {
f, opened := fs.openfiles[file]
if !opened {
return 0, os.ErrInvalid
}
return f.Seek(offset, int(whence))
pos, err := f.Seek(offset, int(whence))
return pos, ewrap(err)
}

func (fs *FilesystemServer) Fstat(ctx context.Context, file proxy.FileHandle) (_r *proxy.FileInfo, _err error) {
Expand All @@ -87,7 +89,7 @@ func (fs *FilesystemServer) Fstat(ctx context.Context, file proxy.FileHandle) (_
}
info, err := f.Stat()
if err != nil {
return nil, err
return nil, ewrap(err)
}
return wrapFileInfo(info), nil
}
Expand All @@ -97,15 +99,15 @@ func (fs *FilesystemServer) Fsync(ctx context.Context, file proxy.FileHandle) (_
if !opened {
return os.ErrInvalid
}
return f.Sync()
return ewrap(f.Sync())
}

func (fs *FilesystemServer) Ftruncate(ctx context.Context, file proxy.FileHandle, size int64) (_err error) {
f, opened := fs.openfiles[file]
if !opened {
return os.ErrInvalid
}
return f.Truncate(size)
return ewrap(f.Truncate(size))
}

func (fs *FilesystemServer) Fwrite(ctx context.Context, file proxy.FileHandle, buffer []byte) (_r int32, _err error) {
Expand All @@ -114,7 +116,7 @@ func (fs *FilesystemServer) Fwrite(ctx context.Context, file proxy.FileHandle, b
return 0, os.ErrInvalid
}
r, err := f.Write(buffer)
return int32(r), err
return int32(r), ewrap(err)
}

func (fs *FilesystemServer) FwriteAt(ctx context.Context, file proxy.FileHandle, buffer []byte, offset int64) (_r int32, _err error) {
Expand All @@ -123,7 +125,7 @@ func (fs *FilesystemServer) FwriteAt(ctx context.Context, file proxy.FileHandle,
return 0, os.ErrInvalid
}
r, err := f.WriteAt(buffer, offset)
return int32(r), err
return int32(r), ewrap(err)
}

func (fs *FilesystemServer) FwriteString(ctx context.Context, file proxy.FileHandle, value string) (_r int32, _err error) {
Expand All @@ -132,5 +134,5 @@ func (fs *FilesystemServer) FwriteString(ctx context.Context, file proxy.FileHan
return 0, os.ErrInvalid
}
r, err := f.WriteString(value)
return int32(r), err
return int32(r), ewrap(err)
}
20 changes: 10 additions & 10 deletions bindings/proxy/server/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func New(fs afero.Fs) proxy.Filesystem {
var _ proxy.Filesystem = (*FilesystemServer)(nil)

func (fs *FilesystemServer) Chown(ctx context.Context, name string, uid int32, gid int32) (_err error) {
return fs.fs.Chown(name, int(uid), int(gid))
return ewrap(fs.fs.Chown(name, int(uid), int(gid)))
}

func (fs *FilesystemServer) Chtimes(ctx context.Context, name string, atime proxy.Timestamp, mtime proxy.Timestamp) (_err error) {
return fs.fs.Chtimes(name, time.UnixMicro(int64(atime)), time.UnixMicro(int64(mtime)))
return ewrap(fs.fs.Chtimes(name, time.UnixMicro(int64(atime)), time.UnixMicro(int64(mtime))))
}

func (fs *FilesystemServer) Create(ctx context.Context, name string) (_r proxy.FileHandle, _err error) {
file, err := fs.fs.Create(name)
if err != nil {
return 0, err
return 0, ewrap(err)
}
fs.count++
handle := proxy.FileHandle(fs.count)
Expand All @@ -45,11 +45,11 @@ func (fs *FilesystemServer) Create(ctx context.Context, name string) (_r proxy.F
}

func (fs *FilesystemServer) Mkdir(ctx context.Context, path string, perm proxy.FileMode) (_err error) {
return fs.fs.Mkdir(path, os.FileMode(perm))
return ewrap(fs.fs.Mkdir(path, os.FileMode(perm)))
}

func (fs *FilesystemServer) MkdirAll(ctx context.Context, path string, perm proxy.FileMode) (_err error) {
return fs.fs.MkdirAll(path, os.FileMode(perm))
return ewrap(fs.fs.MkdirAll(path, os.FileMode(perm)))
}

func (fs *FilesystemServer) Name(ctx context.Context) (_r string, _err error) {
Expand All @@ -59,7 +59,7 @@ func (fs *FilesystemServer) Name(ctx context.Context) (_r string, _err error) {
func (fs *FilesystemServer) Open(ctx context.Context, name string) (_r proxy.FileHandle, _err error) {
file, err := fs.fs.Open(name)
if err != nil {
return 0, err
return 0, ewrap(err)
}
fs.count++
handle := proxy.FileHandle(fs.count)
Expand All @@ -70,7 +70,7 @@ func (fs *FilesystemServer) Open(ctx context.Context, name string) (_r proxy.Fil
func (fs *FilesystemServer) OpenFile(ctx context.Context, name string, flag int32, perm proxy.FileMode) (_r proxy.FileHandle, _err error) {
file, err := fs.fs.OpenFile(name, int(flag), os.FileMode(perm))
if err != nil {
return 0, err
return 0, ewrap(err)
}
fs.count++
handle := proxy.FileHandle(fs.count)
Expand All @@ -79,15 +79,15 @@ func (fs *FilesystemServer) OpenFile(ctx context.Context, name string, flag int3
}

func (fs *FilesystemServer) Remove(ctx context.Context, name string) (_err error) {
return fs.fs.Remove(name)
return ewrap(fs.fs.Remove(name))
}

func (fs *FilesystemServer) RemoveAll(ctx context.Context, name string) (_err error) {
return fs.fs.RemoveAll(name)
return ewrap(fs.fs.RemoveAll(name))
}

func (fs *FilesystemServer) Rename(ctx context.Context, oldname string, newname string) (_err error) {
return fs.fs.Rename(oldname, newname)
return ewrap(fs.fs.Rename(oldname, newname))
}

func wrapFileInfo(file os.FileInfo) *proxy.FileInfo {
Expand Down

0 comments on commit f487289

Please sign in to comment.