add more error functions
Showing
1 changed file
with
116 additions
and
36 deletions
| ... | @@ -210,42 +210,38 @@ func init() { | ... | @@ -210,42 +210,38 @@ func init() { |
| 210 | ErrorMaps = make(map[string]*errorInfo) | 210 | ErrorMaps = make(map[string]*errorInfo) |
| 211 | } | 211 | } |
| 212 | 212 | ||
| 213 | // show 404 notfound error. | 213 | // show 401 unauthorized error. |
| 214 | func NotFound(rw http.ResponseWriter, r *http.Request) { | 214 | func unauthorized(rw http.ResponseWriter, r *http.Request) { |
| 215 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 215 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 216 | data := make(map[string]interface{}) | 216 | data := make(map[string]interface{}) |
| 217 | data["Title"] = "Page Not Found" | 217 | data["Title"] = "Unauthorized" |
| 218 | data["Content"] = template.HTML("<br>The page you have requested has flown the coop." + | 218 | data["Content"] = template.HTML("<br>The page you have requested can't be authorized." + |
| 219 | "<br>Perhaps you are here because:" + | 219 | "<br>Perhaps you are here because:" + |
| 220 | "<br><br><ul>" + | 220 | "<br><br><ul>" + |
| 221 | "<br>The page has moved" + | 221 | "<br>The credentials you supplied are incorrect" + |
| 222 | "<br>The page no longer exists" + | 222 | "<br>There are errors in the website address" + |
| 223 | "<br>You were looking for your puppy and got lost" + | ||
| 224 | "<br>You like 404 pages" + | ||
| 225 | "</ul>") | 223 | "</ul>") |
| 226 | data["BeegoVersion"] = VERSION | 224 | data["BeegoVersion"] = VERSION |
| 227 | //rw.WriteHeader(http.StatusNotFound) | ||
| 228 | t.Execute(rw, data) | 225 | t.Execute(rw, data) |
| 229 | } | 226 | } |
| 230 | 227 | ||
| 231 | // show 401 unauthorized error. | 228 | // show 402 Payment Required |
| 232 | func Unauthorized(rw http.ResponseWriter, r *http.Request) { | 229 | func paymentRequired(rw http.ResponseWriter, r *http.Request) { |
| 233 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 230 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 234 | data := make(map[string]interface{}) | 231 | data := make(map[string]interface{}) |
| 235 | data["Title"] = "Unauthorized" | 232 | data["Title"] = "Payment Required" |
| 236 | data["Content"] = template.HTML("<br>The page you have requested can't be authorized." + | 233 | data["Content"] = template.HTML("<br>The page you have requested Payment Required." + |
| 237 | "<br>Perhaps you are here because:" + | 234 | "<br>Perhaps you are here because:" + |
| 238 | "<br><br><ul>" + | 235 | "<br><br><ul>" + |
| 239 | "<br>The credentials you supplied are incorrect" + | 236 | "<br>The credentials you supplied are incorrect" + |
| 240 | "<br>There are errors in the website address" + | 237 | "<br>There are errors in the website address" + |
| 241 | "</ul>") | 238 | "</ul>") |
| 242 | data["BeegoVersion"] = VERSION | 239 | data["BeegoVersion"] = VERSION |
| 243 | //rw.WriteHeader(http.StatusUnauthorized) | ||
| 244 | t.Execute(rw, data) | 240 | t.Execute(rw, data) |
| 245 | } | 241 | } |
| 246 | 242 | ||
| 247 | // show 403 forbidden error. | 243 | // show 403 forbidden error. |
| 248 | func Forbidden(rw http.ResponseWriter, r *http.Request) { | 244 | func forbidden(rw http.ResponseWriter, r *http.Request) { |
| 249 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 245 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 250 | data := make(map[string]interface{}) | 246 | data := make(map[string]interface{}) |
| 251 | data["Title"] = "Forbidden" | 247 | data["Title"] = "Forbidden" |
| ... | @@ -257,28 +253,43 @@ func Forbidden(rw http.ResponseWriter, r *http.Request) { | ... | @@ -257,28 +253,43 @@ func Forbidden(rw http.ResponseWriter, r *http.Request) { |
| 257 | "<br>You need to log in" + | 253 | "<br>You need to log in" + |
| 258 | "</ul>") | 254 | "</ul>") |
| 259 | data["BeegoVersion"] = VERSION | 255 | data["BeegoVersion"] = VERSION |
| 260 | //rw.WriteHeader(http.StatusForbidden) | ||
| 261 | t.Execute(rw, data) | 256 | t.Execute(rw, data) |
| 262 | } | 257 | } |
| 263 | 258 | ||
| 264 | // show 503 service unavailable error. | 259 | // show 404 notfound error. |
| 265 | func ServiceUnavailable(rw http.ResponseWriter, r *http.Request) { | 260 | func notFound(rw http.ResponseWriter, r *http.Request) { |
| 266 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 261 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 267 | data := make(map[string]interface{}) | 262 | data := make(map[string]interface{}) |
| 268 | data["Title"] = "Service Unavailable" | 263 | data["Title"] = "Page Not Found" |
| 269 | data["Content"] = template.HTML("<br>The page you have requested is unavailable." + | 264 | data["Content"] = template.HTML("<br>The page you have requested has flown the coop." + |
| 270 | "<br>Perhaps you are here because:" + | 265 | "<br>Perhaps you are here because:" + |
| 271 | "<br><br><ul>" + | 266 | "<br><br><ul>" + |
| 272 | "<br><br>The page is overloaded" + | 267 | "<br>The page has moved" + |
| 273 | "<br>Please try again later." + | 268 | "<br>The page no longer exists" + |
| 269 | "<br>You were looking for your puppy and got lost" + | ||
| 270 | "<br>You like 404 pages" + | ||
| 271 | "</ul>") | ||
| 272 | data["BeegoVersion"] = VERSION | ||
| 273 | t.Execute(rw, data) | ||
| 274 | } | ||
| 275 | |||
| 276 | // show 405 Method Not Allowed | ||
| 277 | func methodNotAllowed(rw http.ResponseWriter, r *http.Request) { | ||
| 278 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 279 | data := make(map[string]interface{}) | ||
| 280 | data["Title"] = "Method Not Allowed" | ||
| 281 | data["Content"] = template.HTML("<br>The method you have requested Not Allowed." + | ||
| 282 | "<br>Perhaps you are here because:" + | ||
| 283 | "<br><br><ul>" + | ||
| 284 | "<br>The method specified in the Request-Line is not allowed for the resource identified by the Request-URI" + | ||
| 285 | "<br>The response MUST include an Allow header containing a list of valid methods for the requested resource." + | ||
| 274 | "</ul>") | 286 | "</ul>") |
| 275 | data["BeegoVersion"] = VERSION | 287 | data["BeegoVersion"] = VERSION |
| 276 | //rw.WriteHeader(http.StatusServiceUnavailable) | ||
| 277 | t.Execute(rw, data) | 288 | t.Execute(rw, data) |
| 278 | } | 289 | } |
| 279 | 290 | ||
| 280 | // show 500 internal server error. | 291 | // show 500 internal server error. |
| 281 | func InternalServerError(rw http.ResponseWriter, r *http.Request) { | 292 | func internalServerError(rw http.ResponseWriter, r *http.Request) { |
| 282 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | 293 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 283 | data := make(map[string]interface{}) | 294 | data := make(map[string]interface{}) |
| 284 | data["Title"] = "Internal Server Error" | 295 | data["Title"] = "Internal Server Error" |
| ... | @@ -287,35 +298,104 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) { | ... | @@ -287,35 +298,104 @@ func InternalServerError(rw http.ResponseWriter, r *http.Request) { |
| 287 | "<br>Please try again later and report the error to the website administrator" + | 298 | "<br>Please try again later and report the error to the website administrator" + |
| 288 | "<br></ul>") | 299 | "<br></ul>") |
| 289 | data["BeegoVersion"] = VERSION | 300 | data["BeegoVersion"] = VERSION |
| 290 | //rw.WriteHeader(http.StatusInternalServerError) | ||
| 291 | t.Execute(rw, data) | 301 | t.Execute(rw, data) |
| 292 | } | 302 | } |
| 293 | 303 | ||
| 294 | // show 500 internal error with simple text string. | 304 | // show 501 Not Implemented. |
| 295 | func SimpleServerError(rw http.ResponseWriter, r *http.Request) { | 305 | func notImplemented(rw http.ResponseWriter, r *http.Request) { |
| 296 | http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | 306 | t, _ := template.New("beegoerrortemp").Parse(errtpl) |
| 307 | data := make(map[string]interface{}) | ||
| 308 | data["Title"] = "Not Implemented" | ||
| 309 | data["Content"] = template.HTML("<br>The page you have requested is Not Implemented." + | ||
| 310 | "<br><br><ul>" + | ||
| 311 | "<br>Please try again later and report the error to the website administrator" + | ||
| 312 | "<br></ul>") | ||
| 313 | data["BeegoVersion"] = VERSION | ||
| 314 | t.Execute(rw, data) | ||
| 315 | } | ||
| 316 | |||
| 317 | // show 502 Bad Gateway. | ||
| 318 | func badGateway(rw http.ResponseWriter, r *http.Request) { | ||
| 319 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 320 | data := make(map[string]interface{}) | ||
| 321 | data["Title"] = "Bad Gateway" | ||
| 322 | data["Content"] = template.HTML("<br>The page you have requested is down right now." + | ||
| 323 | "<br><br><ul>" + | ||
| 324 | "<br>The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request." + | ||
| 325 | "<br>Please try again later and report the error to the website administrator" + | ||
| 326 | "<br></ul>") | ||
| 327 | data["BeegoVersion"] = VERSION | ||
| 328 | t.Execute(rw, data) | ||
| 329 | } | ||
| 330 | |||
| 331 | // show 503 service unavailable error. | ||
| 332 | func serviceUnavailable(rw http.ResponseWriter, r *http.Request) { | ||
| 333 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 334 | data := make(map[string]interface{}) | ||
| 335 | data["Title"] = "Service Unavailable" | ||
| 336 | data["Content"] = template.HTML("<br>The page you have requested is unavailable." + | ||
| 337 | "<br>Perhaps you are here because:" + | ||
| 338 | "<br><br><ul>" + | ||
| 339 | "<br><br>The page is overloaded" + | ||
| 340 | "<br>Please try again later." + | ||
| 341 | "</ul>") | ||
| 342 | data["BeegoVersion"] = VERSION | ||
| 343 | t.Execute(rw, data) | ||
| 344 | } | ||
| 345 | |||
| 346 | // show 504 Gateway Timeout. | ||
| 347 | func gatewayTimeout(rw http.ResponseWriter, r *http.Request) { | ||
| 348 | t, _ := template.New("beegoerrortemp").Parse(errtpl) | ||
| 349 | data := make(map[string]interface{}) | ||
| 350 | data["Title"] = "Gateway Timeout" | ||
| 351 | data["Content"] = template.HTML("<br>The page you have requested is unavailable." + | ||
| 352 | "<br>Perhaps you are here because:" + | ||
| 353 | "<br><br><ul>" + | ||
| 354 | "<br><br>The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI." + | ||
| 355 | "<br>Please try again later." + | ||
| 356 | "</ul>") | ||
| 357 | data["BeegoVersion"] = VERSION | ||
| 358 | t.Execute(rw, data) | ||
| 297 | } | 359 | } |
| 298 | 360 | ||
| 299 | // register default error http handlers, 404,401,403,500 and 503. | 361 | // register default error http handlers, 404,401,403,500 and 503. |
| 300 | func registerDefaultErrorHandler() { | 362 | func registerDefaultErrorHandler() { |
| 301 | if _, ok := ErrorMaps["404"]; !ok { | 363 | if _, ok := ErrorMaps["401"]; !ok { |
| 302 | Errorhandler("404", NotFound) | 364 | Errorhandler("401", unauthorized) |
| 303 | } | 365 | } |
| 304 | 366 | ||
| 305 | if _, ok := ErrorMaps["401"]; !ok { | 367 | if _, ok := ErrorMaps["402"]; !ok { |
| 306 | Errorhandler("401", Unauthorized) | 368 | Errorhandler("402", paymentRequired) |
| 307 | } | 369 | } |
| 308 | 370 | ||
| 309 | if _, ok := ErrorMaps["403"]; !ok { | 371 | if _, ok := ErrorMaps["403"]; !ok { |
| 310 | Errorhandler("403", Forbidden) | 372 | Errorhandler("403", forbidden) |
| 311 | } | 373 | } |
| 312 | 374 | ||
| 313 | if _, ok := ErrorMaps["503"]; !ok { | 375 | if _, ok := ErrorMaps["404"]; !ok { |
| 314 | Errorhandler("503", ServiceUnavailable) | 376 | Errorhandler("404", notFound) |
| 377 | } | ||
| 378 | |||
| 379 | if _, ok := ErrorMaps["405"]; !ok { | ||
| 380 | Errorhandler("405", methodNotAllowed) | ||
| 315 | } | 381 | } |
| 316 | 382 | ||
| 317 | if _, ok := ErrorMaps["500"]; !ok { | 383 | if _, ok := ErrorMaps["500"]; !ok { |
| 318 | Errorhandler("500", InternalServerError) | 384 | Errorhandler("500", internalServerError) |
| 385 | } | ||
| 386 | if _, ok := ErrorMaps["501"]; !ok { | ||
| 387 | Errorhandler("501", notImplemented) | ||
| 388 | } | ||
| 389 | if _, ok := ErrorMaps["502"]; !ok { | ||
| 390 | Errorhandler("502", badGateway) | ||
| 391 | } | ||
| 392 | |||
| 393 | if _, ok := ErrorMaps["503"]; !ok { | ||
| 394 | Errorhandler("503", serviceUnavailable) | ||
| 395 | } | ||
| 396 | |||
| 397 | if _, ok := ErrorMaps["504"]; !ok { | ||
| 398 | Errorhandler("504", gatewayTimeout) | ||
| 319 | } | 399 | } |
| 320 | } | 400 | } |
| 321 | 401 | ... | ... |
-
Please register or sign in to post a comment