mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-24 09:48:49 +02:00
refactor(errs): migrate from gperr.Error to standard Go error interface
This is a large-scale refactoring across the codebase that replaces the custom `gperr.Error` type with Go's standard `error` interface. The changes include: - Replacing `gperr.Error` return types with `error` in function signatures - Using `errors.New()` and `fmt.Errorf()` instead of `gperr.New()` and `gperr.Errorf()` - Using `%w` format verb for error wrapping instead of `.With()` method - Replacing `gperr.Subject()` calls with `gperr.PrependSubject()` - Converting error logging from `gperr.Log*()` functions to zerolog's `.Err().Msg()` pattern - Update NewLogger to handle multiline error message - Updating `goutils` submodule to latest commit This refactoring aligns with Go idioms and removes the dependency on custom error handling abstractions in favor of standard library patterns.
This commit is contained in:
@@ -2,6 +2,7 @@ package notif
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -26,9 +27,11 @@ var webhookTemplates = map[string]string{
|
||||
"discord": discordPayload,
|
||||
}
|
||||
|
||||
func (webhook *Webhook) Validate() gperr.Error {
|
||||
if err := webhook.ProviderBase.Validate(); err != nil && !err.Is(ErrMissingToken) {
|
||||
return err
|
||||
func (webhook *Webhook) Validate() error {
|
||||
var errs gperr.Builder
|
||||
|
||||
if err := webhook.ProviderBase.Validate(); err != nil && !errors.Is(err, ErrMissingToken) {
|
||||
errs.Add(err)
|
||||
}
|
||||
|
||||
switch webhook.MIMEType {
|
||||
@@ -36,18 +39,18 @@ func (webhook *Webhook) Validate() gperr.Error {
|
||||
webhook.MIMEType = MimeTypeJSON
|
||||
case MimeTypeJSON, MimeTypeForm, MimeTypeText:
|
||||
default:
|
||||
return gperr.Errorf("invalid mime_type, expect %s", strings.Join([]string{"empty", MimeTypeJSON, MimeTypeForm, MimeTypeText}, ", "))
|
||||
errs.Addf("invalid mime_type, expect %s", strings.Join([]string{"empty", MimeTypeJSON, MimeTypeForm, MimeTypeText}, ", "))
|
||||
}
|
||||
|
||||
switch webhook.Template {
|
||||
case "":
|
||||
if webhook.MIMEType == MimeTypeJSON {
|
||||
if !validateJSONPayload(webhook.Payload) {
|
||||
return gperr.New("invalid payload, expect valid JSON")
|
||||
errs.Adds("invalid payload, expect valid JSON")
|
||||
}
|
||||
}
|
||||
if webhook.Payload == "" {
|
||||
return gperr.New("invalid payload, expect non-empty")
|
||||
errs.Adds("invalid payload, expect non-empty")
|
||||
}
|
||||
case "discord":
|
||||
webhook.ColorMode = "dec"
|
||||
@@ -57,7 +60,7 @@ func (webhook *Webhook) Validate() gperr.Error {
|
||||
webhook.Payload = discordPayload
|
||||
}
|
||||
default:
|
||||
return gperr.New("invalid template, expect empty or 'discord'")
|
||||
errs.Adds("invalid template, expect empty or 'discord'")
|
||||
}
|
||||
|
||||
switch webhook.Method {
|
||||
@@ -65,7 +68,7 @@ func (webhook *Webhook) Validate() gperr.Error {
|
||||
webhook.Method = http.MethodPost
|
||||
case http.MethodGet, http.MethodPost, http.MethodPut:
|
||||
default:
|
||||
return gperr.New("invalid method, expect empty, 'GET', 'POST' or 'PUT'")
|
||||
errs.Adds("invalid method, expect empty, 'GET', 'POST' or 'PUT'")
|
||||
}
|
||||
|
||||
switch webhook.ColorMode {
|
||||
@@ -73,10 +76,10 @@ func (webhook *Webhook) Validate() gperr.Error {
|
||||
webhook.ColorMode = "hex"
|
||||
case "hex", "dec":
|
||||
default:
|
||||
return gperr.New("invalid color_mode, expect empty, 'hex' or 'dec'")
|
||||
errs.Adds("invalid color_mode, expect empty, 'hex' or 'dec'")
|
||||
}
|
||||
|
||||
return nil
|
||||
return errs.Error()
|
||||
}
|
||||
|
||||
// GetMethod implements Provider.
|
||||
|
||||
Reference in New Issue
Block a user