Skip to content

feat: add custom unset err when parse request - #3460

Open
SpectatorNan wants to merge 19 commits into
zeromicro:masterfrom
SpectatorNan:support-request-custom-field-not-set-err
Open

SpectatorNan wants to merge 19 commits into
zeromicro:masterfrom
SpectatorNan:support-request-custom-field-not-set-err

Conversation

@SpectatorNan

Copy link
Copy Markdown
Contributor

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
 年齢は設定されていません
.......

@codecov

codecov Bot commented Aug 1, 2023 •

Copy link
Copy Markdown

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.

❗ 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%) ⬇️

... and 81 files with indirect coverage changes

@SpectatorNan

Copy link
Copy Markdown
Contributor Author

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()
      ......

@kevwan
kevwan force-pushed the support-request-custom-field-not-set-err branch from 269bcfb to 70d652a Compare August 5, 2023 16:20
@kevwan kevwan added the area/restful Categorizes issue or PR as related to restful apis. label Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/restful Categorizes issue or PR as related to restful apis.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants