refactor: rename module 'err' to 'gperr' in references

This commit is contained in:
yusing
2025-03-28 06:26:36 +08:00
parent e4f6994dfc
commit 361931ed96
38 changed files with 242 additions and 267 deletions

View File

@@ -6,7 +6,7 @@ import (
"net/url"
"strings"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/gperr"
)
type ProviderBase struct {
@@ -16,12 +16,12 @@ type ProviderBase struct {
}
var (
ErrMissingToken = E.New("token is required")
ErrURLMissingScheme = E.New("url missing scheme, expect 'http://' or 'https://'")
ErrMissingToken = gperr.New("token is required")
ErrURLMissingScheme = gperr.New("url missing scheme, expect 'http://' or 'https://'")
)
// Validate implements the utils.CustomValidator interface.
func (base *ProviderBase) Validate() E.Error {
func (base *ProviderBase) Validate() gperr.Error {
if base.Token == "" {
return ErrMissingToken
}
@@ -30,7 +30,7 @@ func (base *ProviderBase) Validate() E.Error {
}
u, err := url.Parse(base.URL)
if err != nil {
return E.Wrap(err)
return gperr.Wrap(err)
}
base.URL = u.String()
return nil
@@ -63,7 +63,7 @@ func (base *ProviderBase) SetHeaders(logMsg *LogMessage, headers http.Header) {
func (base *ProviderBase) makeRespError(resp *http.Response) error {
body, err := io.ReadAll(resp.Body)
if err == nil {
return E.Errorf("%s status %d: %s", base.Name, resp.StatusCode, body)
return gperr.Errorf("%s status %d: %s", base.Name, resp.StatusCode, body)
}
return E.Errorf("%s status %d", base.Name, resp.StatusCode)
return gperr.Errorf("%s status %d", base.Name, resp.StatusCode)
}

View File

@@ -1,7 +1,7 @@
package notif
import (
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/utils"
)
@@ -11,13 +11,13 @@ type NotificationConfig struct {
}
var (
ErrMissingNotifProvider = E.New("missing notification provider")
ErrInvalidNotifProviderType = E.New("invalid notification provider type")
ErrUnknownNotifProvider = E.New("unknown notification provider")
ErrMissingNotifProvider = gperr.New("missing notification provider")
ErrInvalidNotifProviderType = gperr.New("invalid notification provider type")
ErrUnknownNotifProvider = gperr.New("unknown notification provider")
)
// UnmarshalMap implements MapUnmarshaler.
func (cfg *NotificationConfig) UnmarshalMap(m map[string]any) (err E.Error) {
func (cfg *NotificationConfig) UnmarshalMap(m map[string]any) (err gperr.Error) {
// extract provider name
providerName := m["provider"]
switch providerName := providerName.(type) {

View File

@@ -2,7 +2,7 @@ package notif
import (
"github.com/rs/zerolog"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/task"
F "github.com/yusing/go-proxy/internal/utils/functional"
@@ -89,29 +89,15 @@ func (disp *Dispatcher) dispatch(msg *LogMessage) {
task := disp.task.Subtask("dispatcher")
defer task.Finish("notif dispatched")
errs := E.NewBuilder(dispatchErr)
errs := gperr.NewBuilder(dispatchErr)
disp.providers.RangeAllParallel(func(p Provider) {
if err := notifyProvider(task.Context(), p, msg); err != nil {
errs.Add(E.PrependSubject(p.GetName(), err))
errs.Add(gperr.PrependSubject(p.GetName(), err))
}
})
if errs.HasError() {
E.LogError(errs.About(), errs.Error())
gperr.LogError(errs.About(), errs.Error())
} else {
logging.Debug().Str("title", msg.Title).Msgf("dispatched notif")
}
}
// Run implements zerolog.Hook.
// func (disp *Dispatcher) Run(e *zerolog.Event, level zerolog.Level, message string) {
// if strings.HasPrefix(message, dispatchErr) { // prevent recursion
// return
// }
// switch level {
// case zerolog.WarnLevel, zerolog.ErrorLevel, zerolog.FatalLevel, zerolog.PanicLevel:
// disp.logCh <- &LogMessage{
// Level: level,
// Message: message,
// }
// }
// }

View File

@@ -7,7 +7,7 @@ import (
"strings"
"github.com/rs/zerolog"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/gperr"
)
// See https://docs.ntfy.sh/publish
@@ -24,22 +24,22 @@ const (
NtfyStylePlain NtfyStyle = "plain"
)
func (n *Ntfy) Validate() E.Error {
func (n *Ntfy) Validate() gperr.Error {
if n.URL == "" {
return E.New("url is required")
return gperr.New("url is required")
}
if n.Topic == "" {
return E.New("topic is required")
return gperr.New("topic is required")
}
if n.Topic[0] == '/' {
return E.New("topic should not start with a slash")
return gperr.New("topic should not start with a slash")
}
switch n.Style {
case "":
n.Style = NtfyStyleMarkdown
case NtfyStyleMarkdown, NtfyStylePlain:
default:
return E.Errorf("invalid style, expecting %q or %q, got %q", NtfyStyleMarkdown, NtfyStylePlain, n.Style)
return gperr.Errorf("invalid style, expecting %q or %q, got %q", NtfyStyleMarkdown, NtfyStylePlain, n.Style)
}
return nil
}

View File

@@ -6,8 +6,8 @@ import (
"net/http"
"time"
E "github.com/yusing/go-proxy/internal/error"
gphttp "github.com/yusing/go-proxy/internal/net/http"
"github.com/yusing/go-proxy/internal/gperr"
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
"github.com/yusing/go-proxy/internal/utils"
)
@@ -26,7 +26,7 @@ type (
makeRespError(resp *http.Response) error
}
ProviderCreateFunc func(map[string]any) (Provider, E.Error)
ProviderCreateFunc func(map[string]any) (Provider, gperr.Error)
ProviderConfig map[string]any
)
@@ -39,7 +39,7 @@ const (
func notifyProvider(ctx context.Context, provider Provider, msg *LogMessage) error {
body, err := provider.MakeBody(msg)
if err != nil {
return E.PrependSubject(provider.GetName(), err)
return gperr.PrependSubject(provider.GetName(), err)
}
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
@@ -52,7 +52,7 @@ func notifyProvider(ctx context.Context, provider Provider, msg *LogMessage) err
body,
)
if err != nil {
return E.PrependSubject(provider.GetName(), err)
return gperr.PrependSubject(provider.GetName(), err)
}
req.Header.Set("Content-Type", provider.GetMIMEType())
@@ -63,7 +63,7 @@ func notifyProvider(ctx context.Context, provider Provider, msg *LogMessage) err
resp, err := http.DefaultClient.Do(req)
if err != nil {
return E.PrependSubject(provider.GetName(), err)
return gperr.PrependSubject(provider.GetName(), err)
}
defer resp.Body.Close()

View File

@@ -8,7 +8,7 @@ import (
"net/http"
"strings"
E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/gperr"
)
type Webhook struct {
@@ -27,7 +27,7 @@ var webhookTemplates = map[string]string{
"discord": discordPayload,
}
func (webhook *Webhook) Validate() E.Error {
func (webhook *Webhook) Validate() gperr.Error {
if err := webhook.ProviderBase.Validate(); err != nil && !err.Is(ErrMissingToken) {
return err
}
@@ -37,16 +37,16 @@ func (webhook *Webhook) Validate() E.Error {
webhook.MIMEType = "application/json"
case "application/json", "application/x-www-form-urlencoded", "text/plain":
default:
return E.New("invalid mime_type, expect empty, 'application/json', 'application/x-www-form-urlencoded' or 'text/plain'")
return gperr.New("invalid mime_type, expect empty, 'application/json', 'application/x-www-form-urlencoded' or 'text/plain'")
}
switch webhook.Template {
case "":
if webhook.MIMEType == "application/json" && !json.Valid([]byte(webhook.Payload)) {
return E.New("invalid payload, expect valid JSON")
return gperr.New("invalid payload, expect valid JSON")
}
if webhook.Payload == "" {
return E.New("invalid payload, expect non-empty")
return gperr.New("invalid payload, expect non-empty")
}
case "discord":
webhook.ColorMode = "dec"
@@ -56,7 +56,7 @@ func (webhook *Webhook) Validate() E.Error {
webhook.Payload = discordPayload
}
default:
return E.New("invalid template, expect empty or 'discord'")
return gperr.New("invalid template, expect empty or 'discord'")
}
switch webhook.Method {
@@ -64,7 +64,7 @@ func (webhook *Webhook) Validate() E.Error {
webhook.Method = http.MethodPost
case http.MethodGet, http.MethodPost, http.MethodPut:
default:
return E.New("invalid method, expect empty, 'GET', 'POST' or 'PUT'")
return gperr.New("invalid method, expect empty, 'GET', 'POST' or 'PUT'")
}
switch webhook.ColorMode {
@@ -72,7 +72,7 @@ func (webhook *Webhook) Validate() E.Error {
webhook.ColorMode = "hex"
case "hex", "dec":
default:
return E.New("invalid color_mode, expect empty, 'hex' or 'dec'")
return gperr.New("invalid color_mode, expect empty, 'hex' or 'dec'")
}
return nil