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
end
def evaluate(variables = {})
# TODO multiline support
@regexp = Gitlab::UntrustedRegexp.new(@value)
rescue RegexpError
raise Parser::ParserError, 'Invalid regular expression!'
raise Expression::RuntimeError, 'Invalid regular expression!'
end
def self.build(string)
......
......@@ -5,6 +5,8 @@ module Gitlab
class Lexer
include ::Gitlab::Utils::StrongMemoize
SyntaxError = Class.new(Expression::ExpressionError)
LEXEMES = [
Expression::Lexeme::Variable,
Expression::Lexeme::String,
......@@ -12,8 +14,6 @@ module Gitlab
Expression::Lexeme::Equals
].freeze
SyntaxError = Class.new(Statement::StatementError)
MAX_TOKENS = 100
def initialize(statement, max_tokens: MAX_TOKENS)
......
......@@ -3,8 +3,6 @@ module Gitlab
module Pipeline
module Expression
class Parser
ParserError = Class.new(Statement::StatementError)
def initialize(tokens)
@tokens = tokens.to_enum
@nodes = []
......
......@@ -3,7 +3,7 @@ module Gitlab
module Pipeline
module Expression
class Statement
StatementError = Class.new(StandardError)
StatementError = Class.new(Expression::ExpressionError)
GRAMMAR = [
%w[variable equals string],
......@@ -35,13 +35,13 @@ module Gitlab
def truthful?
evaluate.present?
rescue StatementError
rescue Expression::ExpressionError
false
end
def valid?
parse_tree.is_a?(Lexeme::Base)
rescue StatementError
rescue Expression::ExpressionError
false
end
end
......
......@@ -56,7 +56,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexeme::Pattern do
regexp = described_class.new('invalid ( .*')
expect { regexp.evaluate }
.to raise_error(Gitlab::Ci::Pipeline::Expression::Parser::ParserError)
.to raise_error(Gitlab::Ci::Pipeline::Expression::RuntimeError)
end
end
end
......@@ -6,7 +6,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
end
describe '#tokens' do
it 'tokenss single value' do
it 'returns single value' do
tokens = described_class.new('$VARIABLE').tokens
expect(tokens).to be_one
......@@ -20,14 +20,14 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect(tokens).to all(be_an_instance_of(token_class))
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
expect(tokens.size).to eq 2
expect(tokens).to all(be_an_instance_of(token_class))
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
expect(tokens.size).to eq 3
......@@ -36,7 +36,7 @@ describe Gitlab::Ci::Pipeline::Expression::Lexer do
expect(tokens.third.value).to eq '"value"'
end
it 'tokenss tokens and operators' do
it 'returns tokens and operators' do
tokens = described_class.new('$VARIABLE == "text"').tokens
expect(tokens.size).to eq 3
......
......@@ -36,7 +36,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
'== "123"', # invalid left side
'"some string"', # only string provided
'$VAR ==', # invalid right side
'12345', # unknown syntax
'null', # missing lexemes
'' # empty statement
]
......@@ -44,7 +44,7 @@ describe Gitlab::Ci::Pipeline::Expression::Statement do
context "when expression grammar is #{syntax.inspect}" do
let(:text) { syntax }
it 'aises a statement error exception' do
it 'raises a statement error exception' do
expect { subject.parse_tree }
.to raise_error described_class::StatementError
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