mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-23 17:41:05 +01:00
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.
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package notif
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/bytedance/sonic"
|
|
"github.com/gotify/server/v2/model"
|
|
"github.com/rs/zerolog"
|
|
gperr "github.com/yusing/goutils/errs"
|
|
)
|
|
|
|
type (
|
|
GotifyClient struct {
|
|
ProviderBase
|
|
}
|
|
GotifyMessage model.MessageExternal
|
|
)
|
|
|
|
const gotifyMsgEndpoint = "/message"
|
|
|
|
func (client *GotifyClient) Validate() error {
|
|
var errs gperr.Builder
|
|
if err := client.ProviderBase.Validate(); err != nil {
|
|
errs.Add(err)
|
|
}
|
|
if client.Token == "" {
|
|
errs.Adds("token is required")
|
|
}
|
|
return errs.Error()
|
|
}
|
|
|
|
func (client *GotifyClient) GetURL() string {
|
|
return client.URL + gotifyMsgEndpoint
|
|
}
|
|
|
|
// MarshalMessage implements Provider.
|
|
func (client *GotifyClient) MarshalMessage(logMsg *LogMessage) ([]byte, error) {
|
|
var priority int
|
|
|
|
switch logMsg.Level {
|
|
case zerolog.WarnLevel:
|
|
priority = 2
|
|
case zerolog.ErrorLevel:
|
|
priority = 5
|
|
case zerolog.FatalLevel, zerolog.PanicLevel:
|
|
priority = 8
|
|
}
|
|
|
|
body, err := logMsg.Body.Format(client.Format)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
msg := &GotifyMessage{
|
|
Title: logMsg.Title,
|
|
Message: string(body),
|
|
Priority: &priority,
|
|
}
|
|
|
|
if client.Format == LogFormatMarkdown {
|
|
msg.Extras = map[string]interface{}{
|
|
"client::display": map[string]string{
|
|
"contentType": "text/markdown",
|
|
},
|
|
}
|
|
}
|
|
|
|
data, err := sonic.Marshal(msg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
// fmtError implements Provider.
|
|
func (client *GotifyClient) fmtError(respBody io.Reader) error {
|
|
var errm model.Error
|
|
err := sonic.ConfigDefault.NewDecoder(respBody).Decode(&errm)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to decode err response: %w", err)
|
|
}
|
|
return fmt.Errorf("%s: %s", errm.Error, errm.ErrorDescription)
|
|
}
|