@@ -13,8 +13,8 @@ import (
1313 "github.com/lazypower/continuity/internal/engine"
1414)
1515
16- // jsonError writes a JSON error response. All error responses should use this
17- // to avoid JSON injection via string concatenation .
16+ // jsonError writes a JSON error response with proper Content-Type and encoding.
17+ // Prefer this over http.Error for consistent JSON responses .
1818func jsonError (w http.ResponseWriter , msg string , code int ) {
1919 w .Header ().Set ("Content-Type" , "application/json" )
2020 w .WriteHeader (code )
@@ -27,11 +27,11 @@ func (s *Server) handleSessionInit(w http.ResponseWriter, r *http.Request) {
2727 Project string `json:"project"`
2828 }
2929 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
30- http . Error (w , `{"error":" invalid json"}` , http .StatusBadRequest )
30+ jsonError (w , " invalid json" , http .StatusBadRequest )
3131 return
3232 }
3333 if req .SessionID == "" {
34- http . Error (w , `{"error":" session_id required"}` , http .StatusBadRequest )
34+ jsonError (w , " session_id required" , http .StatusBadRequest )
3535 return
3636 }
3737
@@ -60,11 +60,11 @@ func (s *Server) handleAddObservation(w http.ResponseWriter, r *http.Request) {
6060 }
6161 body , err := io .ReadAll (r .Body )
6262 if err != nil {
63- http . Error (w , `{"error":" read body failed"}` , http .StatusBadRequest )
63+ jsonError (w , " read body failed" , http .StatusBadRequest )
6464 return
6565 }
6666 if err := json .Unmarshal (body , & req ); err != nil {
67- http . Error (w , `{"error":" invalid json"}` , http .StatusBadRequest )
67+ jsonError (w , " invalid json" , http .StatusBadRequest )
6868 return
6969 }
7070
@@ -119,7 +119,7 @@ func (s *Server) handleExtractSession(w http.ResponseWriter, r *http.Request) {
119119 Force bool `json:"force"`
120120 }
121121 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
122- http . Error (w , `{"error":" invalid json"}` , http .StatusBadRequest )
122+ jsonError (w , " invalid json" , http .StatusBadRequest )
123123 return
124124 }
125125
@@ -155,11 +155,11 @@ func (s *Server) handleSignal(w http.ResponseWriter, r *http.Request) {
155155 Prompt string `json:"prompt"`
156156 }
157157 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
158- http . Error (w , `{"error":" invalid json"}` , http .StatusBadRequest )
158+ jsonError (w , " invalid json" , http .StatusBadRequest )
159159 return
160160 }
161161 if req .Prompt == "" {
162- http . Error (w , `{"error":" prompt required"}` , http .StatusBadRequest )
162+ jsonError (w , " prompt required" , http .StatusBadRequest )
163163 return
164164 }
165165
@@ -248,11 +248,11 @@ func (s *Server) handleRemember(w http.ResponseWriter, r *http.Request) {
248248 SessionID string `json:"session_id"`
249249 }
250250 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
251- http . Error (w , `{"error":" invalid json"}` , http .StatusBadRequest )
251+ jsonError (w , " invalid json" , http .StatusBadRequest )
252252 return
253253 }
254254 if req .Category == "" || req .Name == "" || req .Summary == "" || req .Body == "" {
255- http . Error (w , `{"error":" category, name, summary, and body are required"}` , http .StatusBadRequest )
255+ jsonError (w , " category, name, summary, and body are required" , http .StatusBadRequest )
256256 return
257257 }
258258
@@ -295,7 +295,7 @@ func (s *Server) handleRemember(w http.ResponseWriter, r *http.Request) {
295295func (s * Server ) handleSearch (w http.ResponseWriter , r * http.Request ) {
296296 query := r .URL .Query ().Get ("q" )
297297 if query == "" {
298- http . Error (w , `{"error":" q parameter required"}` , http .StatusBadRequest )
298+ jsonError (w , " q parameter required" , http .StatusBadRequest )
299299 return
300300 }
301301
0 commit comments