mirror of
https://github.com/Dvorinka/Devour.git
synced 2026-06-03 20:13:03 +00:00
i dont like commits
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -129,11 +130,19 @@ func (s *HTTPServer) Start(ctx context.Context) error {
|
||||
defer r.Body.Close()
|
||||
var req rpcRequest
|
||||
if err := json.NewDecoder(io.LimitReader(r.Body, 2<<20)).Decode(&req); err != nil {
|
||||
writeRPC(w, rpcResponse{JSONRPC: "2.0", Error: &rpcError{Code: -32700, Message: "parse error"}})
|
||||
if writeErr := writeRPC(w, rpcResponse{JSONRPC: "2.0", Error: &rpcError{Code: -32700, Message: "parse error"}}); writeErr != nil {
|
||||
wrapped := wrapTransportError("http", "encode parse-error response", writeErr)
|
||||
log.Printf("%v", wrapped)
|
||||
http.Error(w, wrapped.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
resp := s.handleRPC(r.Context(), req)
|
||||
writeRPC(w, resp)
|
||||
if err := writeRPC(w, resp); err != nil {
|
||||
wrapped := wrapTransportError("http", "encode rpc response", err)
|
||||
log.Printf("%v", wrapped)
|
||||
http.Error(w, wrapped.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
|
||||
host := s.config.Host
|
||||
@@ -156,10 +165,10 @@ func (s *HTTPServer) Start(ctx context.Context) error {
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
_ = s.http.Shutdown(shutdownCtx)
|
||||
return ctx.Err()
|
||||
return wrapTransportError("http", "server context canceled", ctx.Err())
|
||||
case err := <-errCh:
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
return err
|
||||
return wrapTransportError("http", "listen and serve", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -210,17 +219,19 @@ func (s *StdioServer) Start(ctx context.Context) error {
|
||||
|
||||
var req rpcRequest
|
||||
if err := json.Unmarshal([]byte(line), &req); err != nil {
|
||||
_ = out.Encode(rpcResponse{JSONRPC: "2.0", Error: &rpcError{Code: -32700, Message: "parse error"}})
|
||||
if encodeErr := out.Encode(rpcResponse{JSONRPC: "2.0", Error: &rpcError{Code: -32700, Message: "parse error"}}); encodeErr != nil {
|
||||
return wrapTransportError("stdio", "encode parse-error response", encodeErr)
|
||||
}
|
||||
continue
|
||||
}
|
||||
resp := handleRPC(ctx, s.config.Handler, req)
|
||||
if err := out.Encode(resp); err != nil {
|
||||
return err
|
||||
return wrapTransportError("stdio", "encode rpc response", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
return wrapTransportError("stdio", "scan stdin", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -247,10 +258,17 @@ func handleRPC(ctx context.Context, handler MethodHandler, req rpcRequest) rpcRe
|
||||
return rpcResponse{JSONRPC: "2.0", ID: req.ID, Result: result}
|
||||
}
|
||||
|
||||
func writeRPC(w http.ResponseWriter, payload rpcResponse) {
|
||||
func writeRPC(w http.ResponseWriter, payload rpcResponse) error {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if payload.Error != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(payload)
|
||||
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
||||
return fmt.Errorf("encode rpc response: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func wrapTransportError(transport, operation string, err error) error {
|
||||
return fmt.Errorf("%s rpc %s failed: %w", transport, operation, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user