Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust xdto and logger for recovery #9

Closed
Aoi-hosizora opened this issue Nov 2, 2020 · 2 comments
Closed

Adjust xdto and logger for recovery #9

Aoi-hosizora opened this issue Nov 2, 2020 · 2 comments
Labels
enhancement New feature or request

Comments

@Aoi-hosizora
Copy link
Owner

  1. Add WithOthers for ErrorDto and remove BuildFullErrorDto.
  2. Move logger for recovery to xgin or xrecovery???
  3. Need to adjust result and add MarshalJSON to check run mode.
@Aoi-hosizora Aoi-hosizora added the enhancement New feature or request label Nov 2, 2020
@Aoi-hosizora
Copy link
Owner Author

For 3: result.MarshalJSON

func (r *Result) MarshalJSON() ([]byte, error) {
	prop := make([]string, 0, 4)

	// code + message
	prop = append(prop, fmt.Sprintf(`"code":%d`, r.Code))
	prop = append(prop, fmt.Sprintf(`"message":"%s"`, r.Message))

	// data
	if r.Data != nil {
		data, err := json.Marshal(r.Data)
		if err != nil {
			return nil, err
		}
		prop = append(prop, fmt.Sprintf(`"data":%s`, data))
	}

	// error
	if r.Error != nil && gin.Mode() == gin.DebugMode {
		erro, err := json.Marshal(r.Error)
		if err != nil {
			return nil, err
		}
		prop = append(prop, fmt.Sprintf(`"error":%s`, erro))
	}

	out := "{" + strings.Join(prop, ",") + "}"
	return []byte(out), nil
}

func (r *Result) String() string {
	j, err := r.MarshalJSON()
	if err != nil {
		return ""
	}
	return string(j)
}

@Aoi-hosizora
Copy link
Owner Author

Done.

func (r *Result) JSON(c *gin.Context) {
	if gin.Mode() != gin.DebugMode {
		r.Error = nil
	}
	c.JSON(int(r.Status), r)
}

func (r *Result) XML(c *gin.Context) {
	if gin.Mode() != gin.DebugMode {
		r.Error = nil
	}
	c.XML(int(r.Status), r)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant