2022-11-11 20:36:45 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2022-11-11 20:36:45 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-04-01 11:04:21 +02:00
|
|
|
"strings"
|
2022-11-11 20:36:45 +01:00
|
|
|
"time"
|
2023-03-30 10:54:09 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2022-11-11 20:36:45 +01:00
|
|
|
)
|
|
|
|
|
2024-12-10 15:59:25 +01:00
|
|
|
// EmptyObject is an empty struct used to return empty JSON object
|
|
|
|
type EmptyObject struct {
|
|
|
|
}
|
|
|
|
|
2023-07-14 20:45:40 +02:00
|
|
|
type ErrorResponse struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Code int `json:"code"`
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:11:16 +01:00
|
|
|
// WriteJSONObject simply writes object to the HTTP response in JSON format
|
2024-07-03 11:33:02 +02:00
|
|
|
func WriteJSONObject(ctx context.Context, w http.ResponseWriter, obj interface{}) {
|
2022-11-11 20:36:45 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
2024-04-25 21:20:24 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-11-11 20:36:45 +01:00
|
|
|
err := json.NewEncoder(w).Encode(obj)
|
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
WriteError(ctx, err, w)
|
2022-11-11 20:36:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration is used strictly for JSON requests/responses due to duration marshalling issues
|
|
|
|
type Duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals the duration
|
|
|
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(d.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals the duration
|
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch value := v.(type) {
|
|
|
|
case float64:
|
|
|
|
d.Duration = time.Duration(value)
|
|
|
|
return nil
|
|
|
|
case string:
|
|
|
|
var err error
|
|
|
|
d.Duration, err = time.ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("invalid duration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteErrorResponse prepares and writes an error response i nJSON
|
|
|
|
func WriteErrorResponse(errMsg string, httpStatus int, w http.ResponseWriter) {
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
2024-04-25 21:20:24 +02:00
|
|
|
w.WriteHeader(httpStatus)
|
2023-07-14 20:45:40 +02:00
|
|
|
err := json.NewEncoder(w).Encode(&ErrorResponse{
|
2022-11-11 20:36:45 +01:00
|
|
|
Message: errMsg,
|
|
|
|
Code: httpStatus,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "failed handling request", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteError converts an error to an JSON error response.
|
|
|
|
// If it is known internal error of type server.Error then it sets the messages from the error, a generic message otherwise
|
2024-07-03 11:33:02 +02:00
|
|
|
func WriteError(ctx context.Context, err error, w http.ResponseWriter) {
|
|
|
|
log.WithContext(ctx).Errorf("got a handler error: %s", err.Error())
|
2022-11-11 20:36:45 +01:00
|
|
|
errStatus, ok := status.FromError(err)
|
|
|
|
httpStatus := http.StatusInternalServerError
|
|
|
|
msg := "internal server error"
|
|
|
|
if ok {
|
|
|
|
switch errStatus.Type() {
|
|
|
|
case status.UserAlreadyExists:
|
|
|
|
httpStatus = http.StatusConflict
|
|
|
|
case status.AlreadyExists:
|
|
|
|
httpStatus = http.StatusConflict
|
|
|
|
case status.PreconditionFailed:
|
|
|
|
httpStatus = http.StatusPreconditionFailed
|
|
|
|
case status.PermissionDenied:
|
|
|
|
httpStatus = http.StatusForbidden
|
|
|
|
case status.NotFound:
|
|
|
|
httpStatus = http.StatusNotFound
|
|
|
|
case status.Internal:
|
|
|
|
httpStatus = http.StatusInternalServerError
|
|
|
|
case status.InvalidArgument:
|
|
|
|
httpStatus = http.StatusUnprocessableEntity
|
2023-03-30 10:54:09 +02:00
|
|
|
case status.Unauthorized:
|
|
|
|
httpStatus = http.StatusUnauthorized
|
2024-03-27 18:48:48 +01:00
|
|
|
case status.BadRequest:
|
|
|
|
httpStatus = http.StatusBadRequest
|
2022-11-11 20:36:45 +01:00
|
|
|
default:
|
|
|
|
}
|
2023-04-01 11:04:21 +02:00
|
|
|
msg = strings.ToLower(err.Error())
|
2022-11-11 20:36:45 +01:00
|
|
|
} else {
|
|
|
|
unhandledMSG := fmt.Sprintf("got unhandled error code, error: %s", err.Error())
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Error(unhandledMSG)
|
2022-11-11 20:36:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WriteErrorResponse(msg, httpStatus, w)
|
|
|
|
}
|