github.com/decred/politeia@v1.4.0/util/errors.go (about) 1 // Copyright (c) 2021 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package util 6 7 import ( 8 "fmt" 9 10 errs "github.com/pkg/errors" 11 ) 12 13 // stackTracer represents the stack trace functionality for an error from 14 // pkg/errors. 15 type stackTracer interface { 16 StackTrace() errs.StackTrace 17 } 18 19 // StackTrace returns the stack trace for a pkg/errors error. The returned bool 20 // indicates whether the provided error is a pkg/errors error. Stack traces are 21 // not available for stdlib errors. 22 func StackTrace(err error) (string, bool) { 23 e, ok := errs.Cause(err).(stackTracer) 24 if !ok { 25 return "", false 26 } 27 return fmt.Sprintf("%+v\n", e.StackTrace()), true 28 }