improve package gerror

This commit is contained in:
jflyfox
2020-12-30 13:18:43 +08:00
parent bdf23ef48f
commit 86e70ad55c
6 changed files with 189 additions and 15 deletions

View File

@ -1,4 +1,4 @@
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -69,19 +69,23 @@ func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr strin
// niceCallFunc calls function <f> with exception capture logic.
func niceCallFunc(f func()) {
defer func() {
if e := recover(); e != nil {
switch e {
if exception := recover(); exception != nil {
switch exception {
case exceptionExit, exceptionExitAll:
return
default:
if _, ok := e.(errorStack); ok {
if _, ok := exception.(errorStack); ok {
// It's already an error that has stack info.
panic(e)
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.
panic(gerror.NewSkipf(1, "%v", e))
if err, ok := exception.(error); ok {
panic(gerror.Wrap(err, ""))
} else {
panic(gerror.NewSkipf(1, "%v", exception))
}
}
}
}

View File

@ -1,4 +1,4 @@
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -128,7 +128,7 @@ func (m *Middleware) Next() {
// Create a new error with stack info.
// Note that there's a skip pointing the start stacktrace
// of the real error point.
m.request.error = gerror.NewSkip(1, exception.Error())
m.request.error = gerror.WrapSkip(1, exception, "")
}
m.request.Response.WriteStatus(http.StatusInternalServerError, exception)
loop = false

View File

@ -1,4 +1,4 @@
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -65,7 +65,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
if exception := recover(); exception != nil {
request.Response.WriteStatus(http.StatusInternalServerError)
s.handleErrorLog(gerror.Newf("%v", exception), request)
if err, ok := exception.(error); ok {
s.handleErrorLog(gerror.Wrap(err, ""), request)
} else {
s.handleErrorLog(gerror.Newf("%v", exception), request)
}
}
}
// access log handling.

View File

@ -1,4 +1,4 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,