Exception: Informix::Error

Inherits:
StandardError
  • Object
show all
Includes:
Enumerable
Defined in:
lib/informix/exceptions.rb

Overview

The Error class is the base class for the rest of the exception classes used in this extension. It works as a collection of ExcInfo objects when an error condition occurs.

Instance Method Summary collapse

Constructor Details

#initialize(v = nil) ⇒ Error

Informix::Error.new() => obj

Optional string is the exception message. Optional array must contain only instances of Informix::ExcInfo structs.

Examples: exc = Informix::Error.new arr = [ExcInfo.new(x,y,z…), ExcInfo.new(a,b,c…)] exc = Informix::Error.new(arr)



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/informix/exceptions.rb', line 71

def initialize(v = nil)
  case v
  when NilClass
    @info = []
  when String
    @info = []
    super
  when Array
    return @info = v if v.all? {|e| ExcInfo === e}
    raise(TypeError, "Array may contain only Informix::ExcInfo structs")
  else
    raise(TypeError,
             "Expected string, or array of Informix::ExcInfo, as argument")
  end
end

Instance Method Details

#[](index) ⇒ Object

exc => info

Returns the ExcInfo object at index.



118
119
120
# File 'lib/informix/exceptions.rb', line 118

def [](index)
  @info[index]
end

#add_info(*v) ⇒ Object

exc.add_info(sql_code, sql_state, class_origin, subclass_origin,

message, server_name, connection_name)           =>  self

Appends the given information to the exception.

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
# File 'lib/informix/exceptions.rb', line 91

def add_info(*v)
  v.flatten!
  raise(ArgumentError,
    "Invalid number of arguments (got %d, need %d)", v.size, 7) \
    if v.size != 7
  @info.push ExcInfo.new(*v)
end

#each(&block) ⇒ Object

exc.each {|exc_info| block } => exc_info

Calls block once for each Informix::ExcInfo object in the exception.



111
112
113
# File 'lib/informix/exceptions.rb', line 111

def each(&block)
  @info.each(&block)
end

#messageObject

exc.message => string

Overrides Exception#message. Returns first message in ExcInfo array, or if the array is empty, delegates back to the parent class.



138
139
140
# File 'lib/informix/exceptions.rb', line 138

def message
  @info.size > 0 ? @info[0].message : super
end

#sizeObject Also known as: length

exc.size => num

Returns the number of Informix exception messages in the exception.



102
103
104
# File 'lib/informix/exceptions.rb', line 102

def size
  @info.size
end

#sql_codeObject

exc.sqlcode => fixnum

Returns the SQLCODE for the first stored ExcInfo struct, or 0 if none are stored.



146
147
148
# File 'lib/informix/exceptions.rb', line 146

def sql_code
  @info.size > 0 ? @info[0].sql_code : 0
end

#to_sObject

exc.to_s => string

Returns a string representation of self.



125
126
127
128
129
130
131
132
# File 'lib/informix/exceptions.rb', line 125

def to_s
  return super if @info.size == 0
  ret = ""
  @info.each do |info|
    ret += info.to_s
  end
  ret
end