error.go 850 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package xweb
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. type AbortError struct {
  7. Code int
  8. Content string
  9. }
  10. func (a *AbortError) Error() string {
  11. return fmt.Sprintf("%v %v", a.Code, a.Content)
  12. }
  13. func Abort(code int, content ...string) error {
  14. if len(content) >= 1 {
  15. return &AbortError{code, content[0]}
  16. }
  17. return &AbortError{code, statusText[code]}
  18. }
  19. func NotFound(content ...string) error {
  20. return Abort(http.StatusNotFound, content...)
  21. }
  22. func NotSupported(content ...string) error {
  23. return Abort(http.StatusMethodNotAllowed, content...)
  24. }
  25. func InternalServerError(content ...string) error {
  26. return Abort(http.StatusInternalServerError, content...)
  27. }
  28. func Forbidden(content ...string) error {
  29. return Abort(http.StatusForbidden, content...)
  30. }
  31. func Unauthorized(content ...string) error {
  32. return Abort(http.StatusUnauthorized, content...)
  33. }