diff options
author | Bjørn Erik Pedersen <[email protected]> | 2022-12-14 12:20:13 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2022-12-14 13:51:06 +0100 |
commit | ad2059878a8d6ace9669ccc5ff0a8d4e5811ad37 (patch) | |
tree | 34d88705ab22e5b54949a6ce48e028b1fc2ab7e6 /common | |
parent | 87e898a17a52b5338bc9d554dd12b99a54aa2431 (diff) | |
download | hugo-ad2059878a8d6ace9669ccc5ff0a8d4e5811ad37.tar.gz hugo-ad2059878a8d6ace9669ccc5ff0a8d4e5811ad37.zip |
Also consider wrapped errors when checking for file IsNotExist errors
Fixes #10534
Diffstat (limited to 'common')
-rw-r--r-- | common/herrors/errors.go | 21 | ||||
-rw-r--r-- | common/herrors/errors_test.go | 36 |
2 files changed, 55 insertions, 2 deletions
diff --git a/common/herrors/errors.go b/common/herrors/errors.go index 6ce908853..822271ef2 100644 --- a/common/herrors/errors.go +++ b/common/herrors/errors.go @@ -1,4 +1,4 @@ -// Copyright 2018 The Hugo Authors. All rights reserved. +// Copyright 2022 The Hugo Authors. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import ( "errors" "fmt" "io" + "os" "runtime" "runtime/debug" "strconv" @@ -38,7 +39,8 @@ type ErrorSender interface { // Recover is a helper function that can be used to capture panics. // Put this at the top of a method/function that crashes in a template: -// defer herrors.Recover() +// +// defer herrors.Recover() func Recover(args ...any) { if r := recover(); r != nil { fmt.Println("ERR:", r) @@ -69,3 +71,18 @@ func Must(err error) { panic(err) } } + +// IsNotExist returns true if the error is a file not found error. +// Unlike os.IsNotExist, this also considers wrapped errors. +func IsNotExist(err error) bool { + if os.IsNotExist(err) { + return true + } + + // os.IsNotExist does not consider wrapped errors. + if os.IsNotExist(errors.Unwrap(err)) { + return true + } + + return false +} diff --git a/common/herrors/errors_test.go b/common/herrors/errors_test.go new file mode 100644 index 000000000..1e0730028 --- /dev/null +++ b/common/herrors/errors_test.go @@ -0,0 +1,36 @@ +// Copyright 2022 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package herrors + +import ( + "fmt" + "testing" + + qt "github.com/frankban/quicktest" + "github.com/spf13/afero" +) + +func TestIsNotExist(t *testing.T) { + c := qt.New(t) + + c.Assert(IsNotExist(afero.ErrFileNotFound), qt.Equals, true) + c.Assert(IsNotExist(afero.ErrFileExists), qt.Equals, false) + c.Assert(IsNotExist(afero.ErrDestinationExists), qt.Equals, false) + c.Assert(IsNotExist(nil), qt.Equals, false) + + c.Assert(IsNotExist(fmt.Errorf("foo")), qt.Equals, false) + + // os.IsNotExist returns false for wrapped errors. + c.Assert(IsNotExist(fmt.Errorf("foo: %w", afero.ErrFileNotFound)), qt.Equals, true) +} |