TestMiddlewareScenario demonstrates a realistic middleware scenario
(t *testing.T)
| 389 | |
| 390 | // TestMiddlewareScenario demonstrates a realistic middleware scenario |
| 391 | func TestMiddlewareScenario(t *testing.T) { |
| 392 | t.Run("realistic middleware error collection scenario", func(t *testing.T) { |
| 393 | // Simulate a realistic HTTP middleware scenario |
| 394 | |
| 395 | // 1. Request comes in, middleware sets up error tracking |
| 396 | ctx := ContextWithGitHubErrors(context.Background()) |
| 397 | |
| 398 | // 2. Middleware stores reference to context for later inspection |
| 399 | var middlewareCtx context.Context |
| 400 | setupMiddleware := func(ctx context.Context) context.Context { |
| 401 | middlewareCtx = ctx |
| 402 | return ctx |
| 403 | } |
| 404 | |
| 405 | // 3. Setup middleware |
| 406 | ctx = setupMiddleware(ctx) |
| 407 | |
| 408 | // 4. Simulate multiple service calls that add errors |
| 409 | simulateServiceCall1 := func(ctx context.Context) { |
| 410 | resp := &github.Response{Response: &http.Response{StatusCode: 403}} |
| 411 | _, err := NewGitHubAPIErrorToCtx(ctx, "insufficient permissions", resp, fmt.Errorf("forbidden")) |
| 412 | require.NoError(t, err) |
| 413 | } |
| 414 | |
| 415 | simulateServiceCall2 := func(ctx context.Context) { |
| 416 | resp := &github.Response{Response: &http.Response{StatusCode: 404}} |
| 417 | _, err := NewGitHubAPIErrorToCtx(ctx, "resource not found", resp, fmt.Errorf("not found")) |
| 418 | require.NoError(t, err) |
| 419 | } |
| 420 | |
| 421 | simulateGraphQLCall := func(ctx context.Context) { |
| 422 | gqlErr := newGitHubGraphQLError("mutation failed", fmt.Errorf("invalid input")) |
| 423 | _, err := addGitHubGraphQLErrorToContext(ctx, gqlErr) |
| 424 | require.NoError(t, err) |
| 425 | } |
| 426 | |
| 427 | // 5. Execute service calls (without context propagation) |
| 428 | simulateServiceCall1(ctx) |
| 429 | simulateServiceCall2(ctx) |
| 430 | simulateGraphQLCall(ctx) |
| 431 | |
| 432 | // 6. Middleware inspects errors at the end of request processing |
| 433 | finalizeMiddleware := func(ctx context.Context) ([]string, []string) { |
| 434 | var apiErrorMessages []string |
| 435 | var gqlErrorMessages []string |
| 436 | |
| 437 | if apiErrors, err := GetGitHubAPIErrors(ctx); err == nil { |
| 438 | for _, apiErr := range apiErrors { |
| 439 | apiErrorMessages = append(apiErrorMessages, apiErr.Message) |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if gqlErrors, err := GetGitHubGraphQLErrors(ctx); err == nil { |
| 444 | for _, gqlErr := range gqlErrors { |
| 445 | gqlErrorMessages = append(gqlErrorMessages, gqlErr.Message) |
| 446 | } |
| 447 | } |
| 448 |
nothing calls this directly
no test coverage detected