diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-12-14 12:20:13 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-12-14 13:51:06 +0100 |
commit | ad2059878a8d6ace9669ccc5ff0a8d4e5811ad37 (patch) | |
tree | 34d88705ab22e5b54949a6ce48e028b1fc2ab7e6 /common/herrors/errors.go | |
parent | 87e898a17a52b5338bc9d554dd12b99a54aa2431 (diff) |
Also consider wrapped errors when checking for file IsNotExist errors
Fixes #10534
Diffstat (limited to 'common/herrors/errors.go')
-rw-r--r-- | common/herrors/errors.go | 21 |
1 files changed, 19 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 +} |