Exception policy for Rails API applications -
i want know general policy how manage exceptions in rails api application.
i suppose client should receive exceptions related customer's logic. in controller i'm catching these kind of exceptions , sending message of it.
mainapplicationexception
top of hierarchy exception class exceptions in application.
class mycontroller < applicationcontroller def some_action ... # processing of request rescue mainapplicationexception => error render: { :message => error.message } end end end
but sure there're big chances exception raised while processing incoming requests.
how should register exceptions , in application should rescue these exceptions? should set these message log? there tutorial kind of problem?
exception handling hard! in general, , when building out api it's of import have grasp of http status codes. trivial illustration of 1 404. illustration you're doing company.find(params[:id]
throw activerecord::recordnotfound , can caught way in application controller: rescue_from activerecord::recordnotfound, :with => :not_found
the same goes illustration info have process, if format(e.g. you're receiving 'application/xml' instead of 'application/json.') not take can throw an 406.
the hardest part handling user data, of import question arises here how much want inform user? much info might expose scheme potential hacks.
personally find controller place handle exceptions if don't impact other parts of code. otherwise nice way go might builder pattern allows compose response without handling errors in controller.
there loads of books on exception handling simple introduction on handling api errors , collection of resources might this blog post
hope helps.
ruby-on-rails exception design policy
No comments:
Post a Comment