Enhancements for Custom Error Handling and Configuration in Marten Framework #210
Replies: 2 comments 2 replies
-
Currently, for API apps, I would recommend other Crystal frameworks, like Athena and Spider-Gazelle (which can automatically generate OpenAPI spec from classes, and has many API-related features). Marten seems to be more of a HTML-oriented framework in my opinion, although @ellmetha may have a different stance on this. Sure, it would be great to have the level of API support in Marten as Spider-Gazelle has, but if there aren't enough resources, I think it's better to do 1 thing well (HTML) than 2 things partially. There are many features regarding HTML that need to be improved in order to be a production-ready framework. |
Beta Was this translation helpful? Give feedback.
-
Hey! 👋 Welcome here! Regarding (1) and (2) I am not sure Marten will be able to help much unfortunately. Marten is a generic framework with which APIs can be built, but it mainly focuses on relatively generic things (like the main error codes that you mentioned). There is no Marten shard available for helping with building REST APIs at the moment, but I hope that this is something that becomes a thing at one point (eg. something like Django-Rest-Framework for Marten would be awesome). It is worth mentioning that the default error handlers can be overridden if necessary (they are all associated with dedicated settings: Ultimately, I think Marten could return a JSON payload in these default error handlers in case the incoming HTTP request indicates that it accepts JSON (possibly an improvement that we can implement), but for full customization of the error format I would suggest overriding the error handlers directly as I was mentioning. As for handling very specific error codes (409, 429, etc), this is definitely something that should be implemented on an API-tooling shard or in projects directly (eg. custom middlewares) as these codes wouldn't match any existing exception in Marten anyway. Regarding (3), indeed error pages are bypassed in debug mode in order to help with the debugging process. That said, we could probably make it easier to access the error pages in debug mode in another way (without having to add development-specific routes in each project). I will think a bit more about this one, but it seems like an improvement that we could make! |
Beta Was this translation helpful? Give feedback.
-
Overview
Hello there, I hope all is well. I'm developing an API using the Marten framework and have encountered several limitations regarding error handling and its configuration, particularly around custom error codes, JSON error responses, and behavior in debug mode. Below, I detail these interconnected issues and suggest potential improvements.
1. Custom Error Code Handling
Currently, Marten supports handling of a limited set of HTTP error codes (404, 500, 403, 400). I require the ability to define custom handlers for additional HTTP status codes, such as 409, 429, 503 and more to handle specific API error conditions.
Expected Behavior:
2. Global Error Handling with JSON Responses
Marten's error handling seems primarily designed for HTML responses. For API development, a mechanism to globally handle errors and return them in JSON format is essential.
Expected Behavior:
3. Custom Handlers in Debug Mode
In debug mode, custom error handlers are bypassed in favor of default debug pages. For consistency in API error responses, it's crucial to allow custom handlers to function even in debug mode.
Expected Behavior:
Conclusion
Addressing these issues would significantly improve the flexibility and developer experience of using Marten for API development. I believe these enhancements could make Marten a more robust solution for building APIs, offering greater control over error handling and response formatting.
Thank you for considering these suggestions. I look forward to any feedback or discussion on these topics.
Beta Was this translation helpful? Give feedback.
All reactions