gitee.com/gricks/utils@v1.0.8/recover.go (about)

     1  package utils
     2  
     3  import (
     4  	"runtime"
     5  	"strings"
     6  )
     7  
     8  type LoggerI interface {
     9  	Error(format string, v ...interface{})
    10  }
    11  
    12  func Recover(logger LoggerI, wrong *bool) {
    13  	if err := recover(); err != nil {
    14  		if logger != nil {
    15  			logger.Error("Err: %s", err)
    16  			for i := 0; i < 10; i++ {
    17  				pc, file, line, ok := runtime.Caller(i)
    18  				if ok {
    19  					p := strings.Index(file, "/src/")
    20  					if p != -1 {
    21  						file = file[p+len("/src/"):]
    22  					}
    23  					if logger != nil {
    24  						logger.Error("frame %d:[func:%s,file:%s,line:%d]",
    25  							i, runtime.FuncForPC(pc).Name(), file, line)
    26  					}
    27  				} else {
    28  					break
    29  				}
    30  			}
    31  		}
    32  		if wrong != nil {
    33  			*wrong = true
    34  		}
    35  	} else {
    36  		if wrong != nil {
    37  			*wrong = false
    38  		}
    39  	}
    40  }