Comments (1)
The developers are going to have broader options on how to print the returned error. That is one benefit of this improvement.
In addition to that, go-sarah itself also prints some errors with a logger -- the go-sarah's default logger or a logger implementation given by a developer. Checking how the format works on the error implementation of old errors
package and new xerrors
package, and choose the one that has the smallest impact is vital. The ideal formatting is that current logging parts work as-is when old errors
is returned from a depending third-party library and switch to print richer information when the depending library migrates to xerrors
.
As of Go 1.12, when v
, s
, x
, X
or q
is given as a verb
part, passed argument's error.Error()
or Stringer.String()
is called as long as the argument implements error
or Stringer
.
https://github.com/golang/go/blob/3a1b4e75f8b6c1b57db73bccf7ca871bf1a97ca9/src/fmt/print.go#L600-L619
Format behavior of current error
implementation
Because xerrors
prints stacked error with its own fmt.Formatter implementation when %+v
format is given, below test with current error
implementation focuses on formats with v
.
package main
import (
"fmt"
)
func main() {
fmt.Printf("%%s:\n%s\n\n", &myError{"this is my error string"})
fmt.Printf("%%v:\n%v\n\n", &myError{"this is my error string"})
fmt.Printf("%%+v:\n%+v\n\n", &myError{"this is my error string"})
fmt.Printf("%%#v:\n%#v\n\n", &myError{"this is my error string"}) // v with sharp is treated differently
}
type myError struct {
field string
}
var _ error = (*myError)(nil)
func (e *myError) Error() string {
return e.field
}
The output is as below:
%s:
this is my error string
%v:
this is my error string
%+v:
this is my error string
%#v:
&main.myError{field:"this is my error string"}
Format behavior of xerror
implementation
package main
import (
"fmt"
"golang.org/x/xerrors"
)
func main() {
err := outer()
fmt.Printf("%%s:\n%s\n\n", err)
fmt.Printf("%%s with error.Error():\n%s\n\n", err.Error())
fmt.Printf("%%v:\n%v\n\n", err)
fmt.Printf("%%+v:\n%+v\n\n", err)
fmt.Printf("%%#v:\n%#v\n\n", err)
}
func outer() error {
err := middle()
if err != nil {
return xerrors.Errorf("failed calling the middle method: %w", err)
}
return nil
}
func middle() error {
err := root()
if err != nil {
return xerrors.Errorf("failed on root method: %w", err)
}
return nil
}
func root() error {
return xerrors.New("buzz")
}
The output is as below:
%s:
failed calling the middle method: failed on root method: buzz
%s with error.Error():
failed calling the middle method: failed on root method: buzz
%v:
failed calling the middle method: failed on root method: buzz
%+v:
failed calling the middle method:
main.outer
/Users/Oklahomer/dev/xerrors-sample/main.go:19
- failed on root method:
main.middle
/Users/Oklahomer/dev/xerrors-sample/main.go:27
- buzz:
main.root
/Users/Oklahomer/dev/xerrors-sample/main.go:33
%#v:
failed calling the middle method: failed on root method: buzz
Conclusion
Current logging parts mostly use %s
with "error.Error()." This keeps working in the exact same manner as shown in the above test results when xerrors
is used. However, that cannot benefit from the new xerrors
feature to print stacked errors in a hierarchical manner.
The test results suggest that below formatting maintains working in the same way with old error
and still benefits from xerrors
.
fmt.Printf("error on blah blah: %+v", err)
log.Errorf("error on blah blah: %+v", err)
from go-sarah.
Related Issues (20)
- Rename example directory to _example
- Add golang 1.16 to Travis CI setting
- Migrate to go-kasumi
- H4K3R HOT 1
- Set up scheduler's logger in favor of robfig/cron improvement
- New HOT 1
- Race condition on live configuration update HOT 1
- Find suitable replacement for forked github.com/robfig/cron. HOT 4
- On JSON deserialization, let time.Duration-type field accept time.ParseDuration friendly format HOT 2
- xmpp adapter HOT 9
- Remove Go 1.6 support
- Mattermost Adapter HOT 4
- Plan v2 release HOT 2
- Exclude Message() method from sarah.Input interface HOT 1
- Rename SentAt() method of sarah.Input to TimeStamp() HOT 1
- sarah.Command also receives sarah.Input to return available help text
- Provide high-level interface to run go-sarah
- Bot supervisor should also receive Bot's noteworthy state in addition to currently handled critical state HOT 1
- Generalize watcher interface
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-sarah.