Troubleshooting and FAQ
This section provides solutions to common issues you might encounter while using Chanfana, frequently asked questions, debugging tips, and resources for getting help and support.
Common Issues and Solutions
1. "TypeError: Router.get is not a function" or similar errors when using fromHono or fromIttyRouter:
Cause: You might be calling
fromHonoorfromIttyRouteron an object that is not a valid Hono or itty-router router instance.Solution: Ensure that you are passing a valid router instance to
fromHonoorfromIttyRouter. Double-check your router initialization code:typescript// Hono example: import { Hono } from 'hono'; const app = new Hono(); // Correct: Initialize Hono router const openapi = fromHono(app, {/* options */}); // Itty-router example: import { Router } from 'itty-router'; const router = Router(); // Correct: Initialize itty-router const openapi = fromIttyRouter(router, {/* options */});
2. OpenAPI documentation is not showing up at /docs or /openapi.json:
Cause 1: You haven't registered any routes with Chanfana's
openapiinstance.Solution: Make sure you are registering your
OpenAPIRouteclasses using methods likeopenapi.get(),openapi.post(), etc., and not directly on the original router instance (app.get()in Hono orrouter.get()in itty-router after initialization with Chanfana).typescript// Correct: Register route with Chanfana's openapi instance openapi.get('/my-endpoint', MyEndpointClass); // Incorrect: Registering directly on the Hono app instance (OpenAPI not enabled for this route) app.get('/another-endpoint', () => new Response("Hello"));Cause 2: You have set
docs_url,redoc_url, oropenapi_urltonullinRouterOptions.Solution: Check your
RouterOptionsand ensure thatdocs_url,redoc_url, andopenapi_urlare set to valid URL paths (strings) if you want to enable documentation UIs and schema endpoints.Cause 3: Your application is not running or is not accessible at the expected address and port.
Solution: Verify that your application is running correctly and is accessible in your browser or using
curlat the URL where you expect to see the documentation.
3. Request validation errors are not being handled as expected:
Cause 1: You are not defining request schemas in your
OpenAPIRouteclasses.Solution: Ensure that you have defined request schemas (e.g.,
schema.request.body,schema.request.query, etc.) in yourOpenAPIRouteclasses for the endpoints where you want request validation to be performed.Cause 2: You are catching and handling
ZodErrorexceptions manually in yourhandlemethod, potentially overriding Chanfana's default error handling.Solution: In most cases, you should not manually catch
ZodErrorexceptions within yourhandlemethod. Let Chanfana automatically handle validation errors and return400 Bad Requestresponses. Only catch exceptions if you need to perform custom error handling logic for specific error types other than validation errors.
4. "TypeError: Cannot read properties of undefined (reading 'schema')" or similar errors related to _meta:
Cause: You are using auto endpoints (
CreateEndpoint,ReadEndpoint, etc.) without properly defining the_metaproperty in your endpoint class.Solution: When using auto endpoints, you must define the
_metaproperty and assign a validMetaobject to it. Ensure that yourMetaobject includes themodelproperty withschema,primaryKeys, andtableName(if applicable).typescriptclass MyCreateEndpoint extends CreateEndpoint { _meta = { // Correct: Define _meta property model: { schema: MyDataModel, primaryKeys: ['id'], tableName: 'my_table', }, }; // ... }
5. OpenAPI schema is missing descriptions or examples:
Cause: You have not provided descriptions or examples in your Zod schemas or Chanfana parameter types.
Solution: Use Zod's
describe()method and Chanfana parameter type options likedescriptionandexampleto add metadata to your schemas. This metadata is used to generate more informative OpenAPI documentation.typescriptconst nameSchema = Str({ description: 'User name', example: 'John Doe' }); // Add description and example const ageSchema = Int({ description: 'User age' }); // Add description
6. D1 endpoints are not working, "Binding 'DB' is not defined in worker" error:
- Cause: You have not correctly configured the D1 database binding in your
wrangler.tomlfile, or the binding name in your code (dbName = 'DB') does not match the binding name inwrangler.toml. - Solution:
- Verify your
wrangler.tomlfile and ensure that you have a[[d1_databases]]section with abindingname (e.g.,binding = "DB"). - Make sure that the
dbNameproperty in your D1 endpoint class matches thebindingname inwrangler.toml(case-sensitive). - Ensure that you have deployed your Cloudflare Worker or are running it in a local environment where the D1 binding is correctly set up.
- Verify your
Frequently Asked Questions (FAQ)
Q: Can I use Chanfana with routers other than Hono and itty-router?
A: Chanfana is designed to be router-agnostic in principle. While it provides official adapters for Hono and itty-router, you can potentially create custom adapters for other routers. However, creating a custom adapter might require a deeper understanding of Chanfana's internals and the API of the target router.
Q: Does Chanfana support OpenAPI 3.1?
A: Yes, Chanfana fully supports OpenAPI 3.1 (which is the default) and also OpenAPI 3.0.3. You can select the OpenAPI version using the openapiVersion option in RouterOptions.
Q: Can I customize the generated OpenAPI document beyond the RouterOptions?
A: Yes, you can customize the OpenAPI schema output extensively using Zod's describe() and openapi() methods, as well as Chanfana's parameter type options. For more advanced customizations or modifications to the generated OpenAPI document structure, you might need to extend or modify Chanfana's core classes (which is generally not recommended unless you have a deep understanding of the library).
Q: Is Chanfana suitable for production APIs?
A: Yes, Chanfana is considered stable and production-ready. It is used in production at Cloudflare and powers public APIs like Radar 2.0.
Q: Does Chanfana support authentication and authorization?
A: Chanfana itself does not provide built-in authentication or authorization mechanisms. However, it is designed to work seamlessly with middleware and custom logic for implementing authentication and authorization in your API endpoints. Refer to the Examples and Recipes section for a basic example of API key authentication.
Q: How can I handle different content types (e.g., XML, plain text) in request and responses?
A: Chanfana primarily focuses on JSON APIs and provides contentJson for simplifying JSON content handling. For handling other content types like XML or plain text, you might need to:
- Manually define OpenAPI
contentobjects in your schema without usingcontentJson. - Use Zod schemas that are appropriate for the data format (e.g.,
z.string()for plain text). - Handle request parsing and response serialization for non-JSON content types within your endpoint
handlemethods using router-specific or Fetch API methods.
(Future versions of Chanfana might introduce more utilities for handling different content types.)
Debugging Tips
- Check Browser Console: When using Swagger UI or ReDoc, inspect the browser's developer console for any JavaScript errors or warnings that might indicate issues with the documentation rendering or OpenAPI schema.
- Validate OpenAPI Schema: Use online OpenAPI validators (e.g., Swagger Editor, ReDoc online validator) to validate your generated
openapi.jsonschema file for any structural or syntax errors. - Verbose Logging: Add
console.logstatements in your endpointhandlemethods and middleware to trace the request flow, data validation, and error handling logic. - Simplify and Isolate: If you encounter complex issues, try to simplify your endpoint definitions and isolate the problematic part of your code to narrow down the source of the error.
- Example Projects: Refer to the example projects and code snippets in the documentation and Chanfana's repository for working examples and best practices.
Getting Help and Support
- Chanfana GitHub Repository: https://github.com/cloudflare/chanfana/ - Check the repository for issues, discussions, and updates.
- Cloudflare Developer Community: https://community.cloudflare.com/ - Ask questions and seek help from the Cloudflare developer community.
- Radar Discord Channel: https://discord.com/channels/595317990191398933/1035553707116478495 - Join the Radar Discord channel for discussions and support related to Chanfana and Radar API development.
- Report Issues: If you encounter bugs or have feature requests, please open an issue in the Chanfana GitHub repository.