github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_func.go (about)

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