Comments (4)
Also relevant to the Railtie vs Mixin issue. One use case: imagine globally rescuing a NotAuthorized
error with 403 and generic message. But if you're in Admin::PostsController
, you want a custom error message for this internal functionality, maybe with details on who to contact to get privileges. This is another advantage to registering errors on the controller instead of globally.
from graphiti-rails.
@richmolj I think we may still have a slight disconnect. We're not really registering handlers globally from a developer perspective. The fact that we are using GraphitiErrors.register_exception
behind the scenes is an implementation detail that the developer should not have to be concerned about. My concern is to have a default handler for JSON:API (and other types that a developer may opt-in to) and it so happens that a good way to do that is to register a handler with GraphitiErrors.
The Rails approach is to directly use rescue_from
for exceptions that you want to handle. I'd like to do the same and not also require the developer to call a second API (e.g. register_exception
for this to work correctly).
from graphiti-rails.
To rewrite the code in the graphiti_errors_spec.rb, something like this:
def render_graphiti_exception(exception, handler: nil, **options)
raise ArgumentError if handler && !options.empty?
handler ||= GraphitiErrors::ExceptionHandler.new(options)
handler.log(exception)
json = handler.error_payload(exception)
status = handler.status_code(exception)
render json: json, status: status
end
rescue_from(CustomStatusError) { |e| render_graphiti_exception(e, status: 301) }
rescue_from(CustomTitleError) { |e| render_graphiti_exception(e, title: "My Title") }
rescue_from(MessageTrueError) { |e| render_graphiti_exception(e, message: true) }
rescue_from(MessageProcError) do |e|
render_graphiti_exception(e, message: ->(e) { e.class.name.upcase })
end
rescue_from(MetaProcError) do |e|
render_graphiti_exception(e, meta: ->(e) { {class_name: e.class.name.upcase} })
end
rescue_from(LogFalseError) { |e| render_graphiti_exception(e, log: false) }
rescue_from(CustomHandlerError) do |e|
render_graphiti_exception(e, handler: CustomErrorHandler)
end
This seems to me to be the Rails approach, so the thing to do would basically be to make render_graphiti_exception
built-in.
from graphiti-rails.
Addressed by #22
from graphiti-rails.
Related Issues (20)
- Ensure JSON:API spec compliance for exceptions HOT 1
- Use `action_dispatch.backtrace_cleaner` for detailed traces HOT 1
- Move Graphiti::Railtie here HOT 1
- [REGRESSION] Return `__raw_error__` for Vandal and Remote Resources
- Auto-include `Responders` module if responders gem is present HOT 1
- Add config option to omit __details__ HOT 1
- `context_for_endpoint` doesn't work if you have routing constraints HOT 1
- ResourceGenerator overwrites existing controller HOT 1
- Generated file namespacing incorrect after updating namespace
- Exception handling HOT 9
- Failure when log_level is a symbol HOT 1
- My attribute values are saved as NULL
- Include Graphiti stuff only in api HOT 11
- Error responses missing exception message details HOT 7
- Don't set `format` when parsing parameters HOT 4
- Move more tests here HOT 2
- Attribute text not support in generator HOT 1
- Unable to override resource or controller templates
- Sideloads randomly stop getting included in responses
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphiti-rails.