CodeShow

Ruby

raise_error

class Response
  def initialize(code = Code::SUCCESS, message = '', messages = [])
    @code = code
    @message = message
    @messages = messages
    @uuid = RequestStore.store[:request]&.uuid || SecureRandom.uuid
  end
  
  # 幽灵函数
  def method_missing(method_id, *arguments, &block)
    method_message = *arguments.join
    if (method_id.to_s =~ /^raise_[\w]+/) == 0
      error_type = method_id.to_s.split('raise_')[1].upcase!
      @code = "Response::Code::#{error_type}".constantize
      @message = method_message
      error_messages = arguments.first
      @messages = error_messages if error_messages.is_a?(Array)

      yield if block_given?

      raise StandardError.new(method_message)
    else
      super
    end
  end

  def self.rescue(catch_block = nil)
    response = self.new
    begin
      yield(response)
    rescue AtyunError => e
      log(e)
      catch_block.call if catch_block.present?
      response.code = Code::CUSTOM_ERROR
      response.message = e.message
    rescue => e
      log(e)
      catch_block.call if catch_block.present?
      response.code = Code::ERROR
      response.message = e.message
    end

    response
  end
end

rescue_error

catch_error

默认参数选项

循环

循环缓存

测量消耗

事件回调

随机等待重试

向量化

Rails

懒事务

软删除

大表添加索引

batch_update

先记一个bug

tap会改变self

一定要注意对底层属性的更改操作,最好使用拷贝对象

关联替换

有Record如下:

其实例方法替换关联对象:

多数据源

间隔下载

机器人指令

最后更新于

这有帮助吗?