ghttp_func.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "github.com/gogf/gf/v2/text/gstr"
  12. )
  13. // SupportedMethods returns all supported HTTP methods.
  14. func SupportedMethods() []string {
  15. return gstr.SplitAndTrim(supportedHttpMethods, ",")
  16. }
  17. // BuildParams builds the request string for the http client. The `params` can be type of:
  18. // string/[]byte/map/struct/*struct.
  19. //
  20. // The optional parameter `noUrlEncode` specifies whether to ignore the url encoding for the data.
  21. func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
  22. return httputil.BuildParams(params, noUrlEncode...)
  23. }
  24. // niceCallFunc calls function `f` with exception capture logic.
  25. func niceCallFunc(f func()) {
  26. defer func() {
  27. if exception := recover(); exception != nil {
  28. switch exception {
  29. case exceptionExit, exceptionExitAll:
  30. return
  31. default:
  32. if v, ok := exception.(error); ok && gerror.HasStack(v) {
  33. // It's already an error that has stack info.
  34. panic(v)
  35. }
  36. // Create a new error with stack info.
  37. // Note that there's a skip pointing the start stacktrace
  38. // of the real error point.
  39. if v, ok := exception.(error); ok {
  40. if gerror.Code(v) != gcode.CodeNil {
  41. panic(v)
  42. } else {
  43. panic(gerror.WrapCodeSkip(
  44. gcode.CodeInternalPanic, 1, v, "exception recovered",
  45. ))
  46. }
  47. } else {
  48. panic(gerror.NewCodeSkipf(
  49. gcode.CodeInternalPanic, 1, "exception recovered: %+v", exception,
  50. ))
  51. }
  52. }
  53. }
  54. }()
  55. f()
  56. }