go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

feat: add custom unset err when parse request

Open SpectatorNan opened this issue 1 year ago • 2 comments

Support parameter not set custom exception

replace

field age is not set

to custom error

such as internationalization support

- zh
年龄没有设置
- en
 age not set
-jp
 年齢は設定されていません
.......

SpectatorNan avatar Aug 01 '23 15:08 SpectatorNan

Codecov Report

Attention: Patch coverage is 85.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 94.24%. Comparing base (8690859) to head (4f789e7). Report is 64 commits behind head on master.

:exclamation: Current head 4f789e7 differs from pull request most recent head a2e579b

Please upload reports for the commit a2e579b to get more accurate results.

Additional details and impacted files
Files Coverage Δ
rest/httpx/requests.go 100.00% <100.00%> (ø)
core/mapping/unmarshaler.go 96.04% <70.00%> (-0.45%) :arrow_down:

... and 81 files with indirect coverage changes

codecov[bot] avatar Aug 01 '23 16:08 codecov[bot]

i use with i18n the support with go-zero github.com/SpectatorNan/go-zero-i18n/goi18nx

      ......
	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf, rest.WithCors(), errorx.RouteMethodNotAllow(), func(server *rest.Server) {
		fmt.Println("MustNewServer")
	})

	server.Use(i18nx.NewMiddleware(c.LocalizationFiles...).Handle)
	server.Use(validator.NewMiddleware().Handle)

	httpx.SetCustomUnsetError(func(ctx context.Context, fieldName string) error {
		name := goi18nx.FormatText(ctx, fmt.Sprintf("Parameters.%s", fieldName), fieldName)
		msg := goi18nx.FormatTextWithData(ctx, "Parameters.UnsetErr", fmt.Sprintf("%s is unset", fieldName), map[string]interface{}{"name": name})
		return  errorx.NewErrMsg(msg)
	})

	defer server.Stop()
      ......

SpectatorNan avatar Aug 01 '23 20:08 SpectatorNan