move exception classes to their own file - store exception messages and implmenet what()

This commit is contained in:
Ari Lazier 2015-06-10 15:45:29 -07:00
parent b3b3136f9a
commit f972ab4278
5 changed files with 119 additions and 38 deletions

View File

@ -293,7 +293,7 @@ bool ObjectStore::create_tables(realm::Group *group, ObjectStore::Schema &target
bool ObjectStore::is_migration_required(realm::Group *group, uint64_t new_version) {
uint64_t old_version = get_schema_version(group);
if (old_version > new_version && old_version != NotVersioned) {
throw ObjectStoreException(ObjectStoreException::RealmVersionGreaterThanSchemaVersion);
throw ObjectStoreException(ObjectStoreException::Kind::RealmVersionGreaterThanSchemaVersion);
}
return old_version != new_version;
}
@ -387,7 +387,7 @@ bool ObjectStore::update_indexes(Group *group, Schema &schema) {
table->add_search_index(property.table_column);
}
catch (realm::LogicError const&) {
throw ObjectStoreException(ObjectStoreException::RealmPropertyTypeNotIndexable, {
throw ObjectStoreException(ObjectStoreException::Kind::RealmPropertyTypeNotIndexable, {
{"object_type", object_schema.name},
{"property_name", property.name},
{"property_type", string_for_property_type(property.type)}
@ -411,7 +411,8 @@ void ObjectStore::validate_primary_column_uniqueness(Group *group, Schema &schem
TableRef table = table_for_object_type(group, object_schema.name);
if (table->get_distinct_view(primary_prop->table_column).size() != table->size()) {
throw ObjectStoreException(ObjectStoreException::RealmDuplicatePrimaryKeyValue, {{"object_type", object_schema.name}, {"property_name", primary_prop->name}});
throw ObjectStoreException(ObjectStoreException::Kind::RealmDuplicatePrimaryKeyValue,
{{"object_type", object_schema.name}, {"property_name", primary_prop->name}});
}
}
}

View File

@ -24,6 +24,7 @@
#include <functional>
#include "object_schema.hpp"
#include "object_store_exceptions.hpp"
namespace realm {
class Group;
@ -105,38 +106,6 @@ namespace realm {
friend ObjectSchema;
};
class ObjectStoreException : public std::exception {
public:
enum Kind {
// thrown when calling update_realm_to_schema and the realm version is greater than the given version
RealmVersionGreaterThanSchemaVersion,
RealmPropertyTypeNotIndexable, // object_type, property_name, property_type
RealmDuplicatePrimaryKeyValue, // object_type, property_name
};
typedef std::map<std::string, std::string> Dict;
ObjectStoreException(Kind kind, Dict dict = Dict()) : m_kind(kind), m_dict(dict) {}
ObjectStoreException::Kind kind() const { return m_kind; }
const ObjectStoreException::Dict &dict() const { return m_dict; }
private:
Kind m_kind;
Dict m_dict;
};
class ObjectStoreValidationException : public std::exception {
public:
ObjectStoreValidationException(std::vector<std::string> validation_errors, std::string object_type) :
m_validation_errors(validation_errors), m_object_type(object_type) {}
const std::vector<std::string> &validation_errors() const { return m_validation_errors; }
std::string object_type() const { return m_object_type; }
private:
std::vector<std::string> m_validation_errors;
std::string m_object_type;
};
}
#endif /* defined(REALM_OBJECT_STORE_HPP) */

View File

@ -0,0 +1,45 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2014 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.
//
////////////////////////////////////////////////////////////////////////////
#include "object_store_exceptions.hpp"
using namespace realm;
using namespace std;
ObjectStoreException::ObjectStoreException(Kind kind, Dict dict) : m_kind(kind), m_dict(dict) {
switch (m_kind) {
case Kind::RealmVersionGreaterThanSchemaVersion:
m_what = "Schema version less than last set version in Realm";
break;
case Kind::RealmPropertyTypeNotIndexable:
m_what = "Can't index property '" + m_dict.at("object_type") + "." + m_dict.at("property_name") + "': " +
"indexing properties of type '" + m_dict.at("property_type") + "' is currently not supported";
break;
case Kind::RealmDuplicatePrimaryKeyValue:
m_what = "Primary key property '" + m_dict["property_name"] + "' has duplicate values after migration.";
break;
}
}
ObjectStoreValidationException::ObjectStoreValidationException(std::vector<std::string> validation_errors, std::string object_type) :
m_validation_errors(validation_errors), m_object_type(object_type) {
m_what = "Migration is required for object type '" + m_object_type + "' due to the following errors:";
for (auto error : m_validation_errors) {
m_what += "\n- " + error;
}
}

View File

@ -0,0 +1,66 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2014 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 ObjectStoreException : public std::exception {
public:
enum class Kind {
// thrown when calling update_realm_to_schema and the realm version is greater than the given version
RealmVersionGreaterThanSchemaVersion,
RealmPropertyTypeNotIndexable, // object_type, property_name, property_type
RealmDuplicatePrimaryKeyValue, // object_type, property_name
};
typedef std::map<std::string, std::string> Dict;
ObjectStoreException(Kind kind, Dict dict = Dict());
ObjectStoreException::Kind kind() const { return m_kind; }
const ObjectStoreException::Dict &dict() const { return m_dict; }
const char *what() const noexcept override { return m_what.c_str(); }
private:
Kind m_kind;
Dict m_dict;
std::string m_what;
};
class ObjectStoreValidationException : public std::exception {
public:
ObjectStoreValidationException(std::vector<std::string> validation_errors, std::string object_type);
const std::vector<std::string> &validation_errors() const { return m_validation_errors; }
std::string object_type() const { return m_object_type; }
const char *what() const noexcept override { return m_what.c_str(); }
private:
std::vector<std::string> m_validation_errors;
std::string m_object_type;
std::string m_what;
};
}
#endif /* defined(REALM_OBJECT_STORE_EXCEPTIONS_HPP) */

View File

@ -16,8 +16,8 @@
//
////////////////////////////////////////////////////////////////////////////
#ifndef __realm__property__
#define __realm__property__
#ifndef REALM_PROPERTY_HPP
#define REALM_PROPERTY_HPP
#include <string>
@ -83,4 +83,4 @@ namespace realm {
}
}
#endif /* defined__realm__property__ */
#endif /* REALM_PROPERTY_HPP */