Skip to content

Commit 68cc651

Browse files
authored
Merge pull request #475 from kolyshkin/error
Misc error reporting improvements
2 parents 818c6c1 + b5d37cd commit 68cc651

File tree

12 files changed

+115
-119
lines changed

12 files changed

+115
-119
lines changed

daemon/watchdog.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package daemon
1616

1717
import (
18+
"errors"
1819
"fmt"
1920
"os"
2021
"strconv"
@@ -51,10 +52,10 @@ func SdWatchdogEnabled(unsetEnvironment bool) (time.Duration, error) {
5152
}
5253
s, err := strconv.Atoi(wusec)
5354
if err != nil {
54-
return 0, fmt.Errorf("error converting WATCHDOG_USEC: %s", err)
55+
return 0, fmt.Errorf("error converting WATCHDOG_USEC: %w", err)
5556
}
5657
if s <= 0 {
57-
return 0, fmt.Errorf("error WATCHDOG_USEC must be a positive number")
58+
return 0, errors.New("error WATCHDOG_USEC must be a positive number")
5859
}
5960
interval := time.Duration(s) * time.Microsecond
6061

@@ -63,7 +64,7 @@ func SdWatchdogEnabled(unsetEnvironment bool) (time.Duration, error) {
6364
}
6465
p, err := strconv.Atoi(wpid)
6566
if err != nil {
66-
return 0, fmt.Errorf("error converting WATCHDOG_PID: %s", err)
67+
return 0, fmt.Errorf("error converting WATCHDOG_PID: %w", err)
6768
}
6869
if os.Getpid() != p {
6970
return 0, nil

import1/dbus.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ func (c *Conn) ListTransfers() ([]TransferStatus, error) {
177177

178178
transfers := make([]TransferStatus, 0)
179179
for _, v := range result {
180-
transfer, err := transferFromInterfaces(v)
181-
if err != nil {
182-
return nil, err
180+
transfer := transferFromInterfaces(v)
181+
if transfer == nil {
182+
return nil, fmt.Errorf("ListTransfers: can't parse response %+v", v)
183183
}
184184
transfers = append(transfers, *transfer)
185185
}
@@ -192,48 +192,48 @@ func (c *Conn) CancelTransfer(transfer_id uint32) error {
192192
return c.object.Call(dbusInterface+".CancelTransfer", 0, transfer_id).Err
193193
}
194194

195-
func transferFromInterfaces(transfer []any) (*TransferStatus, error) {
195+
func transferFromInterfaces(transfer []any) *TransferStatus {
196196
// Verify may be not defined in response.
197197
if len(transfer) < 5 {
198-
return nil, fmt.Errorf("invalid number of transfer fields: %d", len(transfer))
198+
return nil
199199
}
200200

201201
ok := false
202202
ret := &TransferStatus{}
203203

204204
ret.Id, ok = transfer[0].(uint32)
205205
if !ok {
206-
return nil, fmt.Errorf("failed to typecast transfer field 0 to uint32")
206+
return nil
207207
}
208208
ret.Local, ok = transfer[1].(string)
209209
if !ok {
210-
return nil, fmt.Errorf("failed to typecast transfer field 1 to string")
210+
return nil
211211
}
212212
ret.Remote, ok = transfer[2].(string)
213213
if !ok {
214-
return nil, fmt.Errorf("failed to typecast transfer field 2 to string")
214+
return nil
215215
}
216216
ret.Type, ok = transfer[3].(string)
217217
if !ok {
218-
return nil, fmt.Errorf("failed to typecast transfer field 3 to string")
218+
return nil
219219
}
220220
// Verify is only defined for download operations.
221221
// If operation is not pull, we should ignore Verify field.
222222
if !strings.HasPrefix(ret.Type, "pull-") {
223223
ret.Progress, ok = transfer[4].(float64)
224224
if !ok {
225-
return nil, fmt.Errorf("failed to typecast transfer field 4 to float64")
225+
return nil
226226
}
227-
return ret, nil
227+
return ret
228228
}
229229

230230
ret.Verify, ok = transfer[4].(string)
231231
if !ok {
232-
return nil, fmt.Errorf("failed to typecast transfer field 4 to string")
232+
return nil
233233
}
234234
ret.Progress, ok = transfer[5].(float64)
235235
if !ok {
236-
return nil, fmt.Errorf("failed to typecast transfer field 5 to float64")
236+
return nil
237237
}
238-
return ret, nil
238+
return ret
239239
}

internal/dlopen/dlopen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (l *LibHandle) GetSymbolPointer(symbol string) (unsafe.Pointer, error) {
6969
p := C.dlsym(l.Handle, sym)
7070
e := C.dlerror()
7171
if e != nil {
72-
return nil, fmt.Errorf("error resolving symbol %q: %v", symbol, errors.New(C.GoString(e)))
72+
return nil, fmt.Errorf("error resolving symbol %q: %s", symbol, C.GoString(e))
7373
}
7474

7575
return p, nil
@@ -85,7 +85,7 @@ func (l *LibHandle) Close() error {
8585
C.dlclose(l.Handle)
8686
e := C.dlerror()
8787
if e != nil {
88-
return fmt.Errorf("error closing %v: %v", l.Libname, errors.New(C.GoString(e)))
88+
return fmt.Errorf("error closing %v: %s", l.Libname, C.GoString(e))
8989
}
9090

9191
return nil

internal/dlopen/dlopen_example.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ package dlopen
3030
import "C"
3131

3232
import (
33-
"fmt"
3433
"unsafe"
3534
)
3635

3736
func strlen(libs []string, s string) (int, error) {
3837
h, err := GetHandle(libs)
3938
if err != nil {
40-
return -1, fmt.Errorf(`couldn't get a handle to the library: %v`, err)
39+
return -1, err
4140
}
4241
defer h.Close()
4342

@@ -47,7 +46,7 @@ func strlen(libs []string, s string) (int, error) {
4746

4847
strlen, err := h.GetSymbolPointer(f)
4948
if err != nil {
50-
return -1, fmt.Errorf(`couldn't get symbol %q: %v`, f, err)
49+
return -1, err
5150
}
5251

5352
len := C.my_strlen(strlen, cs)

journal/journal_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func fdIsJournalStream(fd int) (bool, error) {
106106
var expectedStat syscall.Stat_t
107107
_, err := fmt.Sscanf(journalStream, "%d:%d", &expectedStat.Dev, &expectedStat.Ino)
108108
if err != nil {
109-
return false, fmt.Errorf("failed to parse JOURNAL_STREAM=%q: %v", journalStream, err)
109+
return false, fmt.Errorf("failed to parse JOURNAL_STREAM=%q: %w", journalStream, err)
110110
}
111111

112112
var stat syscall.Stat_t

login1/dbus.go

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package login1
1717

1818
import (
1919
"context"
20+
"errors"
2021
"fmt"
2122
"os"
2223
"strconv"
@@ -111,54 +112,52 @@ type User struct {
111112
Path dbus.ObjectPath
112113
}
113114

114-
func sessionFromInterfaces(session []any) (*Session, error) {
115+
func sessionFromInterfaces(session []any) *Session {
115116
if len(session) < 5 {
116-
return nil, fmt.Errorf("invalid number of session fields: %d", len(session))
117+
return nil
117118
}
118119
id, ok := session[0].(string)
119120
if !ok {
120-
return nil, fmt.Errorf("failed to typecast session field 0 to string")
121+
return nil
121122
}
122123
uid, ok := session[1].(uint32)
123124
if !ok {
124-
return nil, fmt.Errorf("failed to typecast session field 1 to uint32")
125+
return nil
125126
}
126127
user, ok := session[2].(string)
127128
if !ok {
128-
return nil, fmt.Errorf("failed to typecast session field 2 to string")
129+
return nil
129130
}
130131
seat, ok := session[3].(string)
131132
if !ok {
132-
return nil, fmt.Errorf("failed to typecast session field 2 to string")
133+
return nil
133134
}
134135
path, ok := session[4].(dbus.ObjectPath)
135136
if !ok {
136-
return nil, fmt.Errorf("failed to typecast session field 4 to ObjectPath")
137+
return nil
137138
}
138139

139-
ret := Session{ID: id, UID: uid, User: user, Seat: seat, Path: path}
140-
return &ret, nil
140+
return &Session{ID: id, UID: uid, User: user, Seat: seat, Path: path}
141141
}
142142

143-
func userFromInterfaces(user []any) (*User, error) {
143+
func userFromInterfaces(user []any) *User {
144144
if len(user) < 3 {
145-
return nil, fmt.Errorf("invalid number of user fields: %d", len(user))
145+
return nil
146146
}
147147
uid, ok := user[0].(uint32)
148148
if !ok {
149-
return nil, fmt.Errorf("failed to typecast user field 0 to uint32")
149+
return nil
150150
}
151151
name, ok := user[1].(string)
152152
if !ok {
153-
return nil, fmt.Errorf("failed to typecast session field 1 to string")
153+
return nil
154154
}
155155
path, ok := user[2].(dbus.ObjectPath)
156156
if !ok {
157-
return nil, fmt.Errorf("failed to typecast user field 2 to ObjectPath")
157+
return nil
158158
}
159159

160-
ret := User{UID: uid, Name: name, Path: path}
161-
return &ret, nil
160+
return &User{UID: uid, Name: name, Path: path}
162161
}
163162

164163
// GetActiveSession may be used to get the session object path for the current active session
@@ -175,20 +174,20 @@ func (c *Conn) GetActiveSession() (dbus.ObjectPath, error) {
175174
}
176175
activeSessionMap, ok := activeSession.Value().([]any)
177176
if !ok || len(activeSessionMap) < 2 {
178-
return "", fmt.Errorf("failed to typecast active session map")
177+
return "", fmt.Errorf("GetActiveSession: can't parse response %+v", activeSession)
179178
}
180179

181180
activeSessionPath, ok := activeSessionMap[1].(dbus.ObjectPath)
182181
if !ok {
183-
return "", fmt.Errorf("failed to typecast dbus active session Path")
182+
return "", fmt.Errorf("GetActiveSession: can't parse response %+v", activeSessionMap)
184183
}
185184
return activeSessionPath, nil
186185
}
187186

188187
// GetSessionUser may be used to get the user of specific session
189188
func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
190189
if len(sessionPath) == 0 {
191-
return nil, fmt.Errorf("empty sessionPath")
190+
return nil, errors.New("empty sessionPath")
192191
}
193192

194193
activeSessionObj := c.conn.Object(dbusDest, sessionPath)
@@ -203,19 +202,19 @@ func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
203202
}
204203
dbusUser, ok := sessionUser.Value().([]any)
205204
if !ok {
206-
return nil, fmt.Errorf("failed to typecast dbus session user")
205+
return nil, fmt.Errorf("GetSessionUser: can't parse response %+v", sessionUser)
207206
}
208207

209208
if len(dbusUser) < 2 {
210-
return nil, fmt.Errorf("invalid number of user fields: %d", len(dbusUser))
209+
return nil, fmt.Errorf("GetSessionUser: can't parse response %+v", dbusUser)
211210
}
212211
uid, ok := dbusUser[0].(uint32)
213212
if !ok {
214-
return nil, fmt.Errorf("failed to typecast user field 0 to uint32")
213+
return nil, fmt.Errorf("GetSessionUser: can't parse response %+v", dbusUser)
215214
}
216215
path, ok := dbusUser[1].(dbus.ObjectPath)
217216
if !ok {
218-
return nil, fmt.Errorf("failed to typecast user field 1 to ObjectPath")
217+
return nil, fmt.Errorf("GetSessionUser: can't parse response %+v", dbusUser)
219218
}
220219

221220
user := User{UID: uid, Name: strings.Trim(sessionUserName.String(), "\""), Path: path}
@@ -226,7 +225,7 @@ func (c *Conn) GetSessionUser(sessionPath dbus.ObjectPath) (*User, error) {
226225
// GetSessionDisplay may be used to get the display for specific session
227226
func (c *Conn) GetSessionDisplay(sessionPath dbus.ObjectPath) (string, error) {
228227
if len(sessionPath) == 0 {
229-
return "", fmt.Errorf("empty sessionPath")
228+
return "", errors.New("empty sessionPath")
230229
}
231230
sessionObj := c.conn.Object(dbusDest, sessionPath)
232231
display, err := sessionObj.GetProperty(dbusDest + ".Session.Display")
@@ -246,7 +245,7 @@ func (c *Conn) GetSession(id string) (dbus.ObjectPath, error) {
246245

247246
ret, ok := out.(dbus.ObjectPath)
248247
if !ok {
249-
return "", fmt.Errorf("failed to typecast session to ObjectPath")
248+
return "", fmt.Errorf("GetSession: can't parse response %+v", out)
250249
}
251250

252251
return ret, nil
@@ -266,9 +265,9 @@ func (c *Conn) ListSessionsContext(ctx context.Context) ([]Session, error) {
266265

267266
ret := []Session{}
268267
for _, el := range out {
269-
session, err := sessionFromInterfaces(el)
270-
if err != nil {
271-
return nil, err
268+
session := sessionFromInterfaces(el)
269+
if session == nil {
270+
return nil, fmt.Errorf("ListSessions: can't parse response %+v", el)
272271
}
273272
ret = append(ret, *session)
274273
}
@@ -289,9 +288,9 @@ func (c *Conn) ListUsersContext(ctx context.Context) ([]User, error) {
289288

290289
ret := []User{}
291290
for _, el := range out {
292-
user, err := userFromInterfaces(el)
293-
if err != nil {
294-
return nil, err
291+
user := userFromInterfaces(el)
292+
if user == nil {
293+
return nil, fmt.Errorf("ListUsers: can't parse response %+v", el)
295294
}
296295
ret = append(ret, *user)
297296
}

0 commit comments

Comments
 (0)