Rails 6 API Tutorial - Exception handling in controllers p.7

preview_player
Показать описание
In this video series we will build a Rails API from scratch. Backend APIs are useful for serving data to frontend applications, mobile apps or other backend services.

This video covers:
0:32 - Ruby exceptions
0:54 - Rescuing ActiveRecord::RecordNotDestroyed from destroy! with an inline rescue block
2:29 - The drawbacks of inline rescue for Rails controllers
3:56 - Using rescue_from to handle exceptions from any controller/action
5:36 - Moving the rescue_from to ApplicationController so that it handles exceptions from any controller in the application
6:38 - Rendering ActiveRecord object errors in the API response body

Рекомендации по теме
Комментарии
Автор

This is GOLD !!
I am currently creating a Rails API and this helped a lot m(_ _)m

minirasamedova
Автор

Amazing video, exactly what I was looking for, thanks for sharing your knowledge!

alejandrosuazabuiles
Автор

Thanks man, this series helped me a lot ❤

ahmedmustafa
Автор

Super clean and well explained! Thanks

jordan
Автор

Please do not run away using wrong code. =)). Instead of ActiveRecord::RecordNotDestroyed, please use ActiveRecord::RecordNotFound (according to your implementation)

class ApplicationController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

private

def record_not_found error
render json: { error: error }, status: :not_found
end
end

mdmahmudurrahman
Автор

how about exception handling on create actions within controllers

Dan-mceo
Автор

Tried using curl to delete an already deleted record and got a 404. How do I trigger this above error?

rtfm-inc