1
0
mirror of https://github.com/golang/go synced 2024-11-21 19:04:44 -07:00

html,bzip2,sql: rename Error methods that return error to Err

There are three classes of methods/functions called Error:

a) The Error method in the just introduced error interface
b) Error methods that create or report errors (http.Error, etc)
c) Error methods that return errors previously associated with
   the receiver (Tokenizer.Error, rows.Error, etc).

This CL introduces the convention that methods in case (c)
should be named Err.

The reasoning for the change is:

- The change differentiates the two kinds of APIs based on
  names rather than just on signature, unloading Error a bit
- Err is closer to the err variable name that is so commonly
  used with the intent of verifying an error
- Err is shorter and thus more convenient to be used often
  on error verifications, such as in iterators following the
  convention of the sql package.

R=bradfitz, rsc
CC=golang-dev
https://golang.org/cl/5327064
This commit is contained in:
Gustavo Niemeyer 2011-11-04 09:50:20 -04:00
parent 39fcca60cb
commit f2dc50b48d
10 changed files with 103 additions and 16 deletions

View File

@ -9,6 +9,7 @@ GOFILES=\
error.go\
filepath.go\
fix.go\
htmlerr.go\
httpfinalurl.go\
httpfs.go\
httpheaders.go\

View File

@ -481,7 +481,7 @@ func newPkgDot(pos token.Pos, pkg, name string) ast.Expr {
}
}
// renameTop renames all references to the top-level name top.
// renameTop renames all references to the top-level name old.
// It returns true if it makes any changes.
func renameTop(f *ast.File, old, new string) bool {
var fixed bool

47
src/cmd/gofix/htmlerr.go Normal file
View File

@ -0,0 +1,47 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"go/ast"
)
func init() {
register(htmlerrFix)
}
var htmlerrFix = fix{
"htmlerr",
"2011-11-04",
htmlerr,
`Rename html's Tokenizer.Error method to Err.
http://codereview.appspot.com/5327064/
`,
}
var htmlerrTypeConfig = &TypeConfig{
Func: map[string]string{
"html.NewTokenizer": "html.Tokenizer",
},
}
func htmlerr(f *ast.File) bool {
if !imports(f, "html") {
return false
}
typeof, _ := typecheck(htmlerrTypeConfig, f)
fixed := false
walk(f, func(n interface{}) {
s, ok := n.(*ast.SelectorExpr)
if ok && typeof[s.X] == "html.Tokenizer" && s.Sel.Name == "Error" {
s.Sel.Name = "Err"
fixed = true
}
})
return fixed
}

View File

@ -0,0 +1,39 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
func init() {
addTestCases(htmlerrTests, htmlerr)
}
var htmlerrTests = []testCase{
{
Name: "htmlerr.0",
In: `package main
import (
"html"
)
func f() {
e := errors.New("")
t := html.NewTokenizer(r)
_, _ = e.Error(), t.Error()
}
`,
Out: `package main
import (
"html"
)
func f() {
e := errors.New("")
t := html.NewTokenizer(r)
_, _ = e.Error(), t.Err()
}
`,
},
}

View File

@ -37,7 +37,7 @@ func newBitReader(r io.Reader) bitReader {
// ReadBits64 reads the given number of bits and returns them in the
// least-significant part of a uint64. In the event of an error, it returns 0
// and the error can be obtained by calling Error().
// and the error can be obtained by calling Err().
func (br *bitReader) ReadBits64(bits uint) (n uint64) {
for bits > br.bits {
b, err := br.r.ReadByte()
@ -82,6 +82,6 @@ func (br *bitReader) ReadBit() bool {
return n != 0
}
func (br *bitReader) Error() error {
func (br *bitReader) Err() error {
return br.err
}

View File

@ -80,7 +80,7 @@ func (bz2 *reader) Read(buf []byte) (n int, err error) {
if !bz2.setupDone {
err = bz2.setup()
brErr := bz2.br.Error()
brErr := bz2.br.Err()
if brErr != nil {
err = brErr
}
@ -91,7 +91,7 @@ func (bz2 *reader) Read(buf []byte) (n int, err error) {
}
n, err = bz2.read(buf)
brErr := bz2.br.Error()
brErr := bz2.br.Err()
if brErr != nil {
err = brErr
}

View File

@ -620,7 +620,7 @@ func (s *Stmt) Close() error {
// err = rows.Scan(&id, &name)
// ...
// }
// err = rows.Error() // get any Error encountered during iteration
// err = rows.Err() // get any error encountered during iteration
// ...
type Rows struct {
db *DB
@ -651,8 +651,8 @@ func (rs *Rows) Next() bool {
return rs.lasterr == nil
}
// Error returns the error, if any, that was encountered during iteration.
func (rs *Rows) Error() error {
// Err returns the error, if any, that was encountered during iteration.
func (rs *Rows) Err() error {
if rs.lasterr == io.EOF {
return nil
}

View File

@ -250,7 +250,7 @@ func (p *parser) read() error {
p.tok = p.tokenizer.Token()
switch p.tok.Type {
case ErrorToken:
return p.tokenizer.Error()
return p.tokenizer.Err()
case SelfClosingTagToken:
p.hasSelfClosingToken = true
p.tok.Type = StartTagToken

View File

@ -149,9 +149,9 @@ type Tokenizer struct {
textIsRaw bool
}
// Error returns the error associated with the most recent ErrorToken token.
// Err returns the error associated with the most recent ErrorToken token.
// This is typically io.EOF, meaning the end of tokenization.
func (z *Tokenizer) Error() error {
func (z *Tokenizer) Err() error {
if z.tt != ErrorToken {
return nil
}

View File

@ -427,7 +427,7 @@ loop:
if tt.golden != "" {
for i, s := range strings.Split(tt.golden, "$") {
if z.Next() == ErrorToken {
t.Errorf("%s token %d: want %q got error %v", tt.desc, i, s, z.Error())
t.Errorf("%s token %d: want %q got error %v", tt.desc, i, s, z.Err())
continue loop
}
actual := z.Token().String()
@ -438,8 +438,8 @@ loop:
}
}
z.Next()
if z.Error() != io.EOF {
t.Errorf("%s: want EOF got %q", tt.desc, z.Error())
if z.Err() != io.EOF {
t.Errorf("%s: want EOF got %q", tt.desc, z.Err())
}
}
}
@ -543,8 +543,8 @@ loop:
tt := z.Next()
switch tt {
case ErrorToken:
if z.Error() != io.EOF {
t.Error(z.Error())
if z.Err() != io.EOF {
t.Error(z.Err())
}
break loop
case TextToken: