-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
errors.go
58 lines (51 loc) · 1.41 KB
/
errors.go
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
50
51
52
53
54
55
56
57
58
package playwright
import (
"errors"
"fmt"
)
var (
// ErrPlaywright wraps all Playwright errors.
// - Use errors.Is to check if the error is a Playwright error.
// - Use errors.As to cast an error to [Error] if you want to access "Stack".
ErrPlaywright = errors.New("playwright")
// ErrTargetClosed usually wraps a reason.
ErrTargetClosed = errors.New("target closed")
// ErrTimeout wraps timeout errors. It can be either Playwright TimeoutError or client timeout.
ErrTimeout = errors.New("timeout")
)
// Error represents a Playwright error
type Error struct {
Name string `json:"name"`
Message string `json:"message"`
Stack string `json:"stack"`
}
func (e *Error) Error() string {
return e.Message
}
func (e *Error) Is(target error) bool {
err, ok := target.(*Error)
if !ok {
return false
}
if err.Name != e.Name {
return false
}
if e.Name != "Error" {
return true // same name and not normal error
}
return e.Message == err.Message
}
func parseError(err Error) error {
if err.Name == "TimeoutError" {
return fmt.Errorf("%w: %w: %w", ErrPlaywright, ErrTimeout, &err)
} else if err.Name == "TargetClosedError" {
return fmt.Errorf("%w: %w: %w", ErrPlaywright, ErrTargetClosed, &err)
}
return fmt.Errorf("%w: %w", ErrPlaywright, &err)
}
func targetClosedError(reason *string) error {
if reason == nil {
return ErrTargetClosed
}
return fmt.Errorf("%w: %s", ErrTargetClosed, *reason)
}