2020-12-30 13:18:43 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-01-11 16:06:42 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-01-11 16:06:42 +08:00
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
2018-09-26 18:44:30 +08:00
|
|
|
import (
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-12-03 23:32:00 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/httputil"
|
2020-10-26 20:21:09 +08:00
|
|
|
)
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// BuildParams builds the request string for the http client. The `params` can be type of:
|
2020-01-20 20:32:39 +08:00
|
|
|
// string/[]byte/map/struct/*struct.
|
|
|
|
//
|
2022-03-19 17:58:21 +08:00
|
|
|
// The optional parameter `noUrlEncode` specifies whether to ignore the url encoding for the data.
|
2019-07-11 19:47:15 +08:00
|
|
|
func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
|
2021-01-25 14:54:38 +08:00
|
|
|
return httputil.BuildParams(params, noUrlEncode...)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-08-03 15:54:12 +08:00
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// niceCallFunc calls function `f` with exception capture logic.
|
2019-08-03 15:54:12 +08:00
|
|
|
func niceCallFunc(f func()) {
|
|
|
|
defer func() {
|
2020-12-30 13:18:43 +08:00
|
|
|
if exception := recover(); exception != nil {
|
|
|
|
switch exception {
|
2020-12-14 13:26:48 +08:00
|
|
|
case exceptionExit, exceptionExitAll:
|
2019-08-03 15:54:12 +08:00
|
|
|
return
|
2021-07-20 23:02:02 +08:00
|
|
|
|
2019-08-03 15:54:12 +08:00
|
|
|
default:
|
2021-09-17 19:26:56 +08:00
|
|
|
if v, ok := exception.(error); ok && gerror.HasStack(v) {
|
2020-04-29 00:14:29 +08:00
|
|
|
// It's already an error that has stack info.
|
2021-09-17 19:26:56 +08:00
|
|
|
panic(v)
|
2022-01-12 19:39:38 +08:00
|
|
|
}
|
|
|
|
// Create a new error with stack info.
|
|
|
|
// Note that there's a skip pointing the start stacktrace
|
|
|
|
// of the real error point.
|
|
|
|
if v, ok := exception.(error); ok {
|
|
|
|
if gerror.Code(v) != gcode.CodeNil {
|
|
|
|
panic(v)
|
2020-12-30 13:18:43 +08:00
|
|
|
} else {
|
2022-01-12 19:39:38 +08:00
|
|
|
panic(gerror.WrapCodeSkip(
|
|
|
|
gcode.CodeInternalError, 1, v, "exception recovered",
|
|
|
|
))
|
2020-12-30 13:18:43 +08:00
|
|
|
}
|
2022-01-12 19:39:38 +08:00
|
|
|
} else {
|
|
|
|
panic(gerror.NewCodeSkipf(
|
|
|
|
gcode.CodeInternalError, 1, "exception recovered: %+v", exception,
|
|
|
|
))
|
2020-04-28 15:04:07 +08:00
|
|
|
}
|
2019-08-03 15:54:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f()
|
|
|
|
}
|