mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-28 11:51:53 +01:00
refactor(http): remove and replace error helpers with standard http.Error
This commit is contained in:
@@ -2,13 +2,10 @@ package gphttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"syscall"
|
||||
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/httpheaders"
|
||||
)
|
||||
|
||||
@@ -33,76 +30,3 @@ func ServerError(w http.ResponseWriter, r *http.Request, err error, code ...int)
|
||||
}
|
||||
http.Error(w, http.StatusText(code[0]), code[0])
|
||||
}
|
||||
|
||||
// ClientError is for responding to client errors.
|
||||
//
|
||||
// It returns http.StatusBadRequest with reason to the client.
|
||||
// Status code can be specified as an argument.
|
||||
//
|
||||
// For JSON marshallable errors (e.g. gperr.Error), it returns the error details as JSON.
|
||||
// Otherwise, it returns the error details as plain text.
|
||||
func ClientError(w http.ResponseWriter, r *http.Request, err error, code ...int) {
|
||||
if len(code) == 0 {
|
||||
code = []int{http.StatusBadRequest}
|
||||
}
|
||||
w.WriteHeader(code[0])
|
||||
accept := GetAccept(r.Header)
|
||||
switch {
|
||||
case accept.AcceptJSON():
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(err)
|
||||
case accept.AcceptMarkdown():
|
||||
w.Header().Set("Content-Type", "text/markdown")
|
||||
w.Write(gperr.Markdown(err))
|
||||
default:
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Write(gperr.Plain(err))
|
||||
}
|
||||
}
|
||||
|
||||
// JSONError returns a JSON response of gperr.Error with the given status code.
|
||||
func JSONError(w http.ResponseWriter, err gperr.Error, code int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(err)
|
||||
}
|
||||
|
||||
// BadRequest returns a Bad Request response with the given error message.
|
||||
func BadRequest(w http.ResponseWriter, err string, code ...int) {
|
||||
if len(code) == 0 {
|
||||
code = []int{http.StatusBadRequest}
|
||||
}
|
||||
w.WriteHeader(code[0])
|
||||
w.Write([]byte(err))
|
||||
}
|
||||
|
||||
// Unauthorized returns an Unauthorized response with the given error message.
|
||||
func Unauthorized(w http.ResponseWriter, err string) {
|
||||
BadRequest(w, err, http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
// Forbidden returns a Forbidden response with the given error message.
|
||||
func Forbidden(w http.ResponseWriter, err string) {
|
||||
BadRequest(w, err, http.StatusForbidden)
|
||||
}
|
||||
|
||||
// NotFound returns a Not Found response with the given error message.
|
||||
func NotFound(w http.ResponseWriter, err string) {
|
||||
BadRequest(w, err, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func MissingKey(w http.ResponseWriter, k string) {
|
||||
BadRequest(w, k+" is required", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func InvalidKey(w http.ResponseWriter, k string) {
|
||||
BadRequest(w, k+" is invalid", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func KeyAlreadyExists(w http.ResponseWriter, k, v string) {
|
||||
BadRequest(w, fmt.Sprintf("%s %q already exists", k, v), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
func ValueNotFound(w http.ResponseWriter, k, v string) {
|
||||
BadRequest(w, fmt.Sprintf("%s %q not found", k, v), http.StatusNotFound)
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func PreRequest(p Provider, w http.ResponseWriter, r *http.Request) (proceed boo
|
||||
}
|
||||
|
||||
if !gphttp.GetAccept(r.Header).AcceptHTML() {
|
||||
gphttp.Forbidden(w, "Captcha is required")
|
||||
http.Error(w, "Captcha is required", http.StatusForbidden)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -45,7 +45,9 @@ func PreRequest(p Provider, w http.ResponseWriter, r *http.Request) (proceed boo
|
||||
http.Redirect(w, r, r.URL.Path, http.StatusFound)
|
||||
return false
|
||||
}
|
||||
gphttp.Unauthorized(w, err.Error())
|
||||
|
||||
log.Warn().Err(err).Str("url", r.URL.String()).Str("remote_addr", r.RemoteAddr).Msg("failed to verify captcha")
|
||||
http.Error(w, "Failed to verify captcha", http.StatusUnauthorized)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user