router.go 673 B

12345678910111213141516171819202122232425262728293031
  1. package web
  2. import (
  3. "net/http"
  4. "runtime"
  5. "git.chuangxin1.com/cx/tyr"
  6. )
  7. // PanicHandler panic router
  8. func PanicHandler(w http.ResponseWriter, r *http.Request, err interface{}) {
  9. var text string
  10. e, ok := err.(runtime.Error)
  11. if ok {
  12. text = e.Error()
  13. } else {
  14. text = err.(string)
  15. }
  16. WriteJSON(w, tyr.ErrReplyData(tyr.ErrException, text))
  17. }
  18. // NotFoundHandler 404
  19. func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
  20. WriteJSON(w, tyr.ErrReplyData(tyr.ErrNotFound, `NotFound`))
  21. }
  22. // NotAllowedHandler 405
  23. func NotAllowedHandler(w http.ResponseWriter, req *http.Request) {
  24. WriteJSON(w, tyr.ErrReplyData(tyr.ErrNotAllowed, `Method Not Allowed`))
  25. }