Skip to content

Commit

Permalink
fix: fix uncaught insufficient permission error
Browse files Browse the repository at this point in the history
Switching over errors does not work with wrapped errors. Prefer the more
verbose errors.Is instead.
  • Loading branch information
Tshaka Eric Lekholoane authored and tshakalekholoane committed Oct 20, 2022
1 parent fb6857d commit be5f816
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
23 changes: 12 additions & 11 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,17 @@ func (a *app) capacity() { a.show(power.Capacity) }

func (a *app) persist() {
if err := a.systemder.Write(); err != nil {
switch err {
case systemd.ErrBashNotFound:
switch {
case errors.Is(err, systemd.ErrBashNotFound):
a.errorln(msgBashNotFound)
return
case systemd.ErrIncompatSystemd:
case errors.Is(err, systemd.ErrIncompatSystemd):
a.errorln(msgIncompatibleSystemd)
return
case power.ErrNotFound:
case errors.Is(err, power.ErrNotFound):
a.errorln(msgIncompatible)
return
case syscall.EACCES:
case errors.Is(err, syscall.EACCES):
a.errorln(msgPermissionDenied)
return
default:
Expand Down Expand Up @@ -269,11 +269,11 @@ func (a *app) threshold(args []string) {
}

if err := a.set(power.Threshold, strings.TrimSpace(val)); err != nil {
switch err {
case power.ErrNotFound:
switch {
case errors.Is(err, power.ErrNotFound):
a.errorln(msgIncompatible)
return
case syscall.EACCES:
case errors.Is(err, syscall.EACCES):
a.errorln(msgPermissionDenied)
return
default:
Expand All @@ -294,9 +294,10 @@ func Run() {
out: os.Stdout,
quit: os.Exit,
},
pager: "less",
get: power.Get,
set: power.Set,
pager: "less",
get: power.Get,
set: power.Set,
systemder: systemd.New(),
}

if len(os.Args) == 1 {
Expand Down
7 changes: 4 additions & 3 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"bytes"
"fmt"
"io/fs"
"os/exec"
"syscall"
"testing"
Expand Down Expand Up @@ -217,7 +218,7 @@ func TestPersist(t *testing.T) {
{systemd.ErrBashNotFound, msgBashNotFound, failure},
{systemd.ErrIncompatSystemd, msgIncompatibleSystemd, failure},
{power.ErrNotFound, msgIncompatible, failure},
{syscall.EACCES, msgPermissionDenied, failure},
{&fs.PathError{Err: syscall.EACCES}, msgPermissionDenied, failure},
}

for _, test := range tests {
Expand Down Expand Up @@ -261,7 +262,7 @@ func TestReset(t *testing.T) {
code int
}{
{nil, msgPersistenceReset, success},
{syscall.EACCES, msgPermissionDenied, failure},
{&fs.PathError{Err: syscall.EACCES}, msgPermissionDenied, failure},
}

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

for _, test := range tests {
Expand Down

0 comments on commit be5f816

Please sign in to comment.