2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-11-01 21:46:59 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Package errors implements functions to manipulate errors.
|
|
|
|
|
package errors
|
|
|
|
|
|
2019-03-13 16:25:02 +01:00
|
|
|
import (
|
|
|
|
|
"internal/errinternal"
|
|
|
|
|
"runtime"
|
|
|
|
|
)
|
2019-03-13 16:47:44 +01:00
|
|
|
|
2011-11-01 21:46:59 -04:00
|
|
|
// New returns an error that formats as the given text.
|
2019-02-22 23:41:38 +01:00
|
|
|
//
|
|
|
|
|
// The returned error contains a Frame set to the caller's location and
|
|
|
|
|
// implements Formatter to show this information when printed with details.
|
2011-11-01 21:46:59 -04:00
|
|
|
func New(text string) error {
|
2019-03-13 16:47:44 +01:00
|
|
|
// Inline call to errors.Callers to improve performance.
|
|
|
|
|
var s Frame
|
|
|
|
|
runtime.Callers(2, s.frames[:])
|
2019-03-13 16:25:02 +01:00
|
|
|
return &errorString{text, nil, s}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
errinternal.NewError = func(text string, err error) error {
|
|
|
|
|
var s Frame
|
|
|
|
|
runtime.Callers(3, s.frames[:])
|
|
|
|
|
return &errorString{text, err, s}
|
|
|
|
|
}
|
2011-11-01 21:46:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// errorString is a trivial implementation of error.
|
|
|
|
|
type errorString struct {
|
2019-02-22 23:41:38 +01:00
|
|
|
s string
|
2019-03-13 16:25:02 +01:00
|
|
|
err error
|
2019-02-22 23:41:38 +01:00
|
|
|
frame Frame
|
2011-11-01 21:46:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *errorString) Error() string {
|
2019-03-13 16:25:02 +01:00
|
|
|
if e.err != nil {
|
|
|
|
|
return e.s + ": " + e.err.Error()
|
|
|
|
|
}
|
2011-11-01 21:46:59 -04:00
|
|
|
return e.s
|
|
|
|
|
}
|
2019-02-22 23:41:38 +01:00
|
|
|
|
|
|
|
|
func (e *errorString) FormatError(p Printer) (next error) {
|
|
|
|
|
p.Print(e.s)
|
|
|
|
|
e.frame.Format(p)
|
2019-03-13 16:25:02 +01:00
|
|
|
return e.err
|
2019-02-22 23:41:38 +01:00
|
|
|
}
|