Test infrastructure: - Django standalone test runner (pytest-django, test settings, EmailUser model) - React unit tests via Vitest with jsdom, jest compat layer, path aliases - Playwright E2E tests using generated hooks in a real Chromium browser - Docker Compose test backend (Django + Redis) for integration testing - Desktop integration test app (PyWebView + Django + uvicorn) - Makefile with test/test-django/test-react/test-integration targets Library bugs found and fixed: - hasJWT truthiness: undefined !== null was true, skipping session init - process.env crash: CSR client referenced process.env in non-Node browsers - baseUrl not forwarded: DjareaProvider didn't pass baseUrl to CSR client - Relative URL handling: new URL() failed with relative base paths - call() race condition: HTTP requests fired before CSRF cookie was set - Session init await: added sessionRef promise so call() waits for session - path_prefix on schema export: both export commands failed with URL reverse - NullBooleanField removed: referenced field doesn't exist in Django 5.0+ - lru_cache on JWT settings: get_settings() now cached as intended - Channel message routing: broadcasts now include channel name and params - httpFunctionCall: fixed URL and request body format Generator fixes: - Removed 1,100 lines of REST/OpenAPI client generation (not part of Djarea) - Generator now works for djarea-only projects without django-ninja REST APIs - Generated DjangoContext now includes ChannelProvider when channels exist - Fixed env var passthrough for schema export commands - Deduplicated fetch logic into single runDjangoCommand helper Test quality: - Fixed 33 tautological Django tests with real assertions - Found hidden bug: benchmark functions were never registered - Found hidden bug: unicode lookalike test used plain ASCII - Deleted worthless React unit tests (duplicates, shape checks, Zod-tests-Zod) - Replaced jsdom integration tests with Playwright browser tests Example apps: - example/: Integration test backend with 33 server functions, 5 forms, 4 channels covering auth variations, contexts, class-based ServerFunction, error codes, DjareaFormMixin, formsets, and JWT - desktop/: PyWebView desktop app with file system access, SQLite CRUD, system introspection, and 39 real HTTP integration tests Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
108 lines
2.4 KiB
TypeScript
108 lines
2.4 KiB
TypeScript
/**
|
|
* Django Server Error Types
|
|
*
|
|
* Typed errors for server function failures.
|
|
*/
|
|
|
|
/**
|
|
* Error codes returned by the server
|
|
*/
|
|
export type ErrorCode =
|
|
| 'NOT_FOUND'
|
|
| 'VALIDATION_ERROR'
|
|
| 'UNAUTHORIZED'
|
|
| 'FORBIDDEN'
|
|
| 'BAD_REQUEST'
|
|
| 'INTERNAL_ERROR'
|
|
| 'NOT_IMPLEMENTED'
|
|
|
|
/**
|
|
* Error response structure from the server
|
|
*/
|
|
export interface FunctionErrorResponse {
|
|
error: true
|
|
code: ErrorCode
|
|
message: string
|
|
details?: {
|
|
fields?: Record<string, string[]>
|
|
required?: string[]
|
|
type?: string
|
|
[key: string]: unknown
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Error thrown when a server function call fails
|
|
*/
|
|
export class DjangoError extends Error {
|
|
/**
|
|
* Error code from the server
|
|
*/
|
|
readonly code: ErrorCode
|
|
|
|
/**
|
|
* Additional error details
|
|
*/
|
|
readonly details?: FunctionErrorResponse['details']
|
|
|
|
/**
|
|
* The original error response
|
|
*/
|
|
readonly response: FunctionErrorResponse
|
|
|
|
constructor(response: FunctionErrorResponse) {
|
|
super(response.message)
|
|
this.name = 'DjangoError'
|
|
this.code = response.code
|
|
this.details = response.details
|
|
this.response = response
|
|
|
|
// Maintains proper stack trace for where error was thrown
|
|
if (Error.captureStackTrace) {
|
|
Error.captureStackTrace(this, DjangoError)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if this is a validation error
|
|
*/
|
|
isValidationError(): boolean {
|
|
return this.code === 'VALIDATION_ERROR'
|
|
}
|
|
|
|
/**
|
|
* Check if this is an authentication error
|
|
*/
|
|
isAuthError(): boolean {
|
|
return this.code === 'UNAUTHORIZED' || this.code === 'FORBIDDEN'
|
|
}
|
|
|
|
/**
|
|
* Check if this is a not found error
|
|
*/
|
|
isNotFound(): boolean {
|
|
return this.code === 'NOT_FOUND'
|
|
}
|
|
|
|
/**
|
|
* Get field-level validation errors (if this is a validation error)
|
|
*/
|
|
getFieldErrors(): Record<string, string[]> | null {
|
|
if (this.code === 'VALIDATION_ERROR' && this.details?.fields) {
|
|
return this.details.fields
|
|
}
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* Get error for a specific field
|
|
*/
|
|
getFieldError(field: string): string | null {
|
|
const errors = this.getFieldErrors()
|
|
if (errors && errors[field]?.length > 0) {
|
|
return errors[field][0]
|
|
}
|
|
return null
|
|
}
|
|
}
|