refactor: improve error handling and response formatting in API

This commit is contained in:
yusing
2025-05-03 17:41:10 +08:00
parent 82c829de18
commit 98e90d7a0b
31 changed files with 657 additions and 185 deletions

View File

@@ -26,6 +26,9 @@ type (
FieldsBody []LogField
ListBody []string
MessageBody string
ErrorBody struct {
Error error
}
)
var (
@@ -113,3 +116,15 @@ func (m MessageBody) Format(format *LogFormat) ([]byte, error) {
}
return nil, fmt.Errorf("unknown format: %v", format)
}
func (e ErrorBody) Format(format *LogFormat) ([]byte, error) {
switch format {
case LogFormatRawJSON:
return json.Marshal(e)
case LogFormatPlain:
return gperr.Plain(e.Error), nil
case LogFormatMarkdown:
return gperr.Markdown(e.Error), nil
}
return nil, fmt.Errorf("unknown format: %v", format)
}