github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/common/herrors/errors.go (about)

     1  // Copyright 2018 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  	_errors "github.com/pkg/errors"
    28  )
    29  
    30  // As defined in https://godoc.org/github.com/pkg/errors
    31  type causer interface {
    32  	Cause() error
    33  }
    34  
    35  type stackTracer interface {
    36  	StackTrace() _errors.StackTrace
    37  }
    38  
    39  // PrintStackTraceFromErr prints the error's stack trace to stdoud.
    40  func PrintStackTraceFromErr(err error) {
    41  	FprintStackTraceFromErr(os.Stdout, err)
    42  }
    43  
    44  // FprintStackTraceFromErr prints the error's stack trace to w.
    45  func FprintStackTraceFromErr(w io.Writer, err error) {
    46  	if err, ok := err.(stackTracer); ok {
    47  		for _, f := range err.StackTrace() {
    48  			fmt.Fprintf(w, "%+s:%d\n", f, f)
    49  		}
    50  	}
    51  }
    52  
    53  // PrintStackTrace prints the current stacktrace to w.
    54  func PrintStackTrace(w io.Writer) {
    55  	buf := make([]byte, 1<<16)
    56  	runtime.Stack(buf, true)
    57  	fmt.Fprintf(w, "%s", buf)
    58  }
    59  
    60  // ErrorSender is a, typically, non-blocking error handler.
    61  type ErrorSender interface {
    62  	SendError(err error)
    63  }
    64  
    65  // Recover is a helper function that can be used to capture panics.
    66  // Put this at the top of a method/function that crashes in a template:
    67  //     defer herrors.Recover()
    68  func Recover(args ...interface{}) {
    69  	if r := recover(); r != nil {
    70  		fmt.Println("ERR:", r)
    71  		args = append(args, "stacktrace from panic: \n"+string(debug.Stack()), "\n")
    72  		fmt.Println(args...)
    73  	}
    74  }
    75  
    76  // Get the current goroutine id. Used only for debugging.
    77  func GetGID() uint64 {
    78  	b := make([]byte, 64)
    79  	b = b[:runtime.Stack(b, false)]
    80  	b = bytes.TrimPrefix(b, []byte("goroutine "))
    81  	b = b[:bytes.IndexByte(b, ' ')]
    82  	n, _ := strconv.ParseUint(string(b), 10, 64)
    83  	return n
    84  }
    85  
    86  // ErrFeatureNotAvailable denotes that a feature is unavailable.
    87  //
    88  // We will, at least to begin with, make some Hugo features (SCSS with libsass) optional,
    89  // and this error is used to signal those situations.
    90  var ErrFeatureNotAvailable = errors.New("this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information")