blob: d819f2c350354267e17e45be163e5bb93037e56f [file]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package common
import (
"encoding/json"
"errors"
"log/slog"
"net/http"
"reflect"
"strings"
"github.com/apache/airavata-custos/pkg/service"
)
func DecodeJSON(r *http.Request, dst any) error {
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
return dec.Decode(dst)
}
func WriteJSON(w http.ResponseWriter, status int, body any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if body == nil {
return
}
// A nil slice would encode as JSON null; list endpoints must return [].
if v := reflect.ValueOf(body); v.Kind() == reflect.Slice && v.IsNil() {
body = []any{}
}
_ = json.NewEncoder(w).Encode(body)
}
func WriteError(w http.ResponseWriter, status int, err error) {
WriteJSON(w, status, map[string]string{"error": err.Error()})
}
func WriteServiceError(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, service.ErrNotFound):
WriteError(w, http.StatusNotFound, err)
case errors.Is(err, service.ErrAlreadyExists):
WriteError(w, http.StatusConflict, err)
case errors.Is(err, service.ErrInvalidInput):
WriteError(w, http.StatusBadRequest, err)
default:
// Avoid leaking driver messages to clients; log the full error.
slog.Error("internal server error", "error", err.Error())
WriteError(w, http.StatusInternalServerError, errors.New(strings.TrimSpace("internal server error")))
}
}