Skip to content

Commit

Permalink
fix/return data field in json error response (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
MindHunter86 authored Jul 20, 2024
2 parents 053895e + 2918a0b commit e1abab4
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 16 deletions.
4 changes: 2 additions & 2 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ func (m *Proxy) doRequest(c *fiber.Ctx, req *fasthttp.Request, rsp *fasthttp.Res
}

func (*Proxy) unmarshalApiResponse(c *fiber.Ctx, rsp *fasthttp.Response) (ok bool, e error) {
var apirsp *utils.ApiResponse
var apirsp *utils.ApiResponseWOData
if apirsp, e = utils.UnmarshalApiResponse(rsp.Body()); e != nil || apirsp == nil {
return
}
defer utils.ReleaseApiResponse(apirsp)
defer utils.ReleaseApiResponseWOData(apirsp)

if apirsp.Status && (apirsp.Error == nil || apirsp.Error.Code == 0) {
ok = true
Expand Down
27 changes: 25 additions & 2 deletions internal/utils/apiv1_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (

type (
ApiResponse struct {
Status bool
Data interface{}
Error *ApiError
}
ApiResponseWOData struct {
Status bool
Data interface{} `json:"-"`
Error *ApiError
Expand All @@ -20,6 +25,24 @@ type (
}
)

var apiResponseWODataPool = sync.Pool{
New: func() interface{} {
return &ApiResponseWOData{
Error: &ApiError{},
}
},
}

func AcquireApiResponseWOData() *ApiResponseWOData {
return apiResponseWODataPool.Get().(*ApiResponseWOData)
}

func ReleaseApiResponseWOData(ar *ApiResponseWOData) {
ar.Status = false
ar.Error.Code, ar.Error.Message, ar.Error.Description = 0, "", ""
apiResponseWODataPool.Put(ar)
}

var apiResponsePool = sync.Pool{
New: func() interface{} {
return &ApiResponse{
Expand Down Expand Up @@ -53,7 +76,7 @@ func RespondWithApiError(status int, msg, desc string, w io.Writer) (e error) {
return
}

func UnmarshalApiResponse(payload []byte) (_ *ApiResponse, e error) {
apirsp := AcquireApiResponse()
func UnmarshalApiResponse(payload []byte) (_ *ApiResponseWOData, e error) {
apirsp := AcquireApiResponseWOData()
return apirsp, easyjson.Unmarshal(payload, apirsp)
}
128 changes: 116 additions & 12 deletions internal/utils/apiv1_response_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e1abab4

Please sign in to comment.