mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
fix: simplify error handling in Buf methods
Consolidate error wrapping to avoid redundant fmt.Errorf calls. Follows PR #73 review feedback.
This commit is contained in:
parent
7e15181c0b
commit
6e33bc48aa
1 changed files with 11 additions and 8 deletions
19
context.go
19
context.go
|
|
@ -418,13 +418,14 @@ func (c *Context) JSON(code int, obj any) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSONBuf 先将 JSON 编码到 buffer, 成功后再写入状态码和响应体.
|
// JSONBuf 先将 JSON 编码到 buffer, 成功后再写入状态码和响应体.
|
||||||
// 与 JSON 相比, 编码失败时可以正确返回 500 状态码, 代价是多一次内存分配.
|
// 与 JSON 相比,编码失败时可以正确返回 500 状态码,代价是多一次内存分配.
|
||||||
func (c *Context) JSONBuf(code int, obj any) {
|
func (c *Context) JSONBuf(code int, obj any) {
|
||||||
data, err := json.Marshal(obj)
|
data, err := json.Marshal(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AddError(fmt.Errorf("failed to marshal JSON: %w", err))
|
errMsg := fmt.Errorf("failed to marshal JSON: %w", err)
|
||||||
c.Errorf("failed to marshal JSON: %s", err)
|
c.AddError(errMsg)
|
||||||
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to marshal JSON: %w", err))
|
c.Errorf("%s", errMsg)
|
||||||
|
c.ErrorUseHandle(http.StatusInternalServerError, errMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
|
@ -451,8 +452,9 @@ func (c *Context) GOBBuf(code int, obj any) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
encoder := gob.NewEncoder(&buf)
|
encoder := gob.NewEncoder(&buf)
|
||||||
if err := encoder.Encode(obj); err != nil {
|
if err := encoder.Encode(obj); err != nil {
|
||||||
c.AddError(fmt.Errorf("failed to encode GOB: %w", err))
|
errMsg := fmt.Errorf("failed to encode GOB: %w", err)
|
||||||
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to encode GOB: %w", err))
|
c.AddError(errMsg)
|
||||||
|
c.ErrorUseHandle(http.StatusInternalServerError, errMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
c.Writer.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
|
@ -478,8 +480,9 @@ func (c *Context) WANF(code int, obj any) {
|
||||||
func (c *Context) WANFBuf(code int, obj any) {
|
func (c *Context) WANFBuf(code int, obj any) {
|
||||||
data, err := wanf.Marshal(obj)
|
data, err := wanf.Marshal(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AddError(fmt.Errorf("failed to encode WANF: %w", err))
|
errMsg := fmt.Errorf("failed to encode WANF: %w", err)
|
||||||
c.ErrorUseHandle(http.StatusInternalServerError, fmt.Errorf("failed to encode WANF: %w", err))
|
c.AddError(errMsg)
|
||||||
|
c.ErrorUseHandle(http.StatusInternalServerError, errMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Writer.Header().Set("Content-Type", "application/vnd.wjqserver.wanf; charset=utf-8")
|
c.Writer.Header().Set("Content-Type", "application/vnd.wjqserver.wanf; charset=utf-8")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue