Một số gem liên quan đến hiển thị lỗi và custom error page của Rails
Bài đăng này đã không được cập nhật trong 4 năm
1. Gem better_erros:
- Gem
better_errorsđược dùng để thay thế trang hiển thị lỗi mặc định của Rails với nhiều chức năng hữu ích hơn
a. Default error page:
- Trang hiển thị lỗi mặc định của Rails gồm các phấn như sau
- Dòng code gay ra lỗi
- Stack trace
- Error response
- Shell để run test code
b. Install gem better_erors:
- Install gem
better_errors# Gemfile gem "better_errors" gem "binding_of_caller"
c. Better errors page:
- Trang hiển thị lỗi của
better_errorsgồm các phần như sau
- Full stack trace
- Source code tương ứng với mỗi stack trace
- Variable được gọi và giá trị của variable trong stack trace
- Shell ứng với mỗi stack trace
- Link để mở source code của stack trace với editor tương ứng
- Link source code demo: https://github.com/thanhlt-1007/demo_better_errors
2. Gem exception_handler:
- Gem
exception_handlerđược dùng để thay thế trang hiển thị lỗi mặc định của Rails với dynamic views có thể custom được
a. Install gem exception_handler:
- Install gem
better_errors# Gemfile gem "exception_handler" - Enable gem
exception_handlerở môi trường developmennt# config/application.rb config.exception_handler = { dev: true }
b. Exception handler page:
- Trang hiển thị lỗi của
exception_handler![]()
Các option khác để custom exception handler:
i: DB:
- Sau khi install gem
exception_handlerta có thể chạyrails db:migrate - Gem
exception_handlersẽ update file schema, tạo tableerrorscó nội dung như sau# db/schema.rb create_table "errors", force: :cascade do |t| t.text "class_name" t.text "status" t.text "message" t.text "trace" t.text "target" t.text "referrer" t.text "params" t.text "user_agent" t.datetime "created_at", null: false t.datetime "updated_at", null: false end - Khi xảy ra lỗi, gem
exception_handlersẽ insert record vào tableerrors![]()
- Tạo model
Errorứng với tableerrors# app/models/error.rb class Error < ApplicationRecord end
ii. Views (layouts, locale):
- Màn hình error của gem
exception_handlerđược generate bởi actionshowcủaExceptionHandler::ExceptionsController-Bạn có thể check source code tại đây - Để custom layout, bạn có thể overrite lại action
showcủaExceptionHandler::ExceptionsControllerhoặc thay đổilayoutvàlocale
Custom locale
- Để custom lại locale bạn tạo file
en.yml# config/locales/en.yml en: exception_handler: not_found: "Your message here" # -> 404 page unauthorized: "You need to login to continue" internal_server_error: "This is a test to show the %{status} of the error" - Với key là english name của lỗi
- Bạn có thể tham khảo tại đây.
- Với mỗi message bạn có thể nhận 2 params là
%{message}và%{status}
Custom layout
- Theo mặn định thì error page của gem
exception_handlersẽ sử dụng layoutlayouts/exception - Bạn có thể check tại đây
- Bạn có thể overrite lại file layout này
- Hoặc sử dụng config để quy định layout theo 1 trong các hướng sau
- Sử dụng layout được quy định bởi
ApplicationController# config/application.rb config.exception_handler = { exceptions: { all: { layout: nil } } } - Quy định layout riêng cho từng lại exception theo HTTP status
# config/application.rb config.exception_handler = { exceptions: { all: { layout: 'exception' }, 5xx: { layout: 'server_error_exception' }, 4xx: { layout: 'client_error_exception' }, 500: { layout: 'internal_server_error_exception' } } }
iii. Generators:
- Để generate ra các file view của gem, bạn có thể chạy lênh sau
rails g exception_handler:views
All rights reserved



