...

Source file src/github.com/palantir/go-baseapp/pkg/errfmt/errfmt.go

Documentation: github.com/palantir/go-baseapp/pkg/errfmt

     1  // Copyright 2018 Palantir Technologies, Inc.
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package errfmt
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/pkg/errors"
    21  )
    22  
    23  type causer interface {
    24  	Cause() error
    25  }
    26  
    27  type stackTracer interface {
    28  	StackTrace() errors.StackTrace
    29  }
    30  
    31  func Print(err error) string {
    32  	if err == nil {
    33  		return ""
    34  	}
    35  
    36  	var deepestStack stackTracer
    37  	currErr := err
    38  	for currErr != nil {
    39  		if st, ok := currErr.(stackTracer); ok {
    40  			deepestStack = st
    41  		}
    42  		cause, ok := currErr.(causer)
    43  		if !ok {
    44  			break
    45  		}
    46  		currErr = cause.Cause()
    47  	}
    48  
    49  	if deepestStack == nil {
    50  		return err.Error()
    51  	}
    52  
    53  	return fmt.Sprintf("%s%+v", err.Error(), deepestStack.StackTrace())
    54  }
    55  

View as plain text