mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 10:18:59 +02:00
refactor: improve error handling and response formatting in API
This commit is contained in:
43
internal/gperr/hint.go
Normal file
43
internal/gperr/hint.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package gperr
|
||||
|
||||
import "github.com/yusing/go-proxy/internal/utils/strutils/ansi"
|
||||
|
||||
type Hint struct {
|
||||
Prefix string
|
||||
Message string
|
||||
Suffix string
|
||||
}
|
||||
|
||||
var _ PlainError = (*Hint)(nil)
|
||||
var _ MarkdownError = (*Hint)(nil)
|
||||
|
||||
func (h *Hint) Error() string {
|
||||
return h.Prefix + ansi.Info(h.Message) + h.Suffix
|
||||
}
|
||||
|
||||
func (h *Hint) Plain() []byte {
|
||||
return []byte(h.Prefix + h.Message + h.Suffix)
|
||||
}
|
||||
|
||||
func (h *Hint) Markdown() []byte {
|
||||
return []byte(h.Prefix + "**" + h.Message + "**" + h.Suffix)
|
||||
}
|
||||
|
||||
func (h *Hint) MarshalText() ([]byte, error) {
|
||||
return h.Plain(), nil
|
||||
}
|
||||
|
||||
func (h *Hint) String() string {
|
||||
return h.Error()
|
||||
}
|
||||
|
||||
func DoYouMean(s string) *Hint {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return &Hint{
|
||||
Prefix: "Do you mean ",
|
||||
Message: s,
|
||||
Suffix: "?",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user