Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
35 lines
632 B
Go
35 lines
632 B
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type errorEnvelope struct {
|
|
Error apiError `json:"error"`
|
|
}
|
|
|
|
type apiError struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
Details any `json:"details"`
|
|
}
|
|
|
|
func writeError(w http.ResponseWriter, status int, code, message string, details any) {
|
|
writeJSON(w, status, errorEnvelope{
|
|
Error: apiError{
|
|
Code: code,
|
|
Message: message,
|
|
Details: details,
|
|
},
|
|
})
|
|
}
|
|
|
|
func decodeJSON(r *http.Request, dest any) error {
|
|
defer func() {
|
|
if err := r.Body.Close(); err != nil {
|
|
return
|
|
}
|
|
}()
|
|
return json.NewDecoder(r.Body).Decode(dest)
|
|
}
|