add runtime error

This commit is contained in:
Kanezoh 2021-05-27 08:59:53 +09:00
parent cbcf025c33
commit 71b97b6a38
4 changed files with 21 additions and 2 deletions

View File

@ -2,6 +2,7 @@ require "./mechanize/http/agent"
require "./mechanize/form" require "./mechanize/form"
require "./mechanize/node" require "./mechanize/node"
require "./mechanize/page" require "./mechanize/page"
require "./mechanize/errors/*"
class Mechanize class Mechanize
VERSION = "0.1.0" VERSION = "0.1.0"

View File

@ -0,0 +1,3 @@
# Base Error class
class MechanizeCr::Error < RuntimeError
end

View File

@ -0,0 +1,13 @@
require "./base_error"
class MechanizeCr::ElementNotFoundError < MechanizeCr::Error
getter element : Symbol
getter conditions : String
def initialize(element, conditions)
@element = element
@conditions = conditions
super "Element #{element} with conditions #{conditions} was not found"
end
end

View File

@ -72,15 +72,17 @@ class MechanizeCr::Form
# TODO # TODO
# when args whose type isn't String is given # when args whose type isn't String is given
end end
fields.select do |field| f = fields.select do |field|
value.all? do |k,v| value.all? do |k,v|
v === field.name v === field.name
end end
end end
f.empty? ? nil : f
end end
def field_with(criteria) def field_with(criteria)
f = fields_with(criteria) f = fields_with(criteria)
f.empty? ? Array(MechanizeCr::FormContent::Field).new : f.first raise MechanizeCr::ElementNotFoundError.new(:field, criteria) if f.nil?
f.first
end end
end end