summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2023-07-01 22:02:01 +0800
committerKeuin <[email protected]>2023-07-01 22:02:01 +0800
commit5aba05d7237c2250e647a717f8abef658f30a9e9 (patch)
tree6fe77f8a9e4b043365bbc993990cdf2a34447039
parent40e0b3172af1e8deab80a7bf2bf95337ef9201c6 (diff)
Refactor: replace homemade error type checking with idiom Go practices.
-rw-r--r--bilibili/request.go6
-rw-r--r--common/errors.go25
-rw-r--r--recording/runner.go2
3 files changed, 3 insertions, 30 deletions
diff --git a/bilibili/request.go b/bilibili/request.go
index feb2b8f..2e4b57e 100644
--- a/bilibili/request.go
+++ b/bilibili/request.go
@@ -2,7 +2,6 @@ package bilibili
import (
"encoding/json"
- "github.com/keuin/slbr/common"
"io"
"net"
"net/http"
@@ -77,9 +76,8 @@ func (b Bilibili) Do(req *http.Request) (resp *http.Response, err error) {
transport.DialContext = netCtx
b.http.Transport = transport
resp, err = b.http.Do(req)
-
- isOpErr := common.IsErrorOfType(err, &net.OpError{})
- isAddrErr := common.IsErrorOfType(err, &net.AddrError{})
+ _, isOpErr := err.(*net.OpError)
+ _, isAddrErr := err.(*net.AddrError)
if err == nil || !isOpErr || !isAddrErr {
// return the first success request
b.logger.Info("Request success with network %v.", typeName)
diff --git a/common/errors.go b/common/errors.go
index 62899c1..bdf49fb 100644
--- a/common/errors.go
+++ b/common/errors.go
@@ -1,34 +1,9 @@
package common
import (
- "errors"
"fmt"
- "reflect"
)
-// IsErrorOfType is a modified version of errors.Is, which loosen the check condition
-func IsErrorOfType(err, target error) bool {
- if target == nil {
- return err == target
- }
-
- isComparable := reflect.TypeOf(target).Comparable()
- for {
- if isComparable && reflect.TypeOf(target) == reflect.TypeOf(err) {
- return true
- }
- if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
- return true
- }
- // TODO: consider supporting target.Is(err). This would allow
- // user-definable predicates, but also may allow for coping with sloppy
- // APIs, thereby making it easier to get away with them.
- if err = errors.Unwrap(err); err == nil {
- return false
- }
- }
-}
-
/*
Task errors.
*/
diff --git a/recording/runner.go b/recording/runner.go
index 4145d5f..8f892fa 100644
--- a/recording/runner.go
+++ b/recording/runner.go
@@ -323,7 +323,7 @@ func record(
logger.Info("Recording live stream to file \"%v\"...", filePath)
return
}, writeBufferSize)
- if common.IsErrorOfType(err, &common.UnrecoverableTaskError{}) {
+ if _, ok := err.(*common.UnrecoverableTaskError); ok {
logger.Error("Cannot record: %v", err)
return err
} else if errors.Is(err, context.Canceled) || err == nil {