百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

safely

> 编程语言
开源

在非关键代码中处理异常并报告

565 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在非关键代码中处理异常并报告

Safely

ruby
safely do
  # keep going if this code fails
end

Exceptions are rescued and automatically reported to your favorite reporting service.

In development and test environments, exceptions are raised so you can fix them.

Read more

Installation

Add this line to your application’s Gemfile:

ruby
gem "safely_block"

Use It Everywhere

“Oh no, analytics brought down search”

ruby
safely { track_search(params) }

“Recommendations stopped updating because of one bad user”

ruby
users.each do |user|
  safely(context: {user_id: user.id}) { update_recommendations(user) }
end

Also aliased as yolo

Features

Pass extra context to be reported with exceptions

ruby
safely context: {user_id: 123} do
  # code
end

Specify a default value to return on exceptions

ruby
show_banner = safely(default: true) { show_banner_logic }

Raise specific exceptions

ruby
safely except: ActiveRecord::RecordNotUnique do
  # all other exceptions will be rescued
end

Pass an array for multiple exception classes.

Rescue only specific exceptions

ruby
safely only: ActiveRecord::RecordNotUnique do
  # all other exceptions will be raised
end

Silence exceptions

ruby
safely silence: ActiveRecord::RecordNotUnique do
  # code
end

Throttle reporting with:

ruby
safely throttle: {limit: 10, period: 1.minute} do
  # reports only first 10 exceptions each minute
end

Note: The throttle limit is approximate and per process.

Reporting

Reports exceptions to a variety of services out of the box.

  • Airbrake
  • Appsignal
  • Bugsnag
  • Datadog
  • Exception Notification
  • Google Stackdriver
  • Honeybadger
  • New Relic
  • Raygun
  • Rollbar
  • Scout APM
  • Sentry

Note: Context is not supported with Google Stackdriver and Scout APM

Customize reporting with:

ruby
Safely.report_exception_method = ->(e) { Rollbar.error(e) }

With Rails, you can add this in an initializer.

By default, exception messages are prefixed with [safely]. This makes it easier to spot rescued exceptions. Turn this off with:

ruby
Safely.tag = false

By default, exceptions are raised in the development and test environments. Change this with:

ruby
Safely.raise_envs += ["staging"]

To report exceptions manually:

ruby
Safely.report_exception(e)

Data Protection

To protect the privacy of your users, do not send personal data to exception services. Filter sensitive form fields, use ids (not email addresses) to identify users, and mask IP addresses.

With Rollbar, you can do:

ruby
Rollbar.configure do |config|
  config.person_id_method = "id" # default
  config.scrub_fields |= [:birthday]
  config.anonymize_user_ip = true
end

While working on exceptions, be on the lookout for personal data and correct as needed.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

  • Report bugs
  • Fix bugs and submit pull requests
  • Write, clarify, or fix documentation
  • Suggest or add new features

To get started with development and testing:

bash
git clone https://github.com/ankane/safely.git
cd safely
bundle install
bundle exec rake test

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言