Skip to content

Commit 7ea5af2

Browse files
committed
重构应用未处理异常代码,add DotWeb.DefaultHTTPErrorHandler,add DotWeb.SetDebugMode
1 parent f09a8f2 commit 7ea5af2

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

dotweb.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Dotweb struct {
1717
Modules []*HttpModule
1818
logpath string
1919
ExceptionHandler ExceptionHandle
20+
DebugMode bool
2021
}
2122

2223
type ExceptionHandle func(*HttpContext, interface{})
@@ -28,6 +29,7 @@ func New() *Dotweb {
2829
dotweb := &Dotweb{
2930
HttpServer: NewHttpServer(),
3031
Modules: make([]*HttpModule, 0, 10),
32+
DebugMode: false,
3133
}
3234
dotweb.HttpServer.setDotweb(dotweb)
3335

@@ -41,6 +43,13 @@ func (ds *Dotweb) RegisterModule(module *HttpModule) {
4143
ds.Modules = append(ds.Modules, module)
4244
}
4345

46+
/*
47+
设置Debug模式,默认为false
48+
*/
49+
func (ds *Dotweb) SetDebugMode(isDebug bool) {
50+
ds.DebugMode = isDebug
51+
}
52+
4453
/*
4554
* 设置异常处理函数
4655
*/
@@ -80,7 +89,7 @@ func (ds *Dotweb) StartServer(httpport int) error {
8089
ds.HttpServer.GET("/dotweb/query/:key", showQuery)
8190

8291
if ds.ExceptionHandler == nil {
83-
ds.SetExceptionHandle(DefaultHTTPErrorHandler)
92+
ds.SetExceptionHandle(ds.DefaultHTTPErrorHandler)
8493
}
8594

8695
port := ":" + strconv.Itoa(httpport)
@@ -89,6 +98,18 @@ func (ds *Dotweb) StartServer(httpport int) error {
8998
return err
9099
}
91100

101+
//默认异常处理
102+
func (ds *Dotweb) DefaultHTTPErrorHandler(ctx *HttpContext, errinfo interface{}) {
103+
//输出内容
104+
ctx.Response.WriteHeader(http.StatusInternalServerError)
105+
ctx.Response.Header().Set(HeaderContentType, CharsetUTF8)
106+
if ds.DebugMode {
107+
ctx.WriteString(fmt.Sprintln(errinfo))
108+
} else {
109+
ctx.WriteString("Internal Server Error")
110+
}
111+
}
112+
92113
//query pprof debug info
93114
//key:heap goroutine threadcreate block
94115
func initPProf(ctx *HttpContext) {
@@ -118,11 +139,3 @@ func showQuery(ctx *HttpContext) {
118139
ctx.WriteString("not support key => " + querykey)
119140
}
120141
}
121-
122-
//默认异常处理
123-
func DefaultHTTPErrorHandler(ctx *HttpContext, errinfo interface{}) {
124-
//输出内容
125-
ctx.Response.WriteHeader(http.StatusInternalServerError)
126-
ctx.Response.Header().Set(HeaderContentType, CharsetUTF8)
127-
ctx.WriteString(fmt.Sprintln(errinfo))
128-
}

example/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ func main() {
1414
//设置dotserver日志目录
1515
dotserver.SetLogPath(file.GetCurrentDirectory())
1616

17+
//设置Debug模式
18+
dotserver.SetDebugMode(true)
19+
1720
//设置路由
1821
InitRoute(dotserver)
1922

@@ -39,7 +42,7 @@ func IndexReg(ctx *dotweb.HttpContext) {
3942
}
4043

4144
func DefaultError(ctx *dotweb.HttpContext) {
42-
panic("DefaultError")
45+
panic("my panic error!")
4346
}
4447

4548
func InitRoute(dotserver *dotweb.Dotweb) {

0 commit comments

Comments
 (0)