Blame view

Pods/Realm/include/core/realm/exceptions.hpp 13.1 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
  /*************************************************************************
   *
   * Copyright 2016 Realm Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   **************************************************************************/
  
  #ifndef REALM_EXCEPTIONS_HPP
  #define REALM_EXCEPTIONS_HPP
  
  #include <stdexcept>
  
  #include <realm/util/features.h>
  #include <realm/util/backtrace.hpp>
  #include <realm/util/to_string.hpp>
  
  namespace realm {
  
  using util::ExceptionWithBacktrace;
  
  /// Thrown by various functions to indicate that a specified table does not
  /// exist.
  class NoSuchTable : public ExceptionWithBacktrace<std::exception> {
  public:
      const char* message() const noexcept override;
  };
  
  class InvalidTableRef : public ExceptionWithBacktrace<std::exception> {
  public:
      InvalidTableRef(const char* cause)
          : m_message(cause)
      {
      }
      const char* message() const noexcept override
      {
          return m_message.c_str();
      }
      std::string m_message;
  };
  
  
  /// Thrown by various functions to indicate that a specified table name is
  /// already in use.
  class TableNameInUse : public ExceptionWithBacktrace<std::exception> {
  public:
      const char* message() const noexcept override;
  };
  
  
  // Thrown by functions that require a table to **not** be the target of link
  // columns, unless those link columns are part of the table itself.
  class CrossTableLinkTarget : public ExceptionWithBacktrace<std::exception> {
  public:
      const char* message() const noexcept override;
  };
  
  
  /// Thrown by various functions to indicate that the dynamic type of a table
  /// does not match a particular other table type (dynamic or static).
  class DescriptorMismatch : public ExceptionWithBacktrace<std::exception> {
  public:
      const char* message() const noexcept override;
  };
  
  
  /// The UnsupportedFileFormatVersion exception is thrown by DB::open()
  /// constructor when opening a database that uses a deprecated file format
  /// and/or a deprecated history schema which this version of Realm cannot
  /// upgrade from.
  class UnsupportedFileFormatVersion : public ExceptionWithBacktrace<> {
  public:
      UnsupportedFileFormatVersion(int source_version);
      /// The unsupported version of the file.
      int source_version = 0;
  };
  
  
  /// Thrown when a sync agent attempts to join a session in which there is
  /// already a sync agent. A session may only contain one sync agent at any given
  /// time.
  class MultipleSyncAgents : public ExceptionWithBacktrace<std::exception> {
  public:
      const char* message() const noexcept override;
  };
  
  
  /// Thrown when memory can no longer be mapped to. When mmap/remap fails.
  class AddressSpaceExhausted : public std::runtime_error {
  public:
      AddressSpaceExhausted(const std::string& msg);
      /// runtime_error::what() returns the msg provided in the constructor.
  };
  
  /// Thrown when creating references that are too large to be contained in our ref_type (size_t)
  class MaximumFileSizeExceeded : public std::runtime_error {
  public:
      MaximumFileSizeExceeded(const std::string& msg);
      /// runtime_error::what() returns the msg provided in the constructor.
  };
  
  /// Thrown when writing fails because the disk is full.
  class OutOfDiskSpace : public std::runtime_error {
  public:
      OutOfDiskSpace(const std::string& msg);
      /// runtime_error::what() returns the msg provided in the constructor.
  };
  
  /// Thrown when a key can not by found
  class KeyNotFound : public std::runtime_error {
  public:
      KeyNotFound(const std::string& msg)
          : std::runtime_error(msg)
      {
      }
  };
  
  /// Thrown when a column can not by found
  class ColumnNotFound : public std::runtime_error {
  public:
      ColumnNotFound()
          : std::runtime_error("Column not found")
      {
      }
  };
  
  /// Thrown when a column key is already used
  class ColumnAlreadyExists : public std::runtime_error {
  public:
      ColumnAlreadyExists()
          : std::runtime_error("Column already exists")
      {
      }
  };
  
  /// Thrown when a key is already existing when trying to create a new object
  class KeyAlreadyUsed : public std::runtime_error {
  public:
      KeyAlreadyUsed(const std::string& msg)
          : std::runtime_error(msg)
      {
      }
  };
  
  // SerialisationError intentionally does not inherit ExceptionWithBacktrace
  // because the query-based-sync permissions queries generated on the server
  // use a LinksToNode which is not currently serialisable (this limitation can
  // be lifted in core 6 given stable ids). Coupled with query metrics which
  // serialize all queries, the capturing of the stack for these frequent
  // permission queries shows up in performance profiles.
  class SerialisationError : public std::runtime_error {
  public:
      SerialisationError(const std::string& msg);
      /// runtime_error::what() returns the msg provided in the constructor.
  };
  
  // thrown when a user constructed link path is not a valid input
  class InvalidPathError : public std::runtime_error {
  public:
      InvalidPathError(const std::string& msg);
      /// runtime_error::what() returns the msg provided in the constructor.
  };
  
  class DuplicatePrimaryKeyValueException : public std::logic_error {
  public:
      DuplicatePrimaryKeyValueException(std::string object_type, std::string property);
  
      std::string const& object_type() const
      {
          return m_object_type;
      }
      std::string const& property() const
      {
          return m_property;
      }
  
  private:
      std::string m_object_type;
      std::string m_property;
  };
  
  
  /// The \c LogicError exception class is intended to be thrown only when
  /// applications (or bindings) violate rules that are stated (or ought to have
  /// been stated) in the documentation of the public API, and only in cases
  /// where the violation could have been easily and efficiently predicted by the
  /// application. In other words, this exception class is for the cases where
  /// the error is due to incorrect use of the public API.
  ///
  /// This class is not supposed to be caught by applications. It is not even
  /// supposed to be considered part of the public API, and therefore the
  /// documentation of the public API should **not** mention the \c LogicError
  /// exception class by name. Note how this contrasts with other exception
  /// classes, such as \c NoSuchTable, which are part of the public API, and are
  /// supposed to be mentioned in the documentation by name. The \c LogicError
  /// exception is part of Realm's private API.
  ///
  /// In other words, the \c LogicError class should exclusively be used in
  /// replacement (or in addition to) asserts (debug or not) in order to
  /// guarantee program interruption, while still allowing for complete
  /// test-cases to be written and run.
  ///
  /// To this effect, the special `CHECK_LOGIC_ERROR()` macro is provided as a
  /// test framework plugin to allow unit tests to check that the functions in
  /// the public API do throw \c LogicError when rules are violated.
  ///
  /// The reason behind hiding this class from the public API is to prevent users
  /// from getting used to the idea that "Undefined Behaviour" equates a specific
  /// exception being thrown. The whole point of properly documenting "Undefined
  /// Behaviour" cases is to help the user know what the limits are, without
  /// constraining the database to handle every and any use-case thrown at it.
  class LogicError : public ExceptionWithBacktrace<std::exception> {
  public:
      enum ErrorKind {
          string_too_big,
          binary_too_big,
          table_name_too_long,
          column_name_too_long,
          column_name_in_use,
          invalid_column_name,
          table_index_out_of_range,
          row_index_out_of_range,
          column_index_out_of_range,
          string_position_out_of_range,
          link_index_out_of_range,
          bad_version,
          illegal_type,
  
          /// Indicates that an argument has a value that is illegal in combination
          /// with another argument, or with the state of an involved object.
          illegal_combination,
  
          /// Indicates a data type mismatch, such as when `Table::find_pkey_int()` is
          /// called and the type of the primary key is not `type_Int`.
          type_mismatch,
  
          /// Indicates that two involved tables are not in the same group.
          group_mismatch,
  
          /// Indicates that an involved descriptor is of the wrong kind, i.e., if
          /// it is a subtable descriptor, and the function requires a root table
          /// descriptor.
          wrong_kind_of_descriptor,
  
          /// Indicates that an involved table is of the wrong kind, i.e., if it
          /// is a subtable, and the function requires a root table, or if it is a
          /// free-standing table, and the function requires a group-level table.
          wrong_kind_of_table,
  
          /// Indicates that an involved accessor is was detached, i.e., was not
          /// attached to an underlying object.
          detached_accessor,
  
          /// Indicates that a specified row index of a target table (a link) is
          /// out of range. This is used for disambiguation in cases such as
          /// Table::set_link() where one specifies both a row index of the origin
          /// table, and a row index of the target table.
          target_row_index_out_of_range,
  
          // Indicates that an involved column lacks a search index.
          no_search_index,
  
          /// Indicates that a modification was attempted that would have produced a
          /// duplicate primary value.
          unique_constraint_violation,
  
          /// User attempted to insert null in non-nullable column
          column_not_nullable,
  
          /// Group::open() is called on a group accessor that is already in the
          /// attached state. Or Group::open() or Group::commit() is called on a
          /// group accessor that is managed by a DB object.
          wrong_group_state,
  
          /// No active transaction on a particular Transaction object (e.g. after commit)
          /// or the Transaction object is of the wrong type (write to a read-only transaction)
          wrong_transact_state,
  
          /// Attempted use of a continuous transaction through a DB
          /// object with no history. See Replication::get_history().
          no_history,
  
          /// Durability setting (as passed to the DB constructor) was
          /// not consistent across the session.
          mixed_durability,
  
          /// History type (as specified by the Replication implementation passed
          /// to the DB constructor) was not consistent across the
          /// session.
          mixed_history_type,
  
          /// History schema version (as specified by the Replication
          /// implementation passed to the DB constructor) was not
          /// consistent across the session.
          mixed_history_schema_version,
  
          /// Adding rows to a table with no columns is not supported.
          table_has_no_columns,
  
          /// Referring to a column that has been deleted.
          column_does_not_exist,
  
          /// You can not add index on a subtable of a subtable
          subtable_of_subtable_index,
  
          /// You try to instantiate a collection object not matching column type
          collection_type_mismatch
      };
  
      LogicError(ErrorKind message);
  
      const char* message() const noexcept override;
      ErrorKind kind() const noexcept;
  
  private:
      ErrorKind m_kind;
  };
  
  
  // Implementation:
  
  // LCOV_EXCL_START (Wording of what() strings are not to be tested)
  
  inline const char* NoSuchTable::message() const noexcept
  {
      return "No such table exists";
  }
  
  inline const char* TableNameInUse::message() const noexcept
  {
      return "The specified table name is already in use";
  }
  
  inline const char* CrossTableLinkTarget::message() const noexcept
  {
      return "Table is target of cross-table link columns";
  }
  
  inline const char* DescriptorMismatch::message() const noexcept
  {
      return "Table descriptor mismatch";
  }
  
  inline UnsupportedFileFormatVersion::UnsupportedFileFormatVersion(int version)
      : ExceptionWithBacktrace<>(
            util::format("Database has an unsupported version (%1) and cannot be upgraded", version))
      , source_version(version)
  {
  }
  
  inline const char* MultipleSyncAgents::message() const noexcept
  {
      return "Multiple sync agents attempted to join the same session";
  }
  
  // LCOV_EXCL_STOP
  
  inline AddressSpaceExhausted::AddressSpaceExhausted(const std::string& msg)
      : std::runtime_error(msg)
  {
  }
  
  inline MaximumFileSizeExceeded::MaximumFileSizeExceeded(const std::string& msg)
      : std::runtime_error(msg)
  {
  }
  
  inline OutOfDiskSpace::OutOfDiskSpace(const std::string& msg)
      : std::runtime_error(msg)
  {
  }
  
  inline SerialisationError::SerialisationError(const std::string& msg)
      : std::runtime_error(msg)
  {
  }
  
  inline InvalidPathError::InvalidPathError(const std::string& msg)
      : runtime_error(msg)
  {
  }
  
  inline LogicError::LogicError(LogicError::ErrorKind k)
      : m_kind(k)
  {
  }
  
  inline LogicError::ErrorKind LogicError::kind() const noexcept
  {
      return m_kind;
  }
  
  
  } // namespace realm
  
  
  #endif // REALM_EXCEPTIONS_HPP