Commit 475d2edf authored by Grzegorz Bizon's avatar Grzegorz Bizon

Reorganize exceptions in pipeline expressions module

parent bbba6d7e
module Gitlab
module Ci
module Pipeline
module Expression
ExpressionError = Class.new(StandardError)
RuntimeError = Class.new(ExpressionError)
end
end
end
end
...@@ -11,10 +11,9 @@ module Gitlab ...@@ -11,10 +11,9 @@ module Gitlab
end end
def evaluate(variables = {}) def evaluate(variables = {})
# TODO multiline support
@regexp = Gitlab::UntrustedRegexp.new(@value) @regexp = Gitlab::UntrustedRegexp.new(@value)
rescue RegexpError rescue RegexpError
raise Parser::ParserError, 'Invalid regular expression!' raise Expression::RuntimeError, 'Invalid regular expression!'
end end
def self.build(string) def self.build(string)
......
...@@ -5,6 +5,8 @@ module Gitlab ...@@ -5,6 +5,8 @@ module Gitlab
class Lexer class Lexer
include ::Gitlab::Utils::StrongMemoize include ::Gitlab::Utils::StrongMemoize
SyntaxError = Class.new(Expression::ExpressionError)
LEXEMES = [ LEXEMES = [
Expression::Lexeme::Variable, Expression::Lexeme::Variable,
Expression::Lexeme::String, Expression::Lexeme::String,
...@@ -12,8 +14,6 @@ module Gitlab ...@@ -12,8 +14,6 @@ module Gitlab
Expression::Lexeme::Equals Expression::Lexeme::Equals
].freeze ].freeze
SyntaxError = Class.new(Statement::StatementError)
MAX_TOKENS = 100 MAX_TOKENS = 100
def initialize(statement, max_tokens: MAX_TOKENS) def initialize(statement, max_tokens: MAX_TOKENS)
......
...@@ -3,8 +3,6 @@ module Gitlab ...@@ -3,8 +3,6 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
class Parser class Parser
ParserError = Class.new(Statement::StatementError)
def initialize(tokens) def initialize(tokens)
@tokens = tokens.to_enum @tokens = tokens.to_enum
@nodes = [] @nodes = []
......
...@@ -3,7 +3,7 @@ module Gitlab ...@@ -3,7 +3,7 @@ module Gitlab
module Pipeline module Pipeline
module Expression module Expression
class Statement class Statement
StatementError = Class.new(StandardError) StatementError = Class.new(Expression::ExpressionError)
GRAMMAR = [ GRAMMAR = [
%w[variable equals string], %w[variable equals string],
...@@ -35,13 +35,13 @@ module Gitlab ...@@ -35,13 +35,13 @@ module Gitlab
def truthful? def truthful?
evaluate.present? evaluate.present?
rescue StatementError rescue Expression::ExpressionError
false false
end end
def valid? def valid?
parse_tree.is_a?(Lexeme::Base) parse_tree.is_a?(Lexeme::Base)
rescue StatementError rescue Expression::ExpressionError
false false
end end
end end
......
...@@ -56,7 +56,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do ...@@ -56,7 +56,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
regexp = described_class.new('invalid ( .*') regexp = described_class.new('invalid ( .*')
expect { regexp.evaluate } expect { regexp.evaluate }
.to raise_error(Gitlab::Ci::Pipeline::Expression::Parser::ParserError) .to raise_error(Gitlab::Ci::Pipeline::Expression::RuntimeError)
end end
end end
end end
...@@ -6,7 +6,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do ...@@ -6,7 +6,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
end end
describe '#tokens' do describe '#tokens' do
it 'tokenss single value' do it 'returns single value' do
tokens = described_class.new('$VARIABLE').tokens tokens = described_class.new('$VARIABLE').tokens
expect(tokens).to be_one expect(tokens).to be_one
...@@ -20,14 +20,14 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do ...@@ -20,14 +20,14 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect(tokens).to all(be_an_instance_of(token_class)) expect(tokens).to all(be_an_instance_of(token_class))
end end
it 'tokenss multiple values of the same token' do it 'returns multiple values of the same token' do
tokens = described_class.new("$VARIABLE1 $VARIABLE2").tokens tokens = described_class.new("$VARIABLE1 $VARIABLE2").tokens
expect(tokens.size).to eq 2 expect(tokens.size).to eq 2
expect(tokens).to all(be_an_instance_of(token_class)) expect(tokens).to all(be_an_instance_of(token_class))
end end
it 'tokenss multiple values with different tokens' do it 'returns multiple values with different tokens' do
tokens = described_class.new('$VARIABLE "text" "value"').tokens tokens = described_class.new('$VARIABLE "text" "value"').tokens
expect(tokens.size).to eq 3 expect(tokens.size).to eq 3
...@@ -36,7 +36,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do ...@@ -36,7 +36,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect(tokens.third.value).to eq '"value"' expect(tokens.third.value).to eq '"value"'
end end
it 'tokenss tokens and operators' do it 'returns tokens and operators' do
tokens = described_class.new('$VARIABLE == "text"').tokens tokens = described_class.new('$VARIABLE == "text"').tokens
expect(tokens.size).to eq 3 expect(tokens.size).to eq 3
......
...@@ -36,7 +36,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -36,7 +36,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
'== "123"', # invalid left side '== "123"', # invalid left side
'"some string"', # only string provided '"some string"', # only string provided
'$VAR ==', # invalid right side '$VAR ==', # invalid right side
'12345', # unknown syntax 'null', # missing lexemes
'' # empty statement '' # empty statement
] ]
...@@ -44,7 +44,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do ...@@ -44,7 +44,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
context "when expression grammar is #{syntax.inspect}" do context "when expression grammar is #{syntax.inspect}" do
let(:text) { syntax } let(:text) { syntax }
it 'aises a statement error exception' do it 'raises a statement error exception' do
expect { subject.parse_tree } expect { subject.parse_tree }
.to raise_error described_class::StatementError .to raise_error described_class::StatementError
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment