Skip to content

Commit

Permalink
Rename errors to conform to Go style
Browse files Browse the repository at this point in the history
Renaming was done by gorename, so should be correct.
```
$ staticcheck -checks ST1012
fsys.go:52:2: error var Eperm should have name of the form ErrFoo (ST1012)
fsys.go:53:2: error var Eexist should have name of the form ErrFoo (ST1012)
fsys.go:54:2: error var Enotdir should have name of the form ErrFoo (ST1012)
xfid.go:19:5: error var Edel should have name of the form ErrFoo (ST1012)
xfid.go:20:5: error var Ebadctl should have name of the form ErrFoo (ST1012)
xfid.go:21:5: error var Ebadaddr should have name of the form ErrFoo (ST1012)
xfid.go:22:5: error var Eaddr should have name of the form ErrFoo (ST1012)
xfid.go:23:5: error var Einuse should have name of the form ErrFoo (ST1012)
xfid.go:24:5: error var Ebadevent should have name of the form ErrFoo (ST1012)
```
  • Loading branch information
fhs authored and rjkroege committed Jan 14, 2019
1 parent 75bb63b commit 846af7b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
2 changes: 1 addition & 1 deletion ecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func cmdexec(t *Text, cp *Cmd) bool {
func edittext(w *Window, q int, r []rune) error {
switch editing {
case Inactive:
return Eperm
return ErrPermission
case Inserting:
f := w.body.file
f.elog.Insert(q, r)
Expand Down
21 changes: 11 additions & 10 deletions fsys.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func initfcall() {
fcall[plan9.Twstat] = fsyswstat
}

// Errors returned by file server.
var (
Eperm = errors.New("permission denied")
Eexist = errors.New("file does not exist")
Enotdir = errors.New("not a directory")
ErrPermission = os.ErrPermission
ErrNotExist = os.ErrNotExist
ErrNotDir = errors.New("not a directory")
)

var dirtab = []*DirTab{
Expand Down Expand Up @@ -245,7 +246,7 @@ func fsysflush(x *Xfid, f *Fid) *Xfid {
func fsysattach(x *Xfid, f *Fid) *Xfid {
if x.fcall.Uname != username {
var t plan9.Fcall
return respond(x, &t, Eperm)
return respond(x, &t, ErrPermission)
}
f.busy = true
f.open = false
Expand Down Expand Up @@ -333,7 +334,7 @@ func fsyswalk(x *Xfid, f *Fid) *Xfid {
for i = 0; i < len(x.fcall.Wname); i++ {
wname = x.fcall.Wname[i]
if (q.Type & plan9.QTDIR) == 0 {
err = Enotdir
err = ErrNotDir
break
}

Expand Down Expand Up @@ -427,7 +428,7 @@ func fsyswalk(x *Xfid, f *Fid) *Xfid {

// If we never incremented
if i == 0 && err == nil {
err = Eexist
err = ErrNotExist
}
if i == plan9.MAXWELEM {
err = fmt.Errorf("name too long")
Expand Down Expand Up @@ -485,12 +486,12 @@ func fsysopen(x *Xfid, f *Fid) *Xfid {

Deny:
var t plan9.Fcall
return respond(x, &t, Eperm)
return respond(x, &t, ErrPermission)
}

func fsyscreate(x *Xfid, f *Fid) *Xfid {
var t plan9.Fcall
return respond(x, &t, Eperm)
return respond(x, &t, ErrPermission)
}

//func idcmp (const void *a, const void *b) (int) {
Expand Down Expand Up @@ -589,7 +590,7 @@ func fsysclunk(x *Xfid, f *Fid) *Xfid {

func fsysremove(x *Xfid, f *Fid) *Xfid {
var t plan9.Fcall
return respond(x, &t, Eperm)
return respond(x, &t, ErrPermission)
}

func fsysstat(x *Xfid, f *Fid) *Xfid {
Expand All @@ -605,7 +606,7 @@ func fsysstat(x *Xfid, f *Fid) *Xfid {
func fsyswstat(x *Xfid, f *Fid) *Xfid {
var t plan9.Fcall

return respond(x, &t, Eperm)
return respond(x, &t, ErrPermission)
}

func newfid(fid uint32) *Fid {
Expand Down
22 changes: 11 additions & 11 deletions fsys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ func TestFSys(t *testing.T) {
}
defer fid.Close()
_, err = fid.Write([]byte("hello\n"))
if err == nil || err.Error() != Eperm.Error() {
t.Fatalf("write to editout returned %v; expected %v", err, Eperm)
if err == nil || err.Error() != ErrPermission.Error() {
t.Fatalf("write to editout returned %v; expected %v", err, ErrPermission)
}
})
t.Run("WriteEvent", func(t *testing.T) {
Expand All @@ -372,15 +372,15 @@ func TestFSys(t *testing.T) {
{nil, "Ml0 0 \n"},
{nil, "MX0 0 \n"},
{nil, "ML0 0 \nMl0 0 \n"},
{Ebadevent, "M\n"},
{Ebadevent, "ML\n"},
{Ebadevent, "ML0 \n"},
{Ebadevent, "MLA 0 \n"},
{Ebadevent, "ML0 A \n"},
{Ebadevent, "M40 0 \n"},
{Ebadevent, "ML9 9 \n"},
{Ebadevent, "MZ0 0 \n"},
{Ebadevent, "MZ0 0 \nML0 0 \n"}, // bad event followed by a good one
{ErrBadEvent, "M\n"},
{ErrBadEvent, "ML\n"},
{ErrBadEvent, "ML0 \n"},
{ErrBadEvent, "MLA 0 \n"},
{ErrBadEvent, "ML0 A \n"},
{ErrBadEvent, "M40 0 \n"},
{ErrBadEvent, "ML9 9 \n"},
{ErrBadEvent, "MZ0 0 \n"},
{ErrBadEvent, "MZ0 0 \nML0 0 \n"}, // bad event followed by a good one
}
for _, tc := range tt {
_, err = w.Write("event", []byte(tc.s))
Expand Down
59 changes: 31 additions & 28 deletions xfid.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import (

const Ctlsize = 5 * 12

var Edel = fmt.Errorf("deleted window")
var Ebadctl = fmt.Errorf("ill-formed control message")
var Ebadaddr = fmt.Errorf("bad address syntax")
var Eaddr = fmt.Errorf("address out of range")
var Einuse = fmt.Errorf("already in use")
var Ebadevent = fmt.Errorf("bad event syntax")
// Errors returned by file server.
var (
ErrDeletedWin = fmt.Errorf("deleted window")
ErrBadCtl = fmt.Errorf("ill-formed control message")
ErrBadAddr = fmt.Errorf("bad address syntax")
ErrAddrRange = fmt.Errorf("address out of range")
ErrInUse = fmt.Errorf("already in use")
ErrBadEvent = fmt.Errorf("bad event syntax")
)

func clampaddr(w *Window) {
if w.addr.q0 < 0 {
Expand Down Expand Up @@ -126,7 +129,7 @@ func xfidopen(x *Xfid) {
// modified during the operation, e.g. by |sort
if w.rdselfd != nil {
w.Unlock()
respond(x, &fc, Einuse)
respond(x, &fc, ErrInUse)
return
}
var err error
Expand Down Expand Up @@ -167,14 +170,14 @@ func xfidopen(x *Xfid) {
case QWeditout:
if editing == Inactive {
w.Unlock()
respond(x, &fc, Eperm)
respond(x, &fc, ErrPermission)
return
}
select {
case w.editoutlk <- true:
default:
w.Unlock()
respond(x, &fc, Einuse)
respond(x, &fc, ErrInUse)
return
}
w.wrselrange = Range{t.q1, t.q1}
Expand All @@ -188,7 +191,7 @@ func xfidopen(x *Xfid) {
select {
case editoutlk <- true:
default:
respond(x, &fc, Einuse)
respond(x, &fc, ErrInUse)
return
}
}
Expand Down Expand Up @@ -304,7 +307,7 @@ func xfidread(x *Xfid) {
w.Lock('F')
if w.col == nil {
w.Unlock()
respond(x, &fc, Edel)
respond(x, &fc, ErrDeletedWin)
return
}
off := x.fcall.Offset
Expand Down Expand Up @@ -345,7 +348,7 @@ func xfidread(x *Xfid) {
case QWdata:
// BUG: what should happen if q1 > q0?
if w.addr.q0 > w.body.Nc() {
respond(x, &fc, Eaddr)
respond(x, &fc, ErrAddrRange)
break
}
w.addr.q0 += xfidruneread(x, &w.body, (w.addr.q0), w.body.Nc())
Expand All @@ -354,7 +357,7 @@ func xfidread(x *Xfid) {
case QWxdata:
// BUG: what should happen if q1 > q0?
if w.addr.q0 > w.body.Nc() {
respond(x, &fc, Eaddr)
respond(x, &fc, ErrAddrRange)
break
}
w.addr.q0 += xfidruneread(x, &w.body, (w.addr.q0), (w.addr.q1))
Expand Down Expand Up @@ -437,7 +440,7 @@ func xfidwrite(x *Xfid) {
w.Lock(c)
if w.col == nil {
w.Unlock()
respond(x, &fc, Edel)
respond(x, &fc, ErrDeletedWin)
return
}
}
Expand Down Expand Up @@ -498,11 +501,11 @@ func xfidwrite(x *Xfid) {
a, eval, nb = address(false, t, w.limit, w.addr, 0, (len(r)),
func(q int) rune { return r[q] }, eval)
if nb < (len(r)) {
respond(x, &fc, Ebadaddr)
respond(x, &fc, ErrBadAddr)
break
}
if !eval {
respond(x, &fc, Eaddr)
respond(x, &fc, ErrAddrRange)
break
}
w.addr = a
Expand Down Expand Up @@ -544,7 +547,7 @@ func xfidwrite(x *Xfid) {
t = &w.body
w.Commit(t)
if a.q0 > t.Nc() || a.q1 > t.Nc() {
respond(x, &fc, Eaddr)
respond(x, &fc, ErrAddrRange)
break
}
r, _, _ := cvttorunes(x.fcall.Data, int(x.fcall.Count))
Expand Down Expand Up @@ -638,7 +641,7 @@ forloop:
t.Show(t.q0, t.q1, true)
case "name": // set file name
if len(words) < 2 {
err = Ebadctl
err = ErrBadCtl
break forloop
}
r, _, nulls := cvttorunes([]byte(words[1]), len(words[1]))
Expand All @@ -657,7 +660,7 @@ forloop:
w.SetName(string(r))
case "dump": // set dump string
if len(words) < 2 {
err = Ebadctl
err = ErrBadCtl
break forloop
}
r, _, nulls := cvttorunes([]byte(words[1]), len(words[1]))
Expand All @@ -668,7 +671,7 @@ forloop:
w.dumpstr = string(r)
case "dumpdir": // set dump directory
if len(words) < 2 {
err = Ebadctl
err = ErrBadCtl
break forloop
}
r, _, nulls := cvttorunes([]byte(words[1]), len(words[1]))
Expand Down Expand Up @@ -719,7 +722,7 @@ forloop:
settag = true

default:
err = Ebadctl
err = ErrBadCtl
break forloop
}
}
Expand Down Expand Up @@ -769,26 +772,26 @@ forloop:
continue
}
if len(events) < 2 {
err = Ebadevent
err = ErrBadEvent
break
}
w.owner = int(events[0])
c := events[1]
words := strings.Fields(events[2:])
if len(words) < 2 {
err = Ebadevent
err = ErrBadEvent
break
}
var num int64
num, err = strconv.ParseInt(words[0], 10, 32)
if err != nil {
err = Ebadevent
err = ErrBadEvent
break
}
q0 := int(num)
num, err = strconv.ParseInt(words[1], 10, 32)
if err != nil {
err = Ebadevent
err = ErrBadEvent
break
}
q1 := int(num)
Expand All @@ -800,11 +803,11 @@ forloop:
case 'A' <= c && c <= 'Z':
t = &w.body
default:
err = Ebadevent
err = ErrBadEvent
break forloop
}
if q0 > t.Nc() || q1 > t.Nc() || q0 > q1 {
err = Ebadevent
err = ErrBadEvent
break
}

Expand All @@ -816,7 +819,7 @@ forloop:
look3(t, q0, q1, true)
default:
row.lk.Unlock()
err = Ebadevent
err = ErrBadEvent
break forloop
}
row.lk.Unlock()
Expand Down

0 comments on commit 846af7b

Please sign in to comment.