Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 165 lines (142 sloc) 6.446 kb
39d6f9e @wycats Make many parts of Rails lazy. In order to facilitate this,
wycats authored
1 module ActiveRecord
2 # Generic Active Record exception class.
3 class ActiveRecordError < StandardError
4 end
5
6 # Raised when the single-table inheritance mechanism fails to locate the subclass
7 # (for example due to improper usage of column that +inheritance_column+ points to).
8 class SubclassNotFound < ActiveRecordError #:nodoc:
9 end
10
11 # Raised when an object assigned to an association has an incorrect type.
12 #
13 # class Ticket < ActiveRecord::Base
14 # has_many :patches
15 # end
16 #
17 # class Patch < ActiveRecord::Base
18 # belongs_to :ticket
19 # end
20 #
21 # # Comments are not patches, this assignment raises AssociationTypeMismatch.
22 # @ticket.patches << Comment.new(:content => "Please attach tests to your patch.")
23 class AssociationTypeMismatch < ActiveRecordError
24 end
25
26 # Raised when unserialized object's type mismatches one specified for serializable field.
27 class SerializationTypeMismatch < ActiveRecordError
28 end
29
30 # Raised when adapter not specified on connection (or configuration file <tt>config/database.yml</tt> misses adapter field).
31 class AdapterNotSpecified < ActiveRecordError
32 end
33
34 # Raised when Active Record cannot find database adapter specified in <tt>config/database.yml</tt> or programmatically.
35 class AdapterNotFound < ActiveRecordError
36 end
37
38 # Raised when connection to the database could not been established (for example when <tt>connection=</tt> is given a nil object).
39 class ConnectionNotEstablished < ActiveRecordError
40 end
41
42 # Raised when Active Record cannot find record by given id or set of ids.
43 class RecordNotFound < ActiveRecordError
44 end
45
46 # Raised by ActiveRecord::Base.save! and ActiveRecord::Base.create! methods when record cannot be
47 # saved because record is invalid.
48 class RecordNotSaved < ActiveRecordError
49 end
50
51 # Raised when SQL statement cannot be executed by the database (for example, it's often the case for MySQL when Ruby driver used is too old).
52 class StatementInvalid < ActiveRecordError
53 end
54
55 # Raised when SQL statement is invalid and the application gets a blank result.
56 class ThrowResult < ActiveRecordError
57 end
58
59 # Parent class for all specific exceptions which wrap database driver exceptions
60 # provides access to the original exception also.
61 class WrappedDatabaseException < StatementInvalid
62 attr_reader :original_exception
63
64 def initialize(message, original_exception)
65 super(message)
66 @original_exception = original_exception
67 end
68 end
69
70 # Raised when a record cannot be inserted because it would violate a uniqueness constraint.
71 class RecordNotUnique < WrappedDatabaseException
72 end
73
74 # Raised when a record cannot be inserted or updated because it references a non-existent record.
75 class InvalidForeignKey < WrappedDatabaseException
76 end
77
78 # Raised when number of bind variables in statement given to <tt>:condition</tt> key (for example, when using +find+ method)
79 # does not match number of expected variables.
80 #
81 # For example, in
82 #
83 # Location.find :all, :conditions => ["lat = ? AND lng = ?", 53.7362]
84 #
85 # two placeholders are given but only one variable to fill them.
86 class PreparedStatementInvalid < ActiveRecordError
87 end
88
89 # Raised on attempt to save stale record. Record is stale when it's being saved in another query after
90 # instantiation, for example, when two users edit the same wiki page and one starts editing and saves
91 # the page before the other.
92 #
93 # Read more about optimistic locking in ActiveRecord::Locking module RDoc.
94 class StaleObjectError < ActiveRecordError
95 end
96
97 # Raised when association is being configured improperly or
98 # user tries to use offset and limit together with has_many or has_and_belongs_to_many associations.
99 class ConfigurationError < ActiveRecordError
100 end
101
102 # Raised on attempt to update record that is instantiated as read only.
103 class ReadOnlyRecord < ActiveRecordError
104 end
105
106 # ActiveRecord::Transactions::ClassMethods.transaction uses this exception
107 # to distinguish a deliberate rollback from other exceptional situations.
108 # Normally, raising an exception will cause the +transaction+ method to rollback
109 # the database transaction *and* pass on the exception. But if you raise an
110 # ActiveRecord::Rollback exception, then the database transaction will be rolled back,
111 # without passing on the exception.
112 #
113 # For example, you could do this in your controller to rollback a transaction:
114 #
115 # class BooksController < ActionController::Base
116 # def create
117 # Book.transaction do
118 # book = Book.new(params[:book])
119 # book.save!
120 # if today_is_friday?
121 # # The system must fail on Friday so that our support department
122 # # won't be out of job. We silently rollback this transaction
123 # # without telling the user.
124 # raise ActiveRecord::Rollback, "Call tech support!"
125 # end
126 # end
127 # # ActiveRecord::Rollback is the only exception that won't be passed on
128 # # by ActiveRecord::Base.transaction, so this line will still be reached
129 # # even on Friday.
130 # redirect_to root_url
131 # end
132 # end
133 class Rollback < ActiveRecordError
134 end
135
136 # Raised when attribute has a name reserved by Active Record (when attribute has name of one of Active Record instance methods).
137 class DangerousAttributeError < ActiveRecordError
138 end
139
140 # Raised when unknown attributes are supplied via mass assignment.
141 class UnknownAttributeError < NoMethodError
142 end
143
144 # Raised when an error occurred while doing a mass assignment to an attribute through the
145 # <tt>attributes=</tt> method. The exception has an +attribute+ property that is the name of the
146 # offending attribute.
147 class AttributeAssignmentError < ActiveRecordError
148 attr_reader :exception, :attribute
149 def initialize(message, exception, attribute)
150 @exception = exception
151 @attribute = attribute
152 @message = message
153 end
154 end
155
156 # Raised when there are multiple errors while doing a mass assignment through the +attributes+
157 # method. The exception has an +errors+ property that contains an array of AttributeAssignmentError
158 # objects, each corresponding to the error while assigning to an attribute.
159 class MultiparameterAssignmentErrors < ActiveRecordError
160 attr_reader :errors
161 def initialize(errors)
162 @errors = errors
163 end
164 end
165 end
Something went wrong with that request. Please try again.