Reorganize exceptions in pipeline expressions module

This commit is contained in:
Grzegorz Bizon 2018-05-15 12:37:09 +02:00
parent bbba6d7e62
commit 475d2edf04
8 changed files with 23 additions and 16 deletions

View file

@ -0,0 +1,10 @@
module Gitlab
module Ci
module Pipeline
module Expression
ExpressionError = Class.new(StandardError)
RuntimeError = Class.new(ExpressionError)
end
end
end
end

View file

@ -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)

View file

@ -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)

View file

@ -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 = []

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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