mirror of
https://git.freecumextremist.com/grumbulon/pomme.git
synced 2024-11-13 06:44:12 +00:00
better and safer type assertions
This commit is contained in:
parent
98824d031c
commit
2c75a27482
2 changed files with 34 additions and 9 deletions
|
@ -62,6 +62,11 @@ func (e *Response[T]) apiError(w http.ResponseWriter, r *http.Request) {
|
|||
"error": e.Err,
|
||||
}
|
||||
|
||||
message, ok := v["message"].(string)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
|
@ -72,7 +77,7 @@ func (e *Response[T]) apiError(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
fallthrough
|
||||
case nil:
|
||||
w.Header().Add("API Error", v["message"].(string))
|
||||
w.Header().Add("API Error", message)
|
||||
}
|
||||
|
||||
w.WriteHeader(v["status"].(int))
|
||||
|
@ -87,21 +92,31 @@ func (e *Response[T]) apiError(w http.ResponseWriter, r *http.Request) {
|
|||
// writeLogEntry takes a response struct and writes info and error level logs
|
||||
// todo: make it write to file maybe
|
||||
func (e *Response[T]) writeLogEntry() {
|
||||
logger := httplog.NewLogger("Pomme", httplog.Options{
|
||||
JSON: true,
|
||||
})
|
||||
|
||||
v := map[string]any{
|
||||
"message": e.Message,
|
||||
"error": e.Err,
|
||||
}
|
||||
|
||||
logger := httplog.NewLogger("Pomme", httplog.Options{
|
||||
JSON: true,
|
||||
})
|
||||
errorString, ok := v["error"].(string)
|
||||
if !ok {
|
||||
logger.Error().Msg("unable to convert any type 'error' to string")
|
||||
}
|
||||
|
||||
message, ok := v["message"].(string)
|
||||
if !ok {
|
||||
logger.Error().Msg("unable to convert any type 'message' to string")
|
||||
}
|
||||
|
||||
switch v["error"] {
|
||||
default:
|
||||
logger.Error().Msg(v["error"].(string))
|
||||
logger.Error().Msg(errorString)
|
||||
|
||||
fallthrough
|
||||
case nil:
|
||||
logger.Info().Msg(v["message"].(string))
|
||||
logger.Info().Msg(message)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,7 +83,17 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
zoneFile := newDNSRequest(header.Filename, claims["username"].(string), b)
|
||||
stringClaimsUName, ok := claims["username"].(string)
|
||||
if !ok {
|
||||
logger := newResponder(Response[any]{
|
||||
Message: "unable to convert any type claim to string",
|
||||
Status: http.StatusInternalServerError,
|
||||
})
|
||||
logger.apiError(w, r)
|
||||
logger.writeLogEntry()
|
||||
}
|
||||
|
||||
zoneFile := newDNSRequest(header.Filename, stringClaimsUName, b)
|
||||
|
||||
if err := zoneFile.parse(); err != nil {
|
||||
logger := newResponder(Response[any]{
|
||||
|
@ -111,7 +121,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// check if request is coming from user not in the DB but has a valid JWT
|
||||
db.Where("username = ?", claims["username"].(string)).First(&result)
|
||||
db.Where("username = ?", stringClaimsUName).First(&result)
|
||||
|
||||
if result.Username == "" {
|
||||
logger := newResponder(Response[any]{
|
||||
|
@ -125,7 +135,7 @@ func ReceiveFile(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
db.Create(
|
||||
&ZoneRequest{
|
||||
User: claims["username"].(string),
|
||||
User: stringClaimsUName,
|
||||
Zone: &Zone{
|
||||
FileName: header.Filename,
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue