linsk/utils/errors.go

26 lines
685 B
Go
Raw Normal View History

2023-08-27 15:53:44 +01:00
package utils
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
func WrapErrWithLog(err error, msg, log string) error {
2023-08-29 11:51:06 +01:00
return errors.Wrapf(err, "%v %v", msg, GetLogErrMsg(log, "log"))
2023-08-27 15:53:44 +01:00
}
2023-08-29 11:51:06 +01:00
func GetLogErrMsg(s string, logLabel string) string {
2023-08-27 15:53:44 +01:00
logToInclude := strings.ReplaceAll(s, "\n", "\\n")
logToInclude = strings.TrimSuffix(logToInclude, "\\n")
logToInclude = ClearUnprintableChars(logToInclude, false)
origLogLen := len(logToInclude)
const maxLogLen = 256
if origLogLen > maxLogLen {
2023-09-01 16:29:01 +01:00
logToInclude = fmt.Sprintf("[%v chars trimmed]", origLogLen-maxLogLen) + logToInclude[len(logToInclude)-maxLogLen:]
2023-08-27 15:53:44 +01:00
}
2023-08-29 11:51:06 +01:00
return fmt.Sprintf("(%v: '%v')", logLabel, logToInclude)
2023-08-27 15:53:44 +01:00
}