Skip to content

Commit

Permalink
filesystem: refactor: use errors.Is instead of os.IsNotExist
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Aug 5, 2023
1 parent e91b02b commit 15b347d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion internal/gopsutil/net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net"
"os"
"strconv"
Expand Down Expand Up @@ -673,7 +674,7 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
t, err := getProcInodes(root, pid, max)
if err != nil {
// skip if permission error or no longer exists
if os.IsPermission(err) || os.IsNotExist(err) || err == io.EOF {
if os.IsPermission(err) || errors.Is(err, fs.ErrNotExist) || err == io.EOF {
continue
}
return ret, err
Expand Down
4 changes: 3 additions & 1 deletion internal/gopsutil/process/process_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package process

import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -86,7 +88,7 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
// This covers the case when running inside container with a different process namespace (by default)

_, err := os.Stat(common.HostProc(strconv.Itoa(int(pid))))
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return err == nil, err
Expand Down
15 changes: 8 additions & 7 deletions middleware/filesystem/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package filesystem

import (
"errors"
"fmt"
"io/fs"
"net/http"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -143,11 +144,11 @@ func New(config ...Config) fiber.Handler {
path = utils.TrimRight(path, '/')
}
file, err := cfg.Root.Open(path)
if err != nil && os.IsNotExist(err) && cfg.NotFoundFile != "" {
if err != nil && errors.Is(err, fs.ErrNotExist) && cfg.NotFoundFile != "" {
file, err = cfg.Root.Open(cfg.NotFoundFile)
}
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return c.Status(fiber.StatusNotFound).Next()
}
return fmt.Errorf("failed to open: %w", err)
Expand Down Expand Up @@ -217,10 +218,10 @@ func New(config ...Config) fiber.Handler {
}

// SendFile ...
func SendFile(c *fiber.Ctx, fs http.FileSystem, path string) error {
file, err := fs.Open(path)
func SendFile(c *fiber.Ctx, filesystem http.FileSystem, path string) error {
file, err := filesystem.Open(path)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, fs.ErrNotExist) {
return fiber.ErrNotFound
}
return fmt.Errorf("failed to open: %w", err)
Expand All @@ -234,7 +235,7 @@ func SendFile(c *fiber.Ctx, fs http.FileSystem, path string) error {
// Serve index if path is directory
if stat.IsDir() {
indexPath := utils.TrimRight(path, '/') + ConfigDefault.Index
index, err := fs.Open(indexPath)
index, err := filesystem.Open(indexPath)
if err == nil {
indexStat, err := index.Stat()
if err == nil {
Expand Down

0 comments on commit 15b347d

Please sign in to comment.