gf/net/ghttp/ghttp_func.go

48 lines
1.4 KiB
Go
Raw Normal View History

2020-12-30 13:18:43 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
import (
"github.com/gogf/gf/errors/gerror"
2021-01-25 14:54:38 +08:00
"github.com/gogf/gf/net/ghttp/internal/httputil"
)
2020-01-20 20:32:39 +08:00
// BuildParams builds the request string for the http client. The <params> can be type of:
// string/[]byte/map/struct/*struct.
//
// The optional parameter <noUrlEncode> specifies whether 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
}
2020-01-20 20:32:39 +08:00
// niceCallFunc calls function <f> with exception capture logic.
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:
return
default:
2020-12-30 13:18:43 +08:00
if _, ok := exception.(errorStack); ok {
// It's already an error that has stack info.
2020-12-30 13:18:43 +08:00
panic(exception)
} else {
// Create a new error with stack info.
// Note that there's a skip pointing the start stacktrace
// of the real error point.
2020-12-30 13:18:43 +08:00
if err, ok := exception.(error); ok {
panic(gerror.Wrap(err, ""))
} else {
panic(gerror.NewSkipf(1, "%v", exception))
}
}
}
}
}()
f()
}