Skip to content

Commit be5f816

Browse files
Tshaka Eric Lekholoanetshakalekholoane
authored andcommitted
fix: fix uncaught insufficient permission error
Switching over errors does not work with wrapped errors. Prefer the more verbose errors.Is instead.
1 parent fb6857d commit be5f816

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

internal/cli/cli.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ func (a *app) capacity() { a.show(power.Capacity) }
158158

159159
func (a *app) persist() {
160160
if err := a.systemder.Write(); err != nil {
161-
switch err {
162-
case systemd.ErrBashNotFound:
161+
switch {
162+
case errors.Is(err, systemd.ErrBashNotFound):
163163
a.errorln(msgBashNotFound)
164164
return
165-
case systemd.ErrIncompatSystemd:
165+
case errors.Is(err, systemd.ErrIncompatSystemd):
166166
a.errorln(msgIncompatibleSystemd)
167167
return
168-
case power.ErrNotFound:
168+
case errors.Is(err, power.ErrNotFound):
169169
a.errorln(msgIncompatible)
170170
return
171-
case syscall.EACCES:
171+
case errors.Is(err, syscall.EACCES):
172172
a.errorln(msgPermissionDenied)
173173
return
174174
default:
@@ -269,11 +269,11 @@ func (a *app) threshold(args []string) {
269269
}
270270

271271
if err := a.set(power.Threshold, strings.TrimSpace(val)); err != nil {
272-
switch err {
273-
case power.ErrNotFound:
272+
switch {
273+
case errors.Is(err, power.ErrNotFound):
274274
a.errorln(msgIncompatible)
275275
return
276-
case syscall.EACCES:
276+
case errors.Is(err, syscall.EACCES):
277277
a.errorln(msgPermissionDenied)
278278
return
279279
default:
@@ -294,9 +294,10 @@ func Run() {
294294
out: os.Stdout,
295295
quit: os.Exit,
296296
},
297-
pager: "less",
298-
get: power.Get,
299-
set: power.Set,
297+
pager: "less",
298+
get: power.Get,
299+
set: power.Set,
300+
systemder: systemd.New(),
300301
}
301302

302303
if len(os.Args) == 1 {

internal/cli/cli_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"bytes"
55
"fmt"
6+
"io/fs"
67
"os/exec"
78
"syscall"
89
"testing"
@@ -217,7 +218,7 @@ func TestPersist(t *testing.T) {
217218
{systemd.ErrBashNotFound, msgBashNotFound, failure},
218219
{systemd.ErrIncompatSystemd, msgIncompatibleSystemd, failure},
219220
{power.ErrNotFound, msgIncompatible, failure},
220-
{syscall.EACCES, msgPermissionDenied, failure},
221+
{&fs.PathError{Err: syscall.EACCES}, msgPermissionDenied, failure},
221222
}
222223

223224
for _, test := range tests {
@@ -261,7 +262,7 @@ func TestReset(t *testing.T) {
261262
code int
262263
}{
263264
{nil, msgPersistenceReset, success},
264-
{syscall.EACCES, msgPermissionDenied, failure},
265+
{&fs.PathError{Err: syscall.EACCES}, msgPermissionDenied, failure},
265266
}
266267

267268
for _, test := range tests {
@@ -310,7 +311,7 @@ func TestThreshold(t *testing.T) {
310311
{[]string{"bat", "threshold", "80.0"}, failure, nil, msgArgNotInt},
311312
{[]string{"bat", "threshold", "101"}, failure, nil, msgOutOfRangeThresholdVal},
312313
{[]string{"bat", "threshold", "80"}, failure, power.ErrNotFound, msgIncompatible},
313-
{[]string{"bat", "threshold", "80"}, failure, syscall.EACCES, msgPermissionDenied},
314+
{[]string{"bat", "threshold", "80"}, failure, &fs.PathError{Err: syscall.EACCES}, msgPermissionDenied},
314315
}
315316

316317
for _, test := range tests {

0 commit comments

Comments
 (0)