hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

errors.go (2164B)

    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 	"runtime"
   23 	"runtime/debug"
   24 	"strconv"
   25 )
   26 
   27 // PrintStackTrace prints the current stacktrace to w.
   28 func PrintStackTrace(w io.Writer) {
   29 	buf := make([]byte, 1<<16)
   30 	runtime.Stack(buf, true)
   31 	fmt.Fprintf(w, "%s", buf)
   32 }
   33 
   34 // ErrorSender is a, typically, non-blocking error handler.
   35 type ErrorSender interface {
   36 	SendError(err error)
   37 }
   38 
   39 // Recover is a helper function that can be used to capture panics.
   40 // Put this at the top of a method/function that crashes in a template:
   41 //     defer herrors.Recover()
   42 func Recover(args ...any) {
   43 	if r := recover(); r != nil {
   44 		fmt.Println("ERR:", r)
   45 		args = append(args, "stacktrace from panic: \n"+string(debug.Stack()), "\n")
   46 		fmt.Println(args...)
   47 	}
   48 }
   49 
   50 // Get the current goroutine id. Used only for debugging.
   51 func GetGID() uint64 {
   52 	b := make([]byte, 64)
   53 	b = b[:runtime.Stack(b, false)]
   54 	b = bytes.TrimPrefix(b, []byte("goroutine "))
   55 	b = b[:bytes.IndexByte(b, ' ')]
   56 	n, _ := strconv.ParseUint(string(b), 10, 64)
   57 	return n
   58 }
   59 
   60 // ErrFeatureNotAvailable denotes that a feature is unavailable.
   61 //
   62 // We will, at least to begin with, make some Hugo features (SCSS with libsass) optional,
   63 // and this error is used to signal those situations.
   64 var ErrFeatureNotAvailable = errors.New("this feature is not available in your current Hugo version, see https://goo.gl/YMrWcn for more information")
   65 
   66 // Must panics if err != nil.
   67 func Must(err error) {
   68 	if err != nil {
   69 		panic(err)
   70 	}
   71 }