Search code examples
ruby-on-railsrubyrubocop

Rails - Rubocop - Begin + Rescue syntax


I have the following code:

  def payload
    begin
      @payload ||= Warden::JWTAuth::TokenDecoder.new.call(token)
    rescue JWT::ExpiredSignature => e
      Rollbar.warning(e)
    end
  end

From brief reading of a few blogs I'm supposed to be using begin rescue and end to handle the error as I'm doing above, however I'm getting a redundant 'begin' rubocop warning.

Is begin only used when specifying a bit of code that may cause an error within a larger block? And is it therefore redundant here?

Thanks in advance

EDIT: And if I don't need it, is it written as

  def payload
    @payload ||= Warden::JWTAuth::TokenDecoder.new.call(token)
  rescue JWT::ExpiredSignature => e
    Rollbar.warning(e)
  end

?


Solution

  • Do this when the begin would be the first thing in your method

    def payload
      @payload ||= Warden::JWTAuth::TokenDecoder.new.call(token)
    rescue JWT::ExpiredSignature => e
      Rollbar.warning(e)
    end