From 6f7987593a9db5500fa56fef0ec1845a02df9876 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 2 Jul 2023 14:52:06 +0800 Subject: Refactor: move task error types to separate package. --- bilibili/errors/errors.go | 43 +++++++++++++++++++++++++++++++++++++++++++ bilibili/streaming.go | 3 ++- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 bilibili/errors/errors.go (limited to 'bilibili') diff --git a/bilibili/errors/errors.go b/bilibili/errors/errors.go new file mode 100644 index 0000000..2b810ed --- /dev/null +++ b/bilibili/errors/errors.go @@ -0,0 +1,43 @@ +package errors + +import ( + "fmt" +) + +/* +Task errors. +*/ + +type RecoverableTaskError struct { + err error + message string +} + +func (e *RecoverableTaskError) Error() string { + return fmt.Sprintf("%v: %v", e.message, e.err) +} + +func (e *RecoverableTaskError) Unwrap() error { + return e.err +} + +func NewRecoverableTaskError(message string, err error) error { + return &RecoverableTaskError{message: message, err: err} +} + +type UnrecoverableTaskError struct { + err error + message string +} + +func (e *UnrecoverableTaskError) Error() string { + return fmt.Sprintf("%v: %v", e.message, e.err) +} + +func (e *UnrecoverableTaskError) Unwrap() error { + return e.err +} + +func NewUnrecoverableTaskError(message string, err error) error { + return &UnrecoverableTaskError{message: message, err: err} +} diff --git a/bilibili/streaming.go b/bilibili/streaming.go index 8f6c491..d081a8f 100644 --- a/bilibili/streaming.go +++ b/bilibili/streaming.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + errors2 "github.com/keuin/slbr/bilibili/errors" "github.com/keuin/slbr/common" "io" "net/http" @@ -70,7 +71,7 @@ func (b Bilibili) CopyLiveStream( out, err = fileCreator() if err != nil { b.logger.Error("Cannot open file for writing: %v", err) - err = common.NewUnrecoverableTaskError("failed to create file", err) + err = errors2.NewUnrecoverableTaskError("failed to create file", err) return } _, err = out.Write(initBytes) -- cgit v1.2.3