github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/common/herrors/errors.go (about) 1 // Copyright 2022 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // Package herrors contains common Hugo errors and error related utilities. 15 package herrors 16 17 import ( 18 "bytes" 19 "errors" 20 "fmt" 21 "io" 22 "os" 23 "runtime" 24 "runtime/debug" 25 "strconv" 26 ) 27 28 // PrintStackTrace prints the current stacktrace to w. 29 func PrintStackTrace(w io.Writer) { 30 buf := make([]byte, 1<<16) 31 runtime.Stack(buf, true) 32 fmt.Fprintf(w, "%s", buf) 33 } 34 35 // ErrorSender is a, typically, non-blocking error handler. 36 type ErrorSender interface { 37 SendError(err error) 38 } 39 40 // Recover is a helper function that can be used to capture panics. 41 // Put this at the top of a method/function that crashes in a template: 42 // 43 // defer herrors.Recover() 44 func Recover(args ...any) { 45 if r := recover(); r != nil { 46 fmt.Println("ERR:", r) 47 args = append(args, "stacktrace from panic: \n"+string(debug.Stack()), "\n") 48 fmt.Println(args...) 49 } 50 } 51 52 // Get the current goroutine id. Used only for debugging. 53 func GetGID() uint64 { 54 b := make([]byte, 64) 55 b = b[:runtime.Stack(b, false)] 56 b = bytes.TrimPrefix(b, []byte("goroutine ")) 57 b = b[:bytes.IndexByte(b, ' ')] 58 n, _ := strconv.ParseUint(string(b), 10, 64) 59 return n 60 } 61 62 // ErrFeatureNotAvailable denotes that a feature is unavailable. 63 // 64 // We will, at least to begin with, make some Hugo features (SCSS with libsass) optional, 65 // and this error is used to signal those situations. 66 var ErrFeatureNotAvailable = errors.New("this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information") 67 68 // Must panics if err != nil. 69 func Must(err error) { 70 if err != nil { 71 panic(err) 72 } 73 } 74 75 // IsNotExist returns true if the error is a file not found error. 76 // Unlike os.IsNotExist, this also considers wrapped errors. 77 func IsNotExist(err error) bool { 78 if os.IsNotExist(err) { 79 return true 80 } 81 82 // os.IsNotExist does not consider wrapped errors. 83 if os.IsNotExist(errors.Unwrap(err)) { 84 return true 85 } 86 87 return false 88 }