Пример #1
0
// backendNewColor returns a new colorful error string using the following
// format:
//    pkg.func (file:line): error: text
func backendNewColor(text string, skip int) error {
	pc, file, line, ok := runtime.Caller(skip)
	if !ok {
		return errors.New(term.RedBold("error: ") + text)
	}
	f := runtime.FuncForPC(pc)
	if f == nil {
		format := term.WhiteBold("(%s:%d): ") + term.RedBold("error: ") + "%s"
		return fmt.Errorf(format, path.Base(file), line, text)
	}
	format := term.MagentaBold("%s") + term.WhiteBold(" (%s:%d): ") + term.RedBold("error: ") + "%s"
	return fmt.Errorf(format, f.Name(), path.Base(file), line, text)
}
Пример #2
0
Файл: err.go Проект: mewkiz/pkg
// Error returns an error string with position information.
//
// The error format is as follows:
//    pkg.func (file:line): error: text
func (e *ErrInfo) Error() string {
	text := "<nil>"
	if e.Err != nil {
		text = e.Err.Error()
	}

	if UseColor {
		// Use colors.
		prefix := term.RedBold("error:")
		if e.pos == nil {
			return fmt.Sprintf("%s %s", prefix, text)
		}
		return fmt.Sprintf("%s %s %s", e.pos, prefix, text)
	}

	// No colors.
	if e.pos == nil {
		return text
	}
	return fmt.Sprintf("%s %s", e.pos, text)

}
Пример #3
0
// Error returns an error string with position information.
//
// The error format is as follows.
//
//    (file:line:column): error: text
func (e *Error) Error() string {
	// Use colors.
	pos := fmt.Sprintf("(byte offset %d)", e.Pos)
	prefix := "error:"
	text := e.Text
	if UseColor {
		pos = term.Color(pos, term.Bold)
		prefix = term.RedBold(prefix)
		text = term.Color(text, term.Bold)
	}
	src := e.Src
	if src == nil {
		// If Src is nil, the error format is as follows.
		//
		//    (byte offset %d) error: text
		return fmt.Sprintf("%s %s %s", pos, prefix, text)
	}
	// The error format is as follows.
	//
	//    (file:line) error: text
	//       1 = y
	//         ^
	line, col := src.Position(e.Pos)
	end := len(src.Input)
	if len(src.Lines) > line {
		end = src.Lines[line]
	}
	srcLine := src.Input[src.Lines[line-1]:end]
	srcLine = strings.Replace(srcLine, "\t", " ", -1)
	srcLine = strings.TrimRight(srcLine, "\n\r")
	arrow := fmt.Sprintf("%*s", col, "^")
	pos = fmt.Sprintf("(%s:%d)", src.Path, line)
	if UseColor {
		pos = term.Color(pos, term.Bold)
		arrow = term.Color(arrow, term.Bold)
	}
	return fmt.Sprintf("%s %s %s\n%s\n%s", pos, prefix, text, srcLine, arrow)
}