@@ -17,6 +17,7 @@ type Dotweb struct {
1717 Modules []* HttpModule
1818 logpath string
1919 ExceptionHandler ExceptionHandle
20+ DebugMode bool
2021}
2122
2223type 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
94115func 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- }
0 commit comments