Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 19:30 UTC
chore: check against errors of `io/fs` instead of `os` (#6011)
* chore: replace `os.ErrNotExist` with `fs.ErrNotExist` * check against permission error from `io/fs` package
Mohammed Al Sahaf committed
Jan 2, 2024 at 08:48 UTC
787f6b257f3ee34431e4f5a9aa33a5399096a78c
7 files changed
+23
-22
caddy.go
+2
-1
@@ -22,6 +22,7 @@ import (
22
"errors"
23
"fmt"
24
"io"
25
+ "io/fs"
26
"log"
27
"net/http"
28
"os"
@@ -828,7 +829,7 @@ func InstanceID() (uuid.UUID, error) {
829
appDataDir := AppDataDir()
830
uuidFilePath := filepath.Join(appDataDir, "instance.uuid")
831
uuidFileBytes, err := os.ReadFile(uuidFilePath)
831
- if os.IsNotExist(err) {
832
+ if errors.Is(err, fs.ErrNotExist) {
833
uuid, err := uuid.NewRandom()
834
if err != nil {
835
return uuid, err
caddytest/caddytest.go
+2
-1
@@ -8,6 +8,7 @@ import (
8
"errors"
9
"fmt"
10
"io"
11
+ "io/fs"
12
"log"
13
"net"
14
"net/http"
@@ -232,7 +233,7 @@ const initConfig = `{
233
func validateTestPrerequisites(t *testing.T) error {
234
// check certificates are found
235
for _, certName := range Default.Certifcates {
235
- if _, err := os.Stat(getIntegrationDir() + certName); os.IsNotExist(err) {
236
+ if _, err := os.Stat(getIntegrationDir() + certName); errors.Is(err, fs.ErrNotExist) {
237
return fmt.Errorf("caddy integration test certificates (%s) not found", certName)
238
}
239
}
cmd/commandfuncs.go
+1
-1
@@ -190,7 +190,7 @@ func cmdRun(fl Flags) (int, error) {
190
var config []byte
191
if resumeFlag {
192
config, err = os.ReadFile(caddy.ConfigAutosavePath)
193
- if os.IsNotExist(err) {
193
+ if errors.Is(err, fs.ErrNotExist) {
194
// not a bad error; just can't resume if autosave file doesn't exist
195
caddy.Log().Info("no autosave file exists", zap.String("autosave_file", caddy.ConfigAutosavePath))
196
resumeFlag = false
cmd/main.go
+2
-1
@@ -21,6 +21,7 @@ import (
21
"flag"
22
"fmt"
23
"io"
24
+ "io/fs"
25
"log"
26
"net"
27
"os"
@@ -128,7 +129,7 @@ func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([
129
cfgAdapter = caddyconfig.GetAdapter("caddyfile")
130
if cfgAdapter != nil {
131
config, err = os.ReadFile("Caddyfile")
131
- if os.IsNotExist(err) {
132
+ if errors.Is(err, fs.ErrNotExist) {
133
// okay, no default Caddyfile; pretend like this never happened
134
cfgAdapter = nil
135
} else if err != nil {
modules/caddyhttp/fileserver/browse.go
+3
-2
@@ -19,6 +19,7 @@ import (
19
"context"
20
_ "embed"
21
"encoding/json"
22
+ "errors"
23
"fmt"
24
"io"
25
"io/fs"
@@ -92,9 +93,9 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter,
93
// TODO: not entirely sure if path.Clean() is necessary here but seems like a safe plan (i.e. /%2e%2e%2f) - someone could verify this
94
listing, err := fsrv.loadDirectoryContents(r.Context(), dir.(fs.ReadDirFile), root, path.Clean(r.URL.EscapedPath()), repl)
95
switch {
95
- case os.IsPermission(err):
96
+ case errors.Is(err, fs.ErrPermission):
97
return caddyhttp.Error(http.StatusForbidden, err)
97
- case os.IsNotExist(err):
98
+ case errors.Is(err, fs.ErrNotExist):
99
return fsrv.notFound(w, r, next)
100
case err != nil:
101
return caddyhttp.Error(http.StatusInternalServerError, err)
modules/caddyhttp/fileserver/staticfiles.go
+2
-2
@@ -506,10 +506,10 @@ func (fsrv *FileServer) openFile(filename string, w http.ResponseWriter) (fs.Fil
506
file, err := fsrv.fileSystem.Open(filename)
507
if err != nil {
508
err = fsrv.mapDirOpenError(err, filename)
509
- if os.IsNotExist(err) {
509
+ if errors.Is(err, fs.ErrNotExist) {
510
fsrv.logger.Debug("file not found", zap.String("filename", filename), zap.Error(err))
511
return nil, caddyhttp.Error(http.StatusNotFound, err)
512
- } else if os.IsPermission(err) {
512
+ } else if errors.Is(err, fs.ErrPermission) {
513
fsrv.logger.Debug("permission denied", zap.String("filename", filename), zap.Error(err))
514
return nil, caddyhttp.Error(http.StatusForbidden, err)
515
}
modules/caddyhttp/templates/tplcontext_test.go
+11
-14
@@ -17,7 +17,9 @@ package templates
17
import (
18
"bytes"
19
"context"
20
+ "errors"
21
"fmt"
22
+ "io/fs"
23
"net/http"
24
"os"
25
"path/filepath"
@@ -30,8 +32,7 @@ import (
32
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
33
)
34
33
-type handle struct {
34
-}
35
+type handle struct{}
36
37
func (h *handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
38
if r.Header.Get("Accept-Encoding") == "identity" {
@@ -176,11 +177,10 @@ func TestImport(t *testing.T) {
177
} else if !templateWasDefined && actual != "" {
178
// template should be defined, return value should be an empty string
179
t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, tplContext.tpl.DefinedTemplates())
179
-
180
}
181
182
if absFilePath != "" {
183
- if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
183
+ if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
184
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
185
}
186
}
@@ -255,21 +255,20 @@ func TestNestedInclude(t *testing.T) {
255
} else if buf.String() != test.expect {
256
//
257
t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, buf.String())
258
-
258
}
259
260
if absFilePath != "" {
262
- if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
261
+ if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
262
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
263
}
264
}
265
if absFilePath0 != "" {
267
- if err := os.Remove(absFilePath0); err != nil && !os.IsNotExist(err) {
266
+ if err := os.Remove(absFilePath0); err != nil && !errors.Is(err, fs.ErrNotExist) {
267
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
268
}
269
}
270
if absFilePath1 != "" {
272
- if err := os.Remove(absFilePath1); err != nil && !os.IsNotExist(err) {
271
+ if err := os.Remove(absFilePath1); err != nil && !errors.Is(err, fs.ErrNotExist) {
272
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
273
}
274
}
@@ -342,11 +341,10 @@ func TestInclude(t *testing.T) {
341
t.Errorf("Test %d: Expected error but had none", i)
342
} else if actual != test.expect {
343
t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
345
-
344
}
345
346
if absFilePath != "" {
349
- if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
347
+ if err := os.Remove(absFilePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
348
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
349
}
350
}
@@ -460,7 +458,7 @@ func TestFileListing(t *testing.T) {
458
fileNames: nil,
459
inputBase: "doesNotExist",
460
shouldErr: true,
463
- verifyErr: os.IsNotExist,
461
+ verifyErr: func(err error) bool { return errors.Is(err, fs.ErrNotExist) },
462
},
463
{
464
// directory and files exist, but path to a file
@@ -476,7 +474,7 @@ func TestFileListing(t *testing.T) {
474
fileNames: nil,
475
inputBase: filepath.Join("..", "..", "..", "..", "..", "etc"),
476
shouldErr: true,
479
- verifyErr: os.IsNotExist,
477
+ verifyErr: func(err error) bool { return errors.Is(err, fs.ErrNotExist) },
478
},
479
} {
480
tplContext := getContextOrFail(t)
@@ -525,7 +523,7 @@ func TestFileListing(t *testing.T) {
523
}
524
525
if dirPath != "" {
528
- if err := os.RemoveAll(dirPath); err != nil && !os.IsNotExist(err) {
526
+ if err := os.RemoveAll(dirPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
527
t.Fatalf("Test %d: Expected no error removing temporary test directory, got: %v", i, err)
528
}
529
}
@@ -602,7 +600,6 @@ title = "Welcome"
600
t.Errorf("Test %d: Expected body %s, found %s. Input was SplitFrontMatter(%s)", i, test.body, result.Body, test.input)
601
}
602
}
605
-
603
}
604
605
func TestHumanize(t *testing.T) {