realm-js/object_store_exceptions.hpp

101 lines
4.2 KiB
C++
Raw Normal View History

////////////////////////////////////////////////////////////////////////////
//
2015-06-11 17:33:46 +00:00
// Copyright 2015 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_OBJECT_STORE_EXCEPTIONS_HPP
#define REALM_OBJECT_STORE_EXCEPTIONS_HPP
#include <vector>
#include <map>
#include <string>
namespace realm {
class Property;
class ObjectStoreException : public std::exception {
public:
enum class Kind {
RealmVersionGreaterThanSchemaVersion, // OldVersion, NewVersion
RealmPropertyTypeNotIndexable, // ObjectType, PropertyName, PropertyType
RealmDuplicatePrimaryKeyValue, // ObjectType, PropertyName, PropertyType
ObjectSchemaMissingProperty, // ObjectType, PropertyName, PropertyType
ObjectSchemaNewProperty, // ObjectType, PropertyName, PropertyType
ObjectSchemaMismatchedTypes, // ObjectType, PropertyName, PropertyType, OldPropertyType
ObjectSchemaMismatchedObjectTypes, // ObjectType, PropertyName, PropertyType, ObjectType, OldObjectType
2015-06-16 23:19:34 +00:00
ObjectSchemaChangedPrimaryKey, // ObjectType, PrimaryKey
ObjectSchemaNewPrimaryKey, // ObjectType, PrimaryKey
ObjectStoreValidationFailure, // ObjectType, vector<ObjectStoreException>
};
2015-06-16 23:19:34 +00:00
typedef const std::string InfoKey;
typedef std::map<InfoKey, std::string> Info;
ObjectStoreException(Kind kind, const std::string &object_type, const Property &prop);
2015-06-16 23:19:34 +00:00
// ObjectSchemaMismatchedTypes, ObjectSchemaMismatchedObjectTypes
ObjectStoreException(Kind kind, const std::string &object_type, const Property &prop, const Property &oldProp);
2015-06-16 23:19:34 +00:00
// ObjectSchemaChangedPrimaryKey, ObjectSchemaNewPrimaryKey
ObjectStoreException(Kind kind, const std::string &object_type, const std::string primary_key);
// RealmVersionGreaterThanSchemaVersion
ObjectStoreException(uint64_t old_version, uint64_t new_version);
// ObjectStoreValidationFailure
ObjectStoreException(std::vector<ObjectStoreException> validation_errors, const std::string &object_type);
ObjectStoreException::Kind kind() const { return m_kind; }
const ObjectStoreException::Info &info() const { return m_info; }
const char *what() const noexcept override { return m_what.c_str(); }
// implement CustomWhat to customize exception messages per platform/language
2015-06-16 23:19:34 +00:00
typedef std::map<Kind, std::string> FormatStrings;
static void set_custom_format_strings(FormatStrings custom_format_strings) { s_custom_format_strings = custom_format_strings; }
private:
2015-06-16 23:19:34 +00:00
ObjectStoreException(Kind kind, Info info = Info());
Kind m_kind;
Info m_info;
std::vector<ObjectStoreException> m_validation_errors;
std::string m_what;
2015-06-16 23:19:34 +00:00
std::string generate_what() const;
std::string validation_errors_string() const;
std::string populate_format_string(const std::string &format_string) const;
2015-06-16 23:19:34 +00:00
static const FormatStrings s_default_format_strings;
static FormatStrings s_custom_format_strings;
public:
2015-06-17 00:08:07 +00:00
#define INFO_KEY(key) InfoKey InfoKey##key = "InfoKey" #key;
2015-06-16 23:19:34 +00:00
INFO_KEY(OldVersion);
INFO_KEY(NewVersion);
INFO_KEY(ObjectType);
INFO_KEY(PropertyName);
INFO_KEY(PropertyType);
INFO_KEY(OldPropertyType);
INFO_KEY(PropertyObjectType);
INFO_KEY(OldPropertyObjectType);
INFO_KEY(PrimaryKey);
2015-06-17 00:08:07 +00:00
#undef INFO_KEY
};
}
#endif /* defined(REALM_OBJECT_STORE_EXCEPTIONS_HPP) */