Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 214 lines (181 sloc) 7.634 kb
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
1 module ActiveRecord
c869154 @rizwanreza Adds title.
rizwanreza authored
2
3 # = Active Record Errors
4 #
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
5 # Generic Active Record exception class.
6 class ActiveRecordError < StandardError
7 end
8
9 # Raised when the single-table inheritance mechanism fails to locate the subclass
10 # (for example due to improper usage of column that +inheritance_column+ points to).
11 class SubclassNotFound < ActiveRecordError #:nodoc:
12 end
13
14 # Raised when an object assigned to an association has an incorrect type.
15 #
16 # class Ticket < ActiveRecord::Base
17 # has_many :patches
18 # end
19 #
20 # class Patch < ActiveRecord::Base
21 # belongs_to :ticket
22 # end
23 #
24 # # Comments are not patches, this assignment raises AssociationTypeMismatch.
890da51 @AvnerCohen 1.9 Syntax related changes
AvnerCohen authored
25 # @ticket.patches << Comment.new(content: "Please attach tests to your patch.")
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
26 class AssociationTypeMismatch < ActiveRecordError
27 end
28
29 # Raised when unserialized object's type mismatches one specified for serializable field.
30 class SerializationTypeMismatch < ActiveRecordError
31 end
32
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
33 # Raised when adapter not specified on connection (or configuration file <tt>config/database.yml</tt>
1ce40ca @neerajdotname ensuring that description does not exceed 100 columns
neerajdotname authored
34 # misses adapter field).
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
35 class AdapterNotSpecified < ActiveRecordError
36 end
37
38 # Raised when Active Record cannot find database adapter specified in <tt>config/database.yml</tt> or programmatically.
39 class AdapterNotFound < ActiveRecordError
40 end
41
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
42 # Raised when connection to the database could not been established (for example when <tt>connection=</tt>
1ce40ca @neerajdotname ensuring that description does not exceed 100 columns
neerajdotname authored
43 # is given a nil object).
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
44 class ConnectionNotEstablished < ActiveRecordError
45 end
46
47 # Raised when Active Record cannot find record by given id or set of ids.
48 class RecordNotFound < ActiveRecordError
49 end
50
51 # Raised by ActiveRecord::Base.save! and ActiveRecord::Base.create! methods when record cannot be
52 # saved because record is invalid.
53 class RecordNotSaved < ActiveRecordError
54 end
55
4faaa81 @marcandre + ActiveRecord::Base#destroy!
marcandre authored
56 # Raised by ActiveRecord::Base.destroy! when a call to destroy would return false.
57 class RecordNotDestroyed < ActiveRecordError
58 end
59
60bb133 @jeremy StatementInvalid takes WrappedDatabaseException's place
jeremy authored
60 # Superclass for all database execution errors.
61 #
62 # Wraps the underlying database error as +original_exception+.
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
63 class StatementInvalid < ActiveRecordError
60bb133 @jeremy StatementInvalid takes WrappedDatabaseException's place
jeremy authored
64 attr_reader :original_exception
65
66 def initialize(message, original_exception = nil)
67 super(message)
68 @original_exception = original_exception
69 end
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
70 end
71
72 # Raised when SQL statement is invalid and the application gets a blank result.
73 class ThrowResult < ActiveRecordError
74 end
75
60bb133 @jeremy StatementInvalid takes WrappedDatabaseException's place
jeremy authored
76 # Defunct wrapper class kept for compatibility.
77 # +StatementInvalid+ wraps the original exception now.
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
78 class WrappedDatabaseException < StatementInvalid
79 end
80
81 # Raised when a record cannot be inserted because it would violate a uniqueness constraint.
82 class RecordNotUnique < WrappedDatabaseException
83 end
84
85 # Raised when a record cannot be inserted or updated because it references a non-existent record.
86 class InvalidForeignKey < WrappedDatabaseException
87 end
88
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
89 # Raised when number of bind variables in statement given to <tt>:condition</tt> key (for example,
1ce40ca @neerajdotname ensuring that description does not exceed 100 columns
neerajdotname authored
90 # when using +find+ method)
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
91 # does not match number of expected variables.
92 #
93 # For example, in
94 #
4d4819f @amatsuda :conditions => where
amatsuda authored
95 # Location.where("lat = ? AND lng = ?", 53.7362)
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
96 #
97 # two placeholders are given but only one variable to fill them.
98 class PreparedStatementInvalid < ActiveRecordError
99 end
100
101 # Raised on attempt to save stale record. Record is stale when it's being saved in another query after
102 # instantiation, for example, when two users edit the same wiki page and one starts editing and saves
103 # the page before the other.
104 #
105 # Read more about optimistic locking in ActiveRecord::Locking module RDoc.
106 class StaleObjectError < ActiveRecordError
c6f0461 @fabrik42 Consider attempted action in exception message of ActiveRecord::Stale…
fabrik42 authored
107 attr_reader :record, :attempted_action
410fa4c @fabrik42 Includes stale record in StaleObjectError
fabrik42 authored
108
c6f0461 @fabrik42 Consider attempted action in exception message of ActiveRecord::Stale…
fabrik42 authored
109 def initialize(record, attempted_action)
ab7c1c4 @mauricio Fixes #6825, adds tests covering cases and error possibilities, also …
mauricio authored
110 super("Attempted to #{attempted_action} a stale object: #{record.class.name}")
410fa4c @fabrik42 Includes stale record in StaleObjectError
fabrik42 authored
111 @record = record
c6f0461 @fabrik42 Consider attempted action in exception message of ActiveRecord::Stale…
fabrik42 authored
112 @attempted_action = attempted_action
410fa4c @fabrik42 Includes stale record in StaleObjectError
fabrik42 authored
113 end
114
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
115 end
116
117 # Raised when association is being configured improperly or
118 # user tries to use offset and limit together with has_many or has_and_belongs_to_many associations.
119 class ConfigurationError < ActiveRecordError
120 end
121
122 # Raised on attempt to update record that is instantiated as read only.
123 class ReadOnlyRecord < ActiveRecordError
124 end
125
126 # ActiveRecord::Transactions::ClassMethods.transaction uses this exception
127 # to distinguish a deliberate rollback from other exceptional situations.
128 # Normally, raising an exception will cause the +transaction+ method to rollback
129 # the database transaction *and* pass on the exception. But if you raise an
130 # ActiveRecord::Rollback exception, then the database transaction will be rolled back,
131 # without passing on the exception.
132 #
133 # For example, you could do this in your controller to rollback a transaction:
134 #
135 # class BooksController < ActionController::Base
136 # def create
137 # Book.transaction do
138 # book = Book.new(params[:book])
139 # book.save!
140 # if today_is_friday?
141 # # The system must fail on Friday so that our support department
142 # # won't be out of job. We silently rollback this transaction
143 # # without telling the user.
144 # raise ActiveRecord::Rollback, "Call tech support!"
145 # end
146 # end
147 # # ActiveRecord::Rollback is the only exception that won't be passed on
148 # # by ActiveRecord::Base.transaction, so this line will still be reached
149 # # even on Friday.
150 # redirect_to root_url
151 # end
152 # end
153 class Rollback < ActiveRecordError
154 end
155
156 # Raised when attribute has a name reserved by Active Record (when attribute has name of one of Active Record instance methods).
157 class DangerousAttributeError < ActiveRecordError
158 end
159
160 # Raised when unknown attributes are supplied via mass assignment.
161 class UnknownAttributeError < NoMethodError
162 end
163
164 # Raised when an error occurred while doing a mass assignment to an attribute through the
165 # <tt>attributes=</tt> method. The exception has an +attribute+ property that is the name of the
166 # offending attribute.
167 class AttributeAssignmentError < ActiveRecordError
168 attr_reader :exception, :attribute
169 def initialize(message, exception, attribute)
ab7c1c4 @mauricio Fixes #6825, adds tests covering cases and error possibilities, also …
mauricio authored
170 super(message)
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
171 @exception = exception
172 @attribute = attribute
173 end
174 end
175
176 # Raised when there are multiple errors while doing a mass assignment through the +attributes+
177 # method. The exception has an +errors+ property that contains an array of AttributeAssignmentError
178 # objects, each corresponding to the error while assigning to an attribute.
179 class MultiparameterAssignmentErrors < ActiveRecordError
180 attr_reader :errors
181 def initialize(errors)
182 @errors = errors
183 end
184 end
2e9e647 @jonleighton Raise an exception on unknown primary key inside AssociationReflection.
jonleighton authored
185
186 # Raised when a primary key is needed, but there is not one specified in the schema or model.
187 class UnknownPrimaryKey < ActiveRecordError
188 attr_reader :model
189
190 def initialize(model)
ab7c1c4 @mauricio Fixes #6825, adds tests covering cases and error possibilities, also …
mauricio authored
191 super("Unknown primary key for table #{model.table_name} in model #{model}.")
2e9e647 @jonleighton Raise an exception on unknown primary key inside AssociationReflection.
jonleighton authored
192 @model = model
193 end
194
195 end
70d3625 @jonleighton disallow mutating a relation once loaded
jonleighton authored
196
7a36dc7 @frodsan update AR::ImmutableRelation documentation [ci skip]
frodsan authored
197 # Raised when a relation cannot be mutated because it's already loaded.
198 #
199 # class Task < ActiveRecord::Base
200 # end
201 #
202 # relation = Task.all
203 # relation.loaded? # => true
204 #
ee91749 @vijaydev copy edits [ci skip]
vijaydev authored
205 # # Methods which try to mutate a loaded relation fail.
206 # relation.where!(title: 'TODO') # => ActiveRecord::ImmutableRelation
207 # relation.limit!(5) # => ActiveRecord::ImmutableRelation
70d3625 @jonleighton disallow mutating a relation once loaded
jonleighton authored
208 class ImmutableRelation < ActiveRecordError
209 end
392eeec @jonleighton Support for specifying transaction isolation level
jonleighton authored
210
211 class TransactionIsolationError < ActiveRecordError
212 end
1ce40ca @neerajdotname ensuring that description does not exceed 100 columns
neerajdotname authored
213 end
Something went wrong with that request. Please try again.