mirror of
https://github.com/elyby/chrly.git
synced 2025-01-11 14:22:14 +05:30
Cleanup server error handling
This commit is contained in:
parent
bc4d714112
commit
d363433c88
@ -2,6 +2,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@ -54,7 +55,7 @@ func (ctx *Api) postProfileHandler(resp http.ResponseWriter, req *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
apiServerError(resp, "Unable to save profile to db", err)
|
apiServerError(resp, fmt.Errorf("unable to save profile to db: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +66,7 @@ func (ctx *Api) deleteProfileByUuidHandler(resp http.ResponseWriter, req *http.R
|
|||||||
uuid := mux.Vars(req)["uuid"]
|
uuid := mux.Vars(req)["uuid"]
|
||||||
err := ctx.ProfilesManager.RemoveProfileByUuid(uuid)
|
err := ctx.ProfilesManager.RemoveProfileByUuid(uuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
apiServerError(resp, "Unable to delete profile from db", err)
|
apiServerError(resp, fmt.Errorf("unable to delete profile from db: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -124,10 +123,9 @@ func apiBadRequest(resp http.ResponseWriter, errorsPerField map[string][]string)
|
|||||||
|
|
||||||
var internalServerError = []byte("Internal server error")
|
var internalServerError = []byte("Internal server error")
|
||||||
|
|
||||||
func apiServerError(resp http.ResponseWriter, msg string, err error) {
|
func apiServerError(resp http.ResponseWriter, err error) {
|
||||||
resp.WriteHeader(http.StatusInternalServerError)
|
resp.WriteHeader(http.StatusInternalServerError)
|
||||||
resp.Header().Set("Content-Type", "application/json")
|
resp.Header().Set("Content-Type", "text/plain")
|
||||||
slog.Error(msg, slog.Any("error", err))
|
|
||||||
_, _ = resp.Write(internalServerError)
|
_, _ = resp.Write(internalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -58,13 +59,12 @@ func (ctx *Skinsystem) Handler() *mux.Router {
|
|||||||
func (ctx *Skinsystem) skinHandler(response http.ResponseWriter, request *http.Request) {
|
func (ctx *Skinsystem) skinHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), parseUsername(mux.Vars(request)["username"]), true)
|
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), parseUsername(mux.Vars(request)["username"]), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
apiServerError(response, "Unable to retrieve a skin", err)
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if profile == nil || profile.SkinUrl == "" {
|
if profile == nil || profile.SkinUrl == "" {
|
||||||
response.WriteHeader(http.StatusNotFound)
|
response.WriteHeader(http.StatusNotFound)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(response, request, profile.SkinUrl, http.StatusMovedPermanently)
|
http.Redirect(response, request, profile.SkinUrl, http.StatusMovedPermanently)
|
||||||
@ -85,13 +85,12 @@ func (ctx *Skinsystem) skinGetHandler(response http.ResponseWriter, request *htt
|
|||||||
func (ctx *Skinsystem) capeHandler(response http.ResponseWriter, request *http.Request) {
|
func (ctx *Skinsystem) capeHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), parseUsername(mux.Vars(request)["username"]), true)
|
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), parseUsername(mux.Vars(request)["username"]), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
apiServerError(response, "Unable to retrieve a cape", err)
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if profile == nil || profile.CapeUrl == "" {
|
if profile == nil || profile.CapeUrl == "" {
|
||||||
response.WriteHeader(http.StatusNotFound)
|
response.WriteHeader(http.StatusNotFound)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(response, request, profile.CapeUrl, http.StatusMovedPermanently)
|
http.Redirect(response, request, profile.CapeUrl, http.StatusMovedPermanently)
|
||||||
@ -112,7 +111,7 @@ func (ctx *Skinsystem) capeGetHandler(response http.ResponseWriter, request *htt
|
|||||||
func (ctx *Skinsystem) texturesHandler(response http.ResponseWriter, request *http.Request) {
|
func (ctx *Skinsystem) texturesHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), mux.Vars(request)["username"], true)
|
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), mux.Vars(request)["username"], true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
apiServerError(response, "Unable to retrieve a profile", err)
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +139,7 @@ func (ctx *Skinsystem) signedTexturesHandler(response http.ResponseWriter, reque
|
|||||||
getToBool(request.URL.Query().Get("proxy")),
|
getToBool(request.URL.Query().Get("proxy")),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
apiServerError(response, "Unable to retrieve a profile", err)
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,7 +177,7 @@ func (ctx *Skinsystem) signedTexturesHandler(response http.ResponseWriter, reque
|
|||||||
func (ctx *Skinsystem) profileHandler(response http.ResponseWriter, request *http.Request) {
|
func (ctx *Skinsystem) profileHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), mux.Vars(request)["username"], true)
|
profile, err := ctx.ProfilesProvider.FindProfileByUsername(request.Context(), mux.Vars(request)["username"], true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
apiServerError(response, "Unable to retrieve a profile", err)
|
apiServerError(response, fmt.Errorf("unable to retrieve a profile: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,7 +204,7 @@ func (ctx *Skinsystem) profileHandler(response http.ResponseWriter, request *htt
|
|||||||
if request.URL.Query().Has("unsigned") && !getToBool(request.URL.Query().Get("unsigned")) {
|
if request.URL.Query().Has("unsigned") && !getToBool(request.URL.Query().Get("unsigned")) {
|
||||||
signature, err := ctx.TexturesSigner.SignTextures(texturesProp.Value)
|
signature, err := ctx.TexturesSigner.SignTextures(texturesProp.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
apiServerError(response, "Unable to sign textures", err)
|
apiServerError(response, fmt.Errorf("unable to sign textures: %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user