-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
49 lines (45 loc) · 1.64 KB
/
Copy patherrors.go
File metadata and controls
49 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Package usc provides shared error descriptions for USC service clients.
// Service implementations live in the auth, brightspace, classes, handshake,
// libcal, and browser subpackages.
package usc
import (
"errors"
"github.com/nsigel/usc-cli/auth"
"github.com/nsigel/usc-cli/brightspace"
"github.com/nsigel/usc-cli/handshake"
"github.com/nsigel/usc-cli/libcal"
"github.com/nsigel/usc-cli/site"
)
// ErrorInfo is a JSON-ready error description with optional recovery guidance.
type ErrorInfo struct {
Error string `json:"error"`
Code string `json:"code,omitempty"`
Action string `json:"action,omitempty"`
}
// DescribeError classifies existing errors without retrying or authenticating.
// The site name supplies context for shared auth errors; an empty name uses
// the default login command. A nil error returns an empty description.
func DescribeError(err error, name site.Name) ErrorInfo {
if err == nil {
return ErrorInfo{}
}
result := ErrorInfo{Error: err.Error()}
var coded interface{ ErrorCode() string }
if errors.As(err, &coded) {
result.Code = coded.ErrorCode()
}
switch {
case errors.Is(err, handshake.ErrSessionInvalid):
result.Action = "usc auth login handshake"
case errors.Is(err, libcal.ErrAuthenticationRequired):
result.Action = "usc auth login libcal"
case errors.Is(err, brightspace.ErrSessionInvalid):
result.Action = "usc auth login"
case errors.Is(err, auth.ErrCredentialsRequired), errors.Is(err, auth.ErrBypassRejected), errors.Is(err, auth.ErrSessionNotFound):
result.Action = "usc auth login"
if name == site.Handshake || name == site.LibCal {
result.Action += " " + string(name)
}
}
return result
}