Browse Source

default router

ls 5 years ago
parent
commit
c84e7c49bd
1 changed files with 31 additions and 0 deletions
  1. 31 0
      web/router.go

+ 31 - 0
web/router.go

@@ -0,0 +1,31 @@
+package web
+
+import (
+	"net/http"
+	"runtime"
+
+	"git.chuangxin1.com/cx/tyr"
+)
+
+// PanicHandler panic router
+func PanicHandler(w http.ResponseWriter, r *http.Request, err interface{}) {
+	var text string
+	e, ok := err.(runtime.Error)
+	if ok {
+		text = e.Error()
+	} else {
+		text = err.(string)
+	}
+
+	WriteJSON(w, tyr.ErrReplyData(tyr.ErrException, text))
+}
+
+// NotFoundHandler 404
+func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
+	WriteJSON(w, tyr.ErrReplyData(tyr.ErrNotFound, `NotFound`))
+}
+
+// NotAllowedHandler 405
+func NotAllowedHandler(w http.ResponseWriter, req *http.Request) {
+	WriteJSON(w, tyr.ErrReplyData(tyr.ErrNotAllowed, `Method Not Allowed`))
+}