Skip to content

Commit

Permalink
ignore err.Data() when 500
Browse files Browse the repository at this point in the history
  • Loading branch information
bughou committed Oct 25, 2021
1 parent 3538a7d commit 6f79566
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A golang http router with regexp and document generation support.
[![Build Status](https://github.com/lovego/goa/actions/workflows/go.yml/badge.svg)](https://github.com/lovego/goa/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github/lovego/goa/badge.svg?branch=master&1)](https://coveralls.io/github/lovego/goa)
[![Go Report Card](https://goreportcard.com/badge/github.com/lovego/goa)](https://goreportcard.com/report/github.com/lovego/goa)
[![Documentation](https://pkg.go.dev/badge/github.com/lovego/goa)](https://pkg.go.dev/github.com/lovego/goa@v0.2.0)
[![Documentation](https://pkg.go.dev/badge/github.com/lovego/goa)](https://pkg.go.dev/github.com/lovego/goa@v0.2.1)


## Usage
Expand Down
18 changes: 11 additions & 7 deletions context-resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,23 @@ func (c *ContextBeforeLookup) Data(data interface{}, err error) {
c.SetError(err)
}
}
body.Data = getData(data, err, statusCode)

if err != nil {
c.StatusJson(statusCode, body)
}

func getData(data interface{}, err error, code int) interface{} {
if err != nil && code != http.StatusInternalServerError {
if err2, ok := err.(interface {
Data() interface{}
}); ok && err2.Data() != nil {
body.Data = err2.Data()
} else if data != nil && !isNilValue(data) { // 避免返回"data": null
body.Data = data
return err2.Data()
}
} else if data != nil && !isNilValue(data) { // 避免返回"data": null
body.Data = data
}
c.StatusJson(statusCode, body)
if data != nil && !isNilValue(data) { // 避免返回"data": null
return data
}
return nil
}

func isNilValue(itfc interface{}) bool {
Expand Down

0 comments on commit 6f79566

Please sign in to comment.