From 37f84817247d3b8e687a701ccb0d6bc7ffe3cb78 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Fri, 22 Feb 2019 23:41:38 +0100 Subject: errors: add Frame and Formatter/Printer interfaces errors.New now implements Formatter and includes Frame information that is reported when detail is requested. Partly implements proposal Issue #29934. Change-Id: Id76888d246d7d862595b5e92d517b9c03f23a7a6 Reviewed-on: https://go-review.googlesource.com/c/163557 Run-TryBot: Marcel van Lohuizen TryBot-Result: Gobot Gobot Reviewed-by: Damien Neil --- src/errors/errors.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/errors/errors.go') diff --git a/src/errors/errors.go b/src/errors/errors.go index b8a46921be..f23a96c43e 100644 --- a/src/errors/errors.go +++ b/src/errors/errors.go @@ -6,15 +6,25 @@ package errors // New returns an error that formats as the given text. +// +// The returned error contains a Frame set to the caller's location and +// implements Formatter to show this information when printed with details. func New(text string) error { - return &errorString{text} + return &errorString{text, Caller(1)} } // errorString is a trivial implementation of error. type errorString struct { - s string + s string + frame Frame } func (e *errorString) Error() string { return e.s } + +func (e *errorString) FormatError(p Printer) (next error) { + p.Print(e.s) + e.frame.Format(p) + return nil +} -- cgit v1.3