github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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  // GetGID 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  // IsFeatureNotAvailableError returns true if the given error is or contains a FeatureNotAvailableError.
    63  func IsFeatureNotAvailableError(err error) bool {
    64  	return errors.Is(err, &FeatureNotAvailableError{})
    65  }
    66  
    67  // ErrFeatureNotAvailable denotes that a feature is unavailable.
    68  //
    69  // We will, at least to begin with, make some Hugo features (SCSS with libsass) optional,
    70  // and this error is used to signal those situations.
    71  var ErrFeatureNotAvailable = &FeatureNotAvailableError{Cause: errors.New("this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information")}
    72  
    73  // FeatureNotAvailableError is an error type used to signal that a feature is not available.
    74  type FeatureNotAvailableError struct {
    75  	Cause error
    76  }
    77  
    78  func (e *FeatureNotAvailableError) Unwrap() error {
    79  	return e.Cause
    80  }
    81  
    82  func (e *FeatureNotAvailableError) Error() string {
    83  	return e.Cause.Error()
    84  }
    85  
    86  func (e *FeatureNotAvailableError) Is(target error) bool {
    87  	_, ok := target.(*FeatureNotAvailableError)
    88  	return ok
    89  }
    90  
    91  // Must panics if err != nil.
    92  func Must(err error) {
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  }
    97  
    98  // IsNotExist returns true if the error is a file not found error.
    99  // Unlike os.IsNotExist, this also considers wrapped errors.
   100  func IsNotExist(err error) bool {
   101  	if os.IsNotExist(err) {
   102  		return true
   103  	}
   104  
   105  	// os.IsNotExist does not consider wrapped errors.
   106  	if os.IsNotExist(errors.Unwrap(err)) {
   107  		return true
   108  	}
   109  
   110  	return false
   111  }