Project

General

Profile

« Previous | Next » 

Revision bf23ac8c

Added by Marc Dequènes almost 15 years ago

  • ID bf23ac8c74dbe0ad5720ba4c742369e3eafbab85

[evol] begining of the error abstraction with a simplied exception list, with translation using gettext (no translations yet)

View differences:

bin/shadowwalker
# to allow in-place run for test
$: << File.join(File.dirname(__FILE__), "..", "lib")
# get locale from shell
locale = ENV['LANGUAGE'] || ENV['LC_ALL'] || ENV['LC_MESSAGES'] || ENV['LANG']
require 'ldap_shadows'
require 'ldap_shadows/display_utils'
require 'yaml'
......
include LdapShadows
set_locale(locale)
config_str = IO.read(File.join(LdapShadows::Config::CFG_DIR, "test.conf"))
config = YAML.load(config_str)
......
end
$ldapctl.load_relations
rescue ActiveLdap::Error => e
STDERR.puts "LDAP connection error: " + e.to_s
exit 3
STDERR.puts _("LDAP connection error: %s") % e.to_s
exit 2
end
......
protected
def params_shift_location(args)
if args.empty?
STDERR.puts "syntax error: no location name given"
exit 1
end
raise SyntaxError, _("no location name given") if args.empty?
location = args.shift.downcase
if location =~ /^([a-zA-Z]+)\/(.+)$/
loc_obj_hdl = $1
loc_item_hdl = $2
else
STDERR.puts "syntax error: bad location"
exit 1
end
raise SyntaxError, _("bad location") unless location =~ /^([a-zA-Z]+)\/(.+)$/
loc_obj_hdl = $1
loc_item_hdl = $2
split_args = [loc_obj_hdl, loc_item_hdl]
loc_obj_klass = params_shift_object(split_args)
......
end
def params_shift_object(args)
if args.empty?
STDERR.puts "syntax error: no object name given"
exit 1
end
raise SyntaxError, _("no object name given") if args.empty?
obj_hdl = args.shift.downcase.singularize
obj_klass = $ldapctl.find_klass(obj_hdl)
if obj_klass.nil?
STDERR.puts "No such object '#{obj_hdl}'."
exit 2
end
raise PreProcessingError, _("No such object '%s'") % obj_hdl if obj_klass.nil?
obj_klass
end
def params_shift_item(obj_klass, args, with_expert = false)
if args.empty?
STDERR.puts "syntax error: no item name given"
exit 1
end
raise SyntaxError, _("no item name given") if args.empty?
attr_list = ["*"]
attr_list << "+" if $program_options[:expert] and with_expert # add operational attributes if expert
......
begin
item = obj_klass.find(item_hdl, :attributes => attr_list)
rescue ActiveLdap::EntryNotFound
STDERR.puts "No such item '#{obj_klass.handle}/#{item_hdl}'"
exit 2
raise PreProcessingError, _("No such item '%s/%s'") % [obj_klass.handle, item_hdl]
end
item
end
......
end
def execute (args)
if args.size < 1
STDERR.puts "syntax error: no object name given"
exit 1
end
raise SyntaxError, _("no object name given") if args.empty?
obj_hdl = args.shift.downcase.singularize
case obj_hdl
......
end
end
else
STDERR.puts "No such subobject '#{subobj_hdl}'."
exit 2
raise PreProcessingError, _("No such subobject '%s'") % subobj_hdl
end
end
end
......
field_name, file_no = field_info.split("#")
unless item.has_field?(field_name)
STDERR.puts "No such field '#{field_name}' in object '#{obj_klass.handle}'."
exit 2
raise PreProcessingError, _("No such field '%s' in object '%s'") % [field_name, obj_klass.handle]
end
unless item.attribute_present?(field_name)
STDERR.puts "Field '#{field_name}' in item '#{obj_klass.handle}/#{item.name}' is not present."
exit 2
raise PreProcessingError, _("Field '%s' in item '%s/%s' is not present") %
[field_name, obj_klass.handle, item.name]
end
field_data = item.send(field_name, true)
......
field_single = attr_info.single_value?
if file_no.nil? and not field_single
STDERR.puts "Field '#{field_name}' in object '#{obj_klass.handle}' has multiple values, but you didn't select one."
exit 2
raise PreProcessingError, _("Field '%s' in object '%s' has multiple values, but you didn't select one") %
[field_name, obj_klass.handle]
end
file_no = 0 if file_no.nil?
if file_no > 0 and field_single
STDERR.puts "Field '#{field_name}' in object '#{obj_klass.handle}' has only a single value"
exit 2
raise PreProcessingError, _("Field '%s' in object '%s' has only a single value") %
[field_name, obj_klass.handle]
end
if file_no < 0 or file_no >= field_data.size
STDERR.puts "Field '#{field_name}' in object '#{obj_klass.handle}' doesn't have such file number '#{file_no}' (select in range [0, #{field_data.size - 1}])"
exit 2
raise PreProcessingError, _("Field '%s' in object '%s' doesn't have such file number '%s' (select in range [0, %s])") %
[field_name, obj_klass.handle, file_no, field_data.size - 1]
end
result = field_data[file_no]
......
else
file_name = args.shift
if File.exists?(file_name)
STDERR.puts "File '#{file_name}' already exists, and i won't overwrite it."
exit 2
raise PreProcessingError, _("File '%s' already exists, and i won't overwrite it") % file_name
end
begin
......
fp.write result
end
rescue
STDERR.puts "Cannot save file: " + $!
exit 3
raise ProcessingError, _("Cannot save file: %s") % $!
end
puts "File saved."
......
(gconfig[:tree_objects] || []).each do |obj_hdl|
obj_klass = $ldapctl.find_klass(obj_hdl.downcase)
unless obj_klass
STDERR.puts "Location object '#{obj_hdl}' not defined."
exit 2
raise PreProcessingError, _("Location object '%s' not defined") % obj_hdl
end
obj_klass.find(:all, :base => search_base, :scope => :sub).each do |obj|
......
modification_done = false
args.each do |mod_info|
mod_info =~ /^([a-zA-Z]*(?::[a-zA-Z]+)?)(=|\+=|-=)(.*)$/
unless mod_info =~ /^([a-zA-Z]*(?::[a-zA-Z]+)?)(=|\+=|-=)(.*)$/
raise SyntaxError, _("modification parameter '%s' is invalid") % mod_info
end
key = $1
op = $2
val = $3
if key.nil? or op.nil?
STDERR.puts "Syntax error in modification parameters: invalid field name, or missing/unknown operator, or empty value"
exit 1
end
if key.index(":")
type, field = key.split(":")
else
......
case type
when nil
unless item.has_field?(field)
STDERR.puts "No such field '#{field}' in object '#{obj_klass.handle}'."
exit 2
raise PreProcessingError, _("No such field '%s' in object '%s'") % [field, obj_klass.handle]
end
attr_info = ActiveLdap::Base.schema.attribute(field)
if attr_info.read_only?
STDERR.puts "The field '#{field}' cannot be modified (read only), skipping."
next
raise PreProcessingError, _("The field '%s' cannot be modified (read only)") % field
end
if attr_info.binary?
unless File.exists?(val)
STDERR.puts "The field '#{field}' contains binary data, you must provide a filename instead of a direct value."
exit 2
raise PreProcessingError, _("The field '%s' contains binary data, you must provide a filename instead of a direct value") % field
end
begin
val = File.read(val)
rescue
STDERR.puts "The file for the binary field '#{field}' cannot be read: " + $!
exit 2
raise PreProcessingError, _("The file for the binary field '%s' cannot be read: ") % [field, $!]
end
end
old_val = item.send(field, true)
when 'rel'
unless item.relations.include?(field)
STDERR.puts "No such relation '#{field}' for object '#{obj_klass.handle}'."
exit 2
raise PreProcessingError, _("No such relation '%s' for object '%s'") % [field, obj_klass.handle]
end
rel_info = item.info_for_relation(field)
if rel_info[:read_only]
STDERR.puts "The relation '#{field}' cannot be modified (read only), skipping."
next
raise PreProcessingError, _("The relation '%s' cannot be modified (read only)") % field
end
# fetch remote object in relation, which will be the real 'val'
foreign_item_list = rel_info[:foreign_klass].find(:all, val)
if foreign_item_list.empty?
STDERR.puts "Foreign item '#{val}' for relation '#{field}' not found."
exit 2
raise PreProcessingError, _("Foreign item '%s' for relation '%s' not found") % [val, field]
end
if foreign_item_list.size > 1
STDERR.puts "Ambiguous item '#{val}' for relation '#{field}' (#{foreign_item_list.size} possible items)"
exit 4
raise WeirdError, _("Ambiguous item '%s' for relation '%s' (%s possible items)") %
[val, field, foreign_item_list.size]
end
val = foreign_item_list.first
when ''
case field
when 'aspects'
unless item.class.possible_aspects.include?(val)
STDERR.puts "No such aspect '#{val}' for object '#{obj_klass.handle}'."
exit 2
raise PreProcessingError, _("No such aspect '%s' for object '%s'") [val, obj_klass.handle]
end
else
STDERR.puts "Unknown core field '#{field}'"
exit 2
raise PreProcessingError, _("Unknown core field '%s'") % field
end
else
STDERR.puts "Unknown type '#{type}' for field '#{field}'."
exit 2
raise PreProcessingError, _("Unknown type '%s' for field '%s'") % [type, field]
end
# if val is nil or the latest value is removed, then the atribute is removed from the object,
......
case op
when '='
if type == '' and field == 'aspects'
STDERR.puts "The equality operator is not possible for aspects."
exit 2
raise PreProcessingError, _("The equality operator is not possible for aspects")
end
item.send(field + "=", [val])
when '+='
case type
when nil
if attr_info.single_value?
STDERR.puts "The field '#{field}' cannot hold more than one value."
exit 3
raise PreProcessingError, _("The field '%s' cannot hold more than one value") % field
end
new_val = old_val << val
item.send(field + "=", new_val)
when 'rel'
if rel_info[:single_value]
STDERR.puts "The relation '#{field}' cannot hold more than one foreign item."
exit 3
raise PreProcessingError, _("The relation '%s' cannot hold more than one foreign item") % field
end
item.send(field) << val
......
item.remove_aspect(val)
end
end
else
STDERR.puts "Syntax error in modification parameters: wrong operator"
exit 1
end
modification_done = true
end
......
# and exceptions caught in a centralized place in this file.
missing_fields = []
unless missing_fields.empty?
STDERR.puts "Cannot save the modifications; the following fields are missing:"
miss_str = []
missing_fields.each do |field|
str = Translator.translate_field_name(field)
str += " [#{field}]" if $program_options[:handles]
puts " - #{str}"
miss_str << str
end
exit 2
raise PreProcessingError, _("Cannot save the modifications; the following fields are missing: %s") %
miss_str.join(", ")
end
begin
item.save!
rescue ActiveLdap::OperationNotPermitted => e
STDERR.puts "You don't have enough rights to modify--at least--one of these attributes."
exit 3
end
item.save!
puts "Modification done."
else
......
def execute (args)
obj_klass = params_shift_object(args)
if args.empty?
STDERR.puts "syntax error: no item name given"
exit 1
end
raise SyntaxError, _("no item name given") if args.empty?
item_hdl = args.shift.downcase
loc_obj_klass, loc_item = params_shift_location(args)
......
item.base = loc_item.dn_obj - obj_klass.base_obj
args.each do |param|
param =~ /^([a-zA-Z]+)=(.*)$/
raise SyntaxError, _("creation parameter '%s' is invalid") % param unless param =~ /^([a-zA-Z]+)=(.*)$/
key = $1
val = $2
val = nil if val == ''
if key.nil?
STDERR.puts "Syntax error in creation parameters: invalid field name"
exit 1
end
field = key
unless item.has_field?(field)
STDERR.puts "No such field '#{field}' in object '#{obj_klass.handle}'."
exit 2
raise PreProcessingError, _("No such field '%s' in object '%s'") % [field, obj_klass.handle]
end
attr_info = ActiveLdap::Base.schema.attribute(field)
if attr_info.read_only?
STDERR.puts "The field '#{field}' cannot be modified (read only), skipping."
next
raise PreProcessingError, _("The field '%s' cannot be modified (read only)") % field
end
if attr_info.binary?
unless File.exists?(val)
STDERR.puts "The field '#{field}' contains binary data, you must provide a filename instead of a direct value."
exit 2
raise PreProcessingError, _("The field '%s' contains binary data, you must provide a filename instead of a direct value") % field
end
begin
val = File.read(val)
rescue
STDERR.puts "The file for the binary field '#{field}' cannot be read: " + $!
exit 2
raise PreProcessingError, _("The file for the binary field '%s' cannot be read: %s") % [field, $!]
end
end
......
missing_fields = item.missing_attributes
unless missing_fields.empty?
STDERR.puts "Cannot save the new item; the following fields are missing:"
miss_str = []
missing_fields.each do |field|
str = Translator.translate_field_name(field)
str += " [#{field}]" if $program_options[:handles]
puts " - #{str}"
miss_str << str
end
exit 2
raise PreProcessingError, _("Cannot save the new item; the following fields are missing: %s") %
miss_str.join(", ")
end
item.save!
......
begin
cmdparser.parse
rescue SyntaxError => e
STDERR.puts _("Syntax error: %s") % e.to_s
exit 1
rescue PreProcessingError => e
STDERR.puts _("Preprocessing error: %s") % e.to_s
exit 2
rescue ActiveLdap::EntryInvalid => e
# work around activeldap#26758
# work around activeldap#26758 (for english output only :-/)
STDERR.puts e.to_s.sub(/invalid format: .*: required syntax:/, "invalid format. required syntax:")
exit 3
rescue ActiveLdap::OperationNotPermitted => e
STDERR.puts "You don't have enough rights for this operation."
STDERR.puts _("You don't have enough rights for this operation")
exit 3
rescue ActiveLdap::Error => e
STDERR.puts "LDAP error: " + e.to_s
STDERR.puts _("LDAP error: %s") % e.to_s
exit 3
rescue WeirdError =>e
STDERR.puts _("Weird error: %s") % e.to_s
exit 4
end

Also available in: Unified diff