Skip to content

Commit b55a665

Browse files
committed
Separate out ParserError from Error and add #code accessor for generated statusCode.
1 parent 674388f commit b55a665

9 files changed

Lines changed: 43 additions & 26 deletions

File tree

lib/ld/patch.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ def self.parse(input, options = {})
3030
end
3131

3232
class Error < StandardError
33+
# The status code associated with this error
34+
attr_reader :code
35+
36+
##
37+
# Initializes a new patch error instance.
38+
#
39+
# @param [String, #to_s] message
40+
# @param [Hash{Symbol => Object}] options
41+
# @option options [Integer] :code (422)
42+
def initialize(message, options = {})
43+
@code = options.fetch(:status_code, 422)
44+
super(message.to_s)
45+
end
46+
end
47+
48+
# Indicates bad syntax found in LD Patch document
49+
class ParserError < Error
3350
##
3451
# The invalid token which triggered the error.
3552
#
@@ -41,17 +58,19 @@ class Error < StandardError
4158
#
4259
# @return [Integer]
4360
attr_reader :lineno
61+
4462
##
45-
# Initializes a new lexer error instance.
63+
# Initializes a new parser error instance.
4664
#
4765
# @param [String, #to_s] message
4866
# @param [Hash{Symbol => Object}] options
4967
# @option options [String] :token (nil)
5068
# @option options [Integer] :lineno (nil)
69+
# @option options [Integer] :code (400)
5170
def initialize(message, options = {})
5271
@token = options[:token]
5372
@lineno = options[:lineno] || (@token.lineno if @token.respond_to?(:lineno))
54-
super(message.to_s)
73+
super(message.to_s, code: options.fetch(:code, 400))
5574
end
5675
end
5776
end

lib/ld/patch/algebra/add.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ def execute(queryable, options = {})
3737
case var
3838
when RDF::Query::Pattern
3939
s = var.bind(solution)
40-
# FIXME 400 Bad Request
41-
raise LD::Patch::Error, "Operand uses unbound pattern #{var.inspect}" if s.variable?
40+
raise LD::Patch::Error.new("Operand uses unbound pattern #{var.inspect}", code: 400) if s.variable?
4241
s
4342
when RDF::Query::Variable
44-
# FIXME 400 Bad Request
45-
raise LD::Patch::Error, "Operand uses unbound variable #{var.inspect}" unless solution.bound?(var)
43+
raise LD::Patch::Error.new("Operand uses unbound variable #{var.inspect}", code: 400) unless solution.bound?(var)
4644
solution[var]
4745
end
4846
end

lib/ld/patch/algebra/bind.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def execute(queryable, options = {})
7474

7575
# Bind variables to path
7676
if value.variable?
77-
# FIXME 400 Bad Request
78-
raise LD::Patch::Error, "Operand uses unbound variable #{value.inspect}" unless solution.bound?(value)
77+
raise LD::Patch::Error.new("Operand uses unbound variable #{value.inspect}", code: 400) unless solution.bound?(value)
7978
value = solution[value]
8079
end
8180

lib/ld/patch/algebra/cut.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def execute(queryable, options = {})
3232
var = operand(0)
3333

3434
# Bind variable
35-
# FIXME 400 Bad Request
36-
raise LD::Patch::Error, "Operand uses unbound variable #{var.inspect}" unless solution.bound?(var)
35+
raise LD::Patch::Error.new("Operand uses unbound variable #{var.inspect}", code: 400) unless solution.bound?(var)
3736
var = solution[var]
3837

3938
cut_count = 0

lib/ld/patch/algebra/delete.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ def execute(queryable, options = {})
3737
case var
3838
when RDF::Query::Pattern
3939
s = var.bind(solution)
40-
# FIXME 400 Bad Request
41-
raise LD::Patch::Error, "Operand uses unbound pattern #{var.inspect}" if s.variable?
40+
raise LD::Patch::Error.new("Operand uses unbound pattern #{var.inspect}", code: 400) if s.variable?
4241
s
4342
when RDF::Query::Variable
44-
# FIXME 400 Bad Request
45-
raise LD::Patch::Error, "Operand uses unbound variable #{var.inspect}" unless solution.bound?(var)
43+
raise LD::Patch::Error.new("Operand uses unbound variable #{var.inspect}", code: 400) unless solution.bound?(var)
4644
solution[var]
4745
end
4846
end

lib/ld/patch/algebra/update_list.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def execute(queryable, options = {})
3131

3232
# Bind variables to path
3333
if var_or_iri.variable?
34-
# FIXME 400 Bad Request
35-
raise LD::Patch::Error, "Operand uses unbound variable #{var_or_iri.inspect}" unless solution.bound?(var_or_iri)
34+
raise LD::Patch::Error("Operand uses unbound variable #{var_or_iri.inspect}", code: 400) unless solution.bound?(var_or_iri)
3635
var_or_iri = solution[variable]
3736
end
3837

@@ -56,8 +55,7 @@ def execute(queryable, options = {})
5655
else slice2.to_i
5756
end
5857

59-
# FIXME 400 Bad Request
60-
raise LD::Patch::Error, "UpdateList slice indexes out of order #{start}..#{finish}}" if finish < start
58+
raise LD::Patch::Error.new("UpdateList slice indexes out of order #{start}..#{finish}}", code: 400) if finish < start
6159

6260
length = finish - start
6361
raise LD::Patch::Error, "UpdateList out of bounds #{start}..#{finish}}" if start + length > list.length

lib/ld/patch/parser.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Parser
4747
begin
4848
input[:iri] = iri(token.value[1..-2])
4949
rescue ArgumentError => e
50-
raise Error, e.message
50+
raise ParserError, e.message
5151
end
5252
end
5353
terminal(:DOUBLE, DOUBLE) do |prod, token, input|
@@ -351,7 +351,7 @@ def initialize(input = nil, options = {}, &block)
351351
def valid?
352352
parse
353353
true
354-
rescue Error
354+
rescue ParserError
355355
false
356356
end
357357

@@ -416,7 +416,7 @@ def parse(prod = START)
416416
@result.validate! if @result && validate?
417417
@result
418418
rescue EBNF::LL1::Parser::Error, EBNF::LL1::Lexer::Error => e
419-
raise LD::Patch::Error.new(e.message, lineno: e.lineno, token: e.token)
419+
raise LD::Patch::ParserError.new(e.message, lineno: e.lineno, token: e.token)
420420
end
421421

422422
##
@@ -572,7 +572,6 @@ def iri(value)
572572
end
573573

574574
def ns(prefix, suffix)
575-
# FIXME 400 Bad Request
576575
error("pname", "undefined prefix #{prefix.inspect}") unless prefix(prefix)
577576
base = prefix(prefix).to_s
578577
suffix = suffix.to_s.sub(/^\#/, "") if base.index("#")

spec/suite_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ module SuiteTest
7878
"result": {"@id": "mf:result", "@type": "@id"},
7979
"data": {"@id": "ldp:data", "@type": "@id"},
8080
"base": {"@id": "ldp:base", "@type": "@id"},
81-
"patch": {"@id": "ldp:patch", "@type": "@id"}
81+
"patch": {"@id": "ldp:patch", "@type": "@id"},
82+
"statusCode": {"@id": "ldp:statusCode", "@type": "xsd:integer"}
8283
},
8384
"@type": "mf:Manifest",
8485
"entries": {

spec/suite_spec.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
describe m.comment do
1818
m.entries.each do |t|
1919
next if t.approval =~ /Rejected/
20-
specify "#{t.id.split("/").last}: #{t.name} - #{t.comment}#{'( negative)' if t.negative_test?}" do
20+
specify "#{t.name} - #{t.comment}#{'( negative)' if t.negative_test?}" do
2121
if %w(turtle-syntax-bad-struct-09 turtle-syntax-bad-struct-10).include?(t.name)
2222
pending "Multiple '.' allowed in this grammar"
2323
end
@@ -41,9 +41,15 @@
4141
end
4242
fail("Should have raised a parser error")
4343
end
44-
rescue LD::Patch::Error
44+
rescue LD::Patch::ParserError
45+
unless t.syntax? && t.negative_test? || %w(turtle-eval-bad-01 turtle-eval-bad-02 turtle-eval-bad-03).include?(t.name)
46+
raise
47+
end
48+
rescue LD::Patch::Error => e
4549
# Special case
46-
unless t.negative_test?
50+
if t.evaluate? && t.negative_test?
51+
expect(e.code).to eq t.statusCode.to_i
52+
else
4753
raise
4854
end
4955
end

0 commit comments

Comments
 (0)