ghttp_func.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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/v2/errors/gcode"
  9. "github.com/gogf/gf/v2/errors/gerror"
  10. "github.com/gogf/gf/v2/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 to 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 v, ok := exception.(error); ok && gerror.HasStack(v) {
  28. // It's already an error that has stack info.
  29. panic(v)
  30. }
  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 v, ok := exception.(error); ok {
  35. if gerror.Code(v) != gcode.CodeNil {
  36. panic(v)
  37. } else {
  38. panic(gerror.WrapCodeSkip(
  39. gcode.CodeInternalError, 1, v, "exception recovered",
  40. ))
  41. }
  42. } else {
  43. panic(gerror.NewCodeSkipf(
  44. gcode.CodeInternalError, 1, "exception recovered: %+v", exception,
  45. ))
  46. }
  47. }
  48. }
  49. }()
  50. f()
  51. }