hazycora
06a35520c3
All checks were successful
Deploy to VPS / build_site (push) Successful in 4s
how did this bug last this long
75 lines
2.6 KiB
Go
75 lines
2.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.gay/gitgay/pages/errors"
|
|
"git.gay/gitgay/pages/render"
|
|
"git.gay/gitgay/pages/utils"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type Error struct {
|
|
Message string
|
|
Status int
|
|
LongMessage string
|
|
Unknown bool
|
|
}
|
|
|
|
type ErrorPageData struct {
|
|
Meta render.MetaTags
|
|
PAGES_DOMAIN string
|
|
BASE_URL string
|
|
Error Error
|
|
}
|
|
|
|
func ErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
|
|
log.Error().Err(err).Str("host", r.Host).Str("path", r.URL.Path).Msg("Error response")
|
|
page := ErrorPageData{
|
|
PAGES_DOMAIN: utils.PAGES_DOMAIN,
|
|
BASE_URL: utils.BASE_URL,
|
|
Error: Error{
|
|
Message: "Something went wrong",
|
|
Status: 500,
|
|
},
|
|
}
|
|
switch err := err.(type) {
|
|
case *errors.ErrorNotFound, *errors.ErrorCouldNotGetTree:
|
|
page.Error.Message = "Not found"
|
|
page.Error.Status = 404
|
|
case *errors.ErrorGetUserByName:
|
|
page.Error.Message = "Could not find this user"
|
|
page.Error.Status = 404
|
|
page.Error.LongMessage = fmt.Sprintf("Are you sure your username is \"%s\"?", err.Username)
|
|
case *errors.ErrorDNSNotConfigured:
|
|
page.Error.Message = "DNS not configured"
|
|
page.Error.LongMessage = fmt.Sprintf("You need either a CNAME or TXT record of the format BRANCH.REPO.USERNAME.%s.\nkeep in mind some DNS providers such as Cloudflare will at times flatten CNAME records, in which case you'll need to use a TXT record instead.", utils.PAGES_DOMAIN)
|
|
case *errors.ErrorInvalidTarget:
|
|
page.Error.Message = "Invalid target"
|
|
if strings.HasSuffix(strings.ToLower(r.Host), utils.PAGES_DOMAIN) {
|
|
page.Error.LongMessage = fmt.Sprintf("Page domains must follow the format BRANCH.REPO.USERNAME.%s", utils.PAGES_DOMAIN)
|
|
} else {
|
|
page.Error.LongMessage = fmt.Sprintf("Your custom domain TXT / CNAME records must follow the format BRANCH.REPO.USERNAME.%s", utils.PAGES_DOMAIN)
|
|
}
|
|
case *errors.ErrorNotPointedAtUs:
|
|
page.Error.Message = "Not pointed at us"
|
|
page.Error.LongMessage = "pages.gay's servers seem to think this domain doesn't lead to our servers, yet here we are. maybe wait a bit for DNS to update everywhere?"
|
|
default:
|
|
page.Error.LongMessage = "This is likely to be a bug in pages.gay itself. please report it!"
|
|
page.Error.Unknown = true
|
|
}
|
|
w.WriteHeader(page.Error.Status)
|
|
page.Meta.Title = fmt.Sprintf("%d - %s", page.Error.Status, page.Error.Message)
|
|
if page.Error.LongMessage != "" {
|
|
page.Meta.Description = page.Error.LongMessage
|
|
}
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
err = render.Template(w, "error.tmpl", page)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("Failed to render error page")
|
|
w.Write([]byte(fmt.Sprintf("Error: %s", err)))
|
|
}
|
|
}
|