ghttp_func.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package ghttp
  7. import (
  8. "github.com/gogf/gf/errors/gcode"
  9. "github.com/gogf/gf/errors/gerror"
  10. "github.com/gogf/gf/net/ghttp/internal/httputil"
  11. )
  12. // BuildParams builds the request string for the http client. The <params> can be type of:
  13. // string/[]byte/map/struct/*struct.
  14. //
  15. // The optional parameter <noUrlEncode> specifies whether ignore the url encoding for the data.
  16. func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
  17. return httputil.BuildParams(params, noUrlEncode...)
  18. }
  19. // niceCallFunc calls function <f> with exception capture logic.
  20. func niceCallFunc(f func()) {
  21. defer func() {
  22. if exception := recover(); exception != nil {
  23. switch exception {
  24. case exceptionExit, exceptionExitAll:
  25. return
  26. default:
  27. if _, ok := exception.(errorStack); ok {
  28. // It's already an error that has stack info.
  29. panic(exception)
  30. } else {
  31. // Create a new error with stack info.
  32. // Note that there's a skip pointing the start stacktrace
  33. // of the real error point.
  34. if err, ok := exception.(error); ok {
  35. if gerror.Code(err) != gcode.CodeNil {
  36. panic(err)
  37. } else {
  38. panic(gerror.WrapCodeSkip(gcode.CodeInternalError, 1, err, ""))
  39. }
  40. } else {
  41. panic(gerror.NewCodeSkipf(gcode.CodeInternalError, 1, "%+v", exception))
  42. }
  43. }
  44. }
  45. }
  46. }()
  47. f()
  48. }