add comments for middleware packages, fix typo error
Showing
3 changed files
with
18 additions
and
8 deletions
| ... | @@ -221,7 +221,7 @@ func Run() { | ... | @@ -221,7 +221,7 @@ func Run() { |
| 221 | 221 | ||
| 222 | middleware.VERSION = VERSION | 222 | middleware.VERSION = VERSION |
| 223 | middleware.AppName = AppName | 223 | middleware.AppName = AppName |
| 224 | middleware.RegisterErrorHander() | 224 | middleware.RegisterErrorHandler() |
| 225 | 225 | ||
| 226 | if EnableAdmin { | 226 | if EnableAdmin { |
| 227 | go BeeAdminApp.Run() | 227 | go BeeAdminApp.Run() | ... | ... |
| ... | @@ -61,6 +61,7 @@ var tpl = ` | ... | @@ -61,6 +61,7 @@ var tpl = ` |
| 61 | </html> | 61 | </html> |
| 62 | ` | 62 | ` |
| 63 | 63 | ||
| 64 | // render default application error page with error and stack string. | ||
| 64 | func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) { | 65 | func ShowErr(err interface{}, rw http.ResponseWriter, r *http.Request, Stack string) { |
| 65 | t, _ := template.New("beegoerrortemp").Parse(tpl) | 66 | t, _ := template.New("beegoerrortemp").Parse(tpl) |
| 66 | data := make(map[string]string) | 67 | data := make(map[string]string) |
| ... | @@ -175,13 +176,14 @@ var errtpl = ` | ... | @@ -175,13 +176,14 @@ var errtpl = ` |
| 175 | </html> | 176 | </html> |
| 176 | ` | 177 | ` |
| 177 | 178 | ||
| 179 | // map of http handlers for each error string. | ||
| 178 | var ErrorMaps map[string]http.HandlerFunc | 180 | var ErrorMaps map[string]http.HandlerFunc |
| 179 | 181 | ||
| 180 | func init() { | 182 | func init() { |
| 181 | ErrorMaps = make(map[string]http.HandlerFunc) | 183 | ErrorMaps = make(map[string]http.HandlerFunc) |
| 182 | } | 184 | } |
| 183 | 185 | ||
| 184 | //404 | 186 | // show 404 notfound error. |
| 185 | func NotFound(rw http.ResponseWriter, r *http.Request) { | 187 | func NotFound(rw http.ResponseWriter, r *http.Request) { |
| 186 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 188 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 187 | data := make(map[string]interface{}) | 189 | data := make(map[string]interface{}) |
| ... | @@ -199,7 +201,7 @@ func NotFound(rw http.ResponseWriter, r *http.Request) { | ... | @@ -199,7 +201,7 @@ func NotFound(rw http.ResponseWriter, r *http.Request) { |
| 199 | t.Execute(rw, data) | 201 | t.Execute(rw, data) |
| 200 | } | 202 | } |
| 201 | 203 | ||
| 202 | //401 | 204 | // show 401 unauthorized error. |
| 203 | func Unauthorized(rw http.ResponseWriter, r *http.Request) { | 205 | func Unauthorized(rw http.ResponseWriter, r *http.Request) { |
| 204 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 206 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 205 | data := make(map[string]interface{}) | 207 | data := make(map[string]interface{}) |
| ... | @@ -215,7 +217,7 @@ func Unauthorized(rw http.ResponseWriter, r *http.Request) { | ... | @@ -215,7 +217,7 @@ func Unauthorized(rw http.ResponseWriter, r *http.Request) { |
| 215 | t.Execute(rw, data) | 217 | t.Execute(rw, data) |
| 216 | } | 218 | } |
| 217 | 219 | ||
| 218 | //403 | 220 | // show 403 forbidden error. |
| 219 | func Forbidden(rw http.ResponseWriter, r *http.Request) { | 221 | func Forbidden(rw http.ResponseWriter, r *http.Request) { |
| 220 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 222 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 221 | data := make(map[string]interface{}) | 223 | data := make(map[string]interface{}) |
| ... | @@ -232,7 +234,7 @@ func Forbidden(rw http.ResponseWriter, r *http.Request) { | ... | @@ -232,7 +234,7 @@ func Forbidden(rw http.ResponseWriter, r *http.Request) { |
| 232 | t.Execute(rw, data) | 234 | t.Execute(rw, data) |
| 233 | } | 235 | } |
| 234 | 236 | ||
| 235 | //503 | 237 | // show 503 service unavailable error. |
| 236 | func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { | 238 | func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { |
| 237 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 239 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 238 | data := make(map[string]interface{}) | 240 | data := make(map[string]interface{}) |
| ... | @@ -248,7 +250,7 @@ func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { | ... | @@ -248,7 +250,7 @@ func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { |
| 248 | t.Execute(rw, data) | 250 | t.Execute(rw, data) |
| 249 | } | 251 | } |
| 250 | 252 | ||
| 251 | //500 | 253 | // show 500 internal server error. |
| 252 | func InternalServerError(rw http.ResponseWriter, r *http.Request) { | 254 | func InternalServerError(rw http.ResponseWriter, r *http.Request) { |
| 253 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 255 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 254 | data := make(map[string]interface{}) | 256 | data := make(map[string]interface{}) |
| ... | @@ -262,15 +264,18 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) { | ... | @@ -262,15 +264,18 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) { |
| 262 | t.Execute(rw, data) | 264 | t.Execute(rw, data) |
| 263 | } | 265 | } |
| 264 | 266 | ||
| 267 | // show 500 internal error with simple text string. | ||
| 265 | func SimpleServerError(rw http.ResponseWriter, r *http.Request) { | 268 | func SimpleServerError(rw http.ResponseWriter, r *http.Request) { |
| 266 | http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | 269 | http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 267 | } | 270 | } |
| 268 | 271 | ||
| 272 | // add http handler for given error string. | ||
| 269 | func Errorhandler(err string, h http.HandlerFunc) { | 273 | func Errorhandler(err string, h http.HandlerFunc) { |
| 270 | ErrorMaps[err] = h | 274 | ErrorMaps[err] = h |
| 271 | } | 275 | } |
| 272 | 276 | ||
| 273 | func RegisterErrorHander() { | 277 | // register default error http handlers, 404,401,403,500 and 503. |
| 278 | func RegisterErrorHandler() { | ||
| 274 | if _, ok := ErrorMaps["404"]; !ok { | 279 | if _, ok := ErrorMaps["404"]; !ok { |
| 275 | ErrorMaps["404"] = NotFound | 280 | ErrorMaps["404"] = NotFound |
| 276 | } | 281 | } |
| ... | @@ -292,6 +297,8 @@ func RegisterErrorHander() { | ... | @@ -292,6 +297,8 @@ func RegisterErrorHander() { |
| 292 | } | 297 | } |
| 293 | } | 298 | } |
| 294 | 299 | ||
| 300 | // show error string as simple text message. | ||
| 301 | // if error string is empty, show 500 error as default. | ||
| 295 | func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) { | 302 | func Exception(errcode string, w http.ResponseWriter, r *http.Request, msg string) { |
| 296 | if h, ok := ErrorMaps[errcode]; ok { | 303 | if h, ok := ErrorMaps[errcode]; ok { |
| 297 | isint, err := strconv.Atoi(errcode) | 304 | isint, err := strconv.Atoi(errcode) | ... | ... |
| ... | @@ -2,16 +2,19 @@ package middleware | ... | @@ -2,16 +2,19 @@ package middleware |
| 2 | 2 | ||
| 3 | import "fmt" | 3 | import "fmt" |
| 4 | 4 | ||
| 5 | // http exceptions | ||
| 5 | type HTTPException struct { | 6 | type HTTPException struct { |
| 6 | StatusCode int // http status code 4xx, 5xx | 7 | StatusCode int // http status code 4xx, 5xx |
| 7 | Description string | 8 | Description string |
| 8 | } | 9 | } |
| 9 | 10 | ||
| 11 | // return http exception error string, e.g. "400 Bad Request". | ||
| 10 | func (e *HTTPException) Error() string { | 12 | func (e *HTTPException) Error() string { |
| 11 | // return `status description`, e.g. `400 Bad Request` | ||
| 12 | return fmt.Sprintf("%d %s", e.StatusCode, e.Description) | 13 | return fmt.Sprintf("%d %s", e.StatusCode, e.Description) |
| 13 | } | 14 | } |
| 14 | 15 | ||
| 16 | // map of http exceptions for each http status code int. | ||
| 17 | // defined 400,401,403,404,405,500,502,503 and 504 default. | ||
| 15 | var HTTPExceptionMaps map[int]HTTPException | 18 | var HTTPExceptionMaps map[int]HTTPException |
| 16 | 19 | ||
| 17 | func init() { | 20 | func init() { | ... | ... |
-
Please register or sign in to post a comment