| 246 | } |
| 247 | |
| 248 | func (fs *FakeServer) sendErrorResponse(w http.ResponseWriter, id interface{}, code int, message string) { |
| 249 | fs.mu.Lock() |
| 250 | fs.requestsFailed++ |
| 251 | fs.mu.Unlock() |
| 252 | |
| 253 | response := JSONRPCResponse{ |
| 254 | Jsonrpc: "2.0", |
| 255 | Error: map[string]interface{}{"code": code, "message": message}, |
| 256 | ID: id, |
| 257 | } |
| 258 | w.Header().Set("Content-Type", "application/json") |
| 259 | buf := &bytes.Buffer{} |
| 260 | if err := json.NewEncoder(buf).Encode(response); err != nil { |
| 261 | log.Error().Err(err).Msg("Failed to encode response") |
| 262 | return |
| 263 | } |
| 264 | bt := buf.Bytes() |
| 265 | if len(bt) > 1024 { |
| 266 | log.Info().Str("response", string(bt[:1024])).Msg("Response sent (partial)") |
| 267 | } else { |
| 268 | log.Info().Str("response", string(bt)).Msg("Response sent") |
| 269 | } |
| 270 | w.Write(bt) |
| 271 | } |
| 272 | |
| 273 | func (fs *FakeServer) findMatchingSample(req JSONRPCRequest) *JSONRPCResponse { |
| 274 | for _, sample := range fs.samples { |