MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestErrorHandling

Function TestErrorHandling

examples/server/memory/kb_test.go:334–376  ·  view source on GitHub ↗

TestErrorHandling verifies proper error responses for invalid operations.

(t *testing.T)

Source from the content-addressed store, hash-verified

332
333// TestErrorHandling verifies proper error responses for invalid operations.
334func TestErrorHandling(t *testing.T) {
335 t.Run("FileStoreWriteError", func(t *testing.T) {
336 // Test file write to invalid path
337 kb := knowledgeBase{
338 s: &fileStore{path: filepath.Join("nonexistent", "directory", "file.json")},
339 }
340
341 testEntities := []Entity{
342 {Name: "TestEntity"},
343 }
344
345 _, err := kb.createEntities(testEntities)
346 if err == nil {
347 t.Errorf("expected error when writing to non-existent directory, got nil")
348 }
349 })
350
351 for name, newStore := range stores() {
352 t.Run(fmt.Sprintf("AddObservationToNonExistentEntity_%s", name), func(t *testing.T) {
353 s := newStore(t)
354 kb := knowledgeBase{s: s}
355
356 // Setup valid entity for comparison
357 _, err := kb.createEntities([]Entity{{Name: "RealEntity"}})
358 if err != nil {
359 t.Fatalf("failed to create test entity: %v", err)
360 }
361
362 // Test invalid entity reference
363 nonExistentObs := []Observation{
364 {
365 EntityName: "NonExistentEntity",
366 Contents: []string{"This shouldn't work"},
367 },
368 }
369
370 _, err = kb.addObservations(nonExistentObs)
371 if err == nil {
372 t.Errorf("expected error when adding observations to non-existent entity, got nil")
373 }
374 })
375 }
376}
377
378// TestFileFormatting verifies the JSON storage format structure.
379func TestFileFormatting(t *testing.T) {

Callers

nothing calls this directly

Calls 4

createEntitiesMethod · 0.95
addObservationsMethod · 0.95
storesFunction · 0.85
RunMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…