Files
gitlab-foss/lib/gitlab/utils/toml_parser.rb
2024-12-06 15:21:47 +00:00

25 lines
622 B
Ruby

# frozen_string_literal: true
module Gitlab
module Utils
class TomlParser
PARSE_TIMEOUT = 2.seconds
ParseError = Class.new(StandardError)
def self.safe_parse(content)
Timeout.timeout(PARSE_TIMEOUT) do
TomlRB.parse(content)
end
rescue TomlRB::ParseError
raise ParseError, 'content is not valid TOML'
rescue Timeout::Error => e
Gitlab::ErrorTracking.log_exception(e)
raise ParseError, 'timeout while parsing TOML'
rescue TomlRB::Error => e
raise ParseError, "error parsing TOML: #{e.message}"
end
end
end
end