don't throw an exception in the map thread for bad styles

This commit is contained in:
Konstantin Käfer
2015-04-16 16:11:40 +02:00
parent 46bf226910
commit 1c920c4332
4 changed files with 35 additions and 22 deletions

View File

@@ -5,7 +5,6 @@
#include <mbgl/style/style_parser.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/error.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/platform/log.hpp>
@@ -75,7 +74,7 @@ void Style::loadJSON(const uint8_t *const data) {
doc.Parse<0>((const char *const)data);
if (doc.HasParseError()) {
Log::Error(Event::ParseStyle, "Error parsing style JSON at %i: %s", doc.GetErrorOffset(), doc.GetParseError());
throw error::style_parse(doc.GetErrorOffset(), doc.GetParseError());
return;
}
StyleParser parser;

View File

@@ -1,20 +0,0 @@
#ifndef MBGL_UTIL_ERROR
#define MBGL_UTIL_ERROR
#include <stdexcept>
#include <string>
namespace mbgl {
namespace error {
struct style_parse : std::exception {
inline style_parse(size_t offset_, const char *msg_) : offset(offset_), msg(msg_) {}
inline const char* what() const noexcept { return msg.c_str(); }
const size_t offset;
const std::string msg;
};
}
}
#endif

32
test/api/set_style.cpp Normal file
View File

@@ -0,0 +1,32 @@
#include "../fixtures/util.hpp"
#include "../fixtures/fixture_log_observer.hpp"
#include <mbgl/map/map.hpp>
#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/storage/default_file_source.hpp>
TEST(API, SetStyle) {
using namespace mbgl;
auto display = std::make_shared<mbgl::HeadlessDisplay>();
HeadlessView view(display);
DefaultFileSource fileSource(nullptr);
Log::setObserver(util::make_unique<FixtureLogObserver>());
Map map(view, fileSource);
map.start(Map::Mode::Static);
map.setStyleJSON("invalid", "test/suite");
map.stop();
auto observer = Log::removeObserver();
auto flo = dynamic_cast<FixtureLogObserver*>(observer.get());
EXPECT_EQ(1ul, flo->count({ EventSeverity::Error, Event::ParseStyle, -1,
"Error parsing style JSON at 0: Expect either an object or array at root" }));
EXPECT_TRUE(flo->unchecked().empty());
}

View File

@@ -35,6 +35,8 @@
'fixtures/fixture_log_observer.hpp',
'fixtures/fixture_log_observer.cpp',
'api/set_style.cpp',
'headless/headless.cpp',
'miscellaneous/clip_ids.cpp',