-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patherrors.go
More file actions
167 lines (135 loc) · 4.08 KB
/
errors.go
File metadata and controls
167 lines (135 loc) · 4.08 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) Liam Stanley <liam@liam.sh>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
//nolint:errname
package ytdlp
import (
"errors"
"fmt"
"os/exec"
"strings"
)
func wrapError(r *Result, err error) (*Result, error) {
if err == nil {
return r, nil
}
if r == nil {
return nil, &ErrUnknown{wrapped: err}
}
err = r.decorateError(err)
if errors.Is(err, exec.ErrDot) || errors.Is(err, exec.ErrNotFound) {
return r, &ErrMisconfig{wrapped: err, result: r}
}
if r.ExitCode != 0 {
return r, &ErrExitCode{wrapped: err, result: r}
}
if strings.Contains(r.Stderr, "error: no such option") {
return r, &ErrParsing{wrapped: err, result: r}
}
return r, &ErrUnknown{wrapped: err}
}
// ErrExitCode is returned when the exit code of the yt-dlp process is non-zero.
type ErrExitCode struct {
wrapped error
result *Result
}
func (e *ErrExitCode) Unwrap() error {
return e.wrapped
}
func (e *ErrExitCode) Error() string {
return fmt.Sprintf("exit code %d: %s", e.result.ExitCode, e.wrapped)
}
// IsExitCodeError returns true when the exit code of the yt-dlp process is non-zero.
func IsExitCodeError(err error) (*ErrExitCode, bool) {
var e *ErrExitCode
return e, errors.As(err, &e) //nolint:gocritic
}
// ErrMisconfig is returned when the yt-dlp executable is not found, or is not
// configured properly.
type ErrMisconfig struct {
wrapped error
result *Result
}
func (e *ErrMisconfig) Unwrap() error {
return e.wrapped
}
func (e *ErrMisconfig) Error() string {
return fmt.Sprintf("misconfiguration error (executable: %q): %s", e.result.Executable, e.wrapped)
}
// IsMisconfigError returns true when the yt-dlp executable is not found, or is not
// configured properly.
func IsMisconfigError(err error) (*ErrMisconfig, bool) {
var e *ErrMisconfig
return e, errors.As(err, &e) //nolint:gocritic
}
// ErrParsing is returned when the yt-dlp process fails due to an invalid flag or
// argument, possibly due to a version mismatch or go-ytdlp bug.
type ErrParsing struct {
wrapped error
result *Result
}
func (e *ErrParsing) Unwrap() error {
return e.wrapped
}
func (e *ErrParsing) Error() string {
return fmt.Sprintf(
"parsing error (yt-dlp version might be too different, go-ytdlp version built with yt-dlp %s/%s): %s",
Channel,
Version,
e.wrapped,
)
}
// IsParsingError returns true when the yt-dlp process fails due to an invalid flag or
// argument, possibly due to a version mismatch or go-ytdlp bug.
func IsParsingError(err error) (*ErrParsing, bool) {
var e *ErrParsing
return e, errors.As(err, &e) //nolint:gocritic
}
// ErrUnknown is returned when the error is unknown according to go-ytdlp.
type ErrUnknown struct {
wrapped error
}
func (e *ErrUnknown) Unwrap() error {
return e.wrapped
}
func (e *ErrUnknown) Error() string {
return e.wrapped.Error()
}
// IsUnknownError returns true when the error is unknown according to go-ytdlp.
func IsUnknownError(err error) (*ErrUnknown, bool) {
var e *ErrUnknown
return e, errors.As(err, &e) //nolint:gocritic
}
type ErrJSONParsingFlag struct {
ID string `json:"id,omitempty"`
JSONPath string `json:"json_path,omitempty"`
Flag string `json:"flag,omitempty"`
Err error `json:"error,omitempty"`
}
func (e *ErrJSONParsingFlag) Unwrap() error {
return e.Err
}
func (e *ErrJSONParsingFlag) Error() string {
return fmt.Sprintf(
"error while parsing json at path %q (flag: %q, id: %q): %s",
e.JSONPath,
e.Flag,
e.ID,
e.Err,
)
}
// IsJSONParsingFlagError returns true when the error is a JSON parsing error.
func IsJSONParsingFlagError(err error) (*ErrJSONParsingFlag, bool) {
var e *ErrJSONParsingFlag
return e, errors.As(err, &e) //nolint:gocritic
}
type ErrMultipleJSONParsingFlags struct {
Errors []*ErrJSONParsingFlag `json:"errors,omitempty"`
}
func (e *ErrMultipleJSONParsingFlags) Error() string {
return fmt.Sprintf("multiple errors while parsing json: %s", e.Errors)
}
func IsMultipleJSONParsingFlagsError(err error) (*ErrMultipleJSONParsingFlags, bool) {
var e *ErrMultipleJSONParsingFlags
return e, errors.As(err, &e) //nolint:gocritic
}