# Query Error Implementation Parity Check

## Context

Module: {{MODULE_NAME}}
Query docs: {{QUERY_DOCS}}
Error definitions: {{ERROR_DEFS}}
Query code: {{QUERY_CODE}}
Test code: {{QUERY_TEST_CODE}}

## Instructions

1. Read ALL query docs at the paths above
2. Read the error definitions file at the path above
3. Read ALL query code files at the paths above
4. Read ALL test code files at the paths above
5. For each documented error scenario, trace through generated class → query return → test assertion
6. Run every parity check below
7. Return results as JSON per the Output Format section

## Extraction: Query Docs

From each query doc's Error Scenarios section, extract:

- **Error code**: e.g. `INSUFFICIENT_PERMISSION`, `INVALID_SCOPE`
- **Condition**: when this error is returned
- **Expected class name**: derived from error code (PascalCase + `Error` suffix)

## Extraction: Error Definitions

From `lib/errors.generated.ts`, extract:

- **Error class names**: exported error classes
- **Error codes**: the code constant in each class
- **Error message patterns**: message templates

See [errors.md](errors.md) for error generation patterns.

## Extraction: Query Code

From each query file (`query/*.ts`, excluding `*.test.ts` and `*.generated.ts`), extract:

- **Error imports**: which error classes are imported from `../lib/errors.generated`
- **Error returns**: `return err(new XError(...))` statements
- **Error codes used**: which error codes appear in the code
- **Permission checks**: `requirePermission(ctx, ...)` calls that return errors
- **Scope checks**: scope authorization checks that return errors

## Extraction: Test Code

From each test file (`query/*.test.ts`), extract:

- **Error assertions**: tests that check for specific error types or codes
- **Error test descriptions**: test names that reference error scenarios
- **Authorization test cases**: tests verifying permission/scope rejections

## Parity Checks

For each error scenario documented in query docs:

| Check ID              | Question                                                                                                                                     |
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| error_class_generated | Does the error class exist in `errors.generated.ts`?                                                                                         |
| error_code_accuracy   | Does the generated error code equal `{MODULE_PREFIX}_{DOC_CODE}`? The generator prefixes doc codes with the module name in UPPER_SNAKE_CASE. |
| error_returned        | Does the query code return this error via `err(new XError(...))`?                                                                            |
| error_import          | Does the query code import the error class from `errors.generated`?                                                                          |
| error_test_exists     | Does a test case assert this error scenario?                                                                                                 |

### How to Check

1. List all error codes from query docs
2. Verify each has a generated class in `errors.generated.ts`
3. Check query code imports and returns the error
4. Check test file has an assertion for this error

## Common Gap Patterns

- **Missing error class**: Error code in doc but no generated class (re-run `erp-kit module generate code -p <path>`)
- **Error not returned**: Generated class exists but query code doesn't return it
- **Error not imported**: Query uses inline error instead of generated class
- **Missing error test**: Error is returned in code but no test asserts it
- **Error code mismatch**: Doc code differs from generated/code code
- **Missing permission error**: Doc requires permission but no `requirePermission` call in code
- **Missing scope error**: Doc describes scope restriction but no scope check in code

## Output Format

Return a JSON object:

```json
{
  "check_type": "query-error-implementation-parity",
  "module": "{{MODULE_NAME}}",
  "gaps": [...],
  "inconsistencies": [...],
  "summary": { "total_checks": N, "passed": N, "failed": N, "skipped": N }
}
```

See [impl-parity-report-format.md](impl-parity-report-format.md) for field definitions.
