From e10e37bc20a454829a2f7490cb527e9acb638f34 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Wed, 15 Mar 2017 15:11:01 +0200 Subject: [core] Pass std::exception_ptr in style::Observer::onStyleError --- include/mbgl/util/exception.hpp | 15 +++++++++++++++ src/mbgl/map/map.cpp | 14 +++++++++----- src/mbgl/style/observer.hpp | 4 +++- src/mbgl/style/style.cpp | 6 ++++-- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/mbgl/util/exception.hpp b/include/mbgl/util/exception.hpp index 46de8528c7..a9804e96c5 100644 --- a/include/mbgl/util/exception.hpp +++ b/include/mbgl/util/exception.hpp @@ -20,5 +20,20 @@ struct MisuseException : Exception { MisuseException(const std::string &msg) : Exception(msg) {} }; +struct StyleParseException : Exception { + StyleParseException(const char *msg) : Exception(msg) {} + StyleParseException(const std::string &msg) : Exception(msg) {} +}; + +struct StyleLoadException : Exception { + StyleLoadException(const char *msg) : Exception(msg) {} + StyleLoadException(const std::string &msg) : Exception(msg) {} +}; + +struct NotFoundException : Exception { + NotFoundException(const char *msg) : Exception(msg) {} + NotFoundException(const std::string &msg) : Exception(msg) {} +}; + } // namespace util } // namespace mbgl diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index a006571892..39aa5a2d07 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -61,7 +62,7 @@ public: void onSourceAttributionChanged(style::Source&, const std::string&) override; void onUpdate(Update) override; void onStyleLoaded() override; - void onStyleError() override; + void onStyleError(std::exception_ptr) override; void onResourceError(std::exception_ptr) override; void render(View&); @@ -364,11 +365,14 @@ void Map::setStyleURL(const std::string& url) { if (res.error) { if (res.error->reason == Response::Error::Reason::NotFound && util::mapbox::isMapboxURL(impl->styleURL)) { - Log::Error(Event::Setup, "style %s could not be found or is an incompatible legacy map or style", impl->styleURL.c_str()); + const std::string message = "style " + impl->styleURL + " could not be found or is an incompatible legacy map or style"; + Log::Error(Event::Setup, message.c_str()); + impl->onStyleError(std::make_exception_ptr(util::NotFoundException(message))); } else { - Log::Error(Event::Setup, "loading style failed: %s", res.error->message.c_str()); + const std::string message = "loading style failed: " + res.error->message; + Log::Error(Event::Setup, message.c_str()); + impl->onStyleError(std::make_exception_ptr(util::StyleLoadException(message))); } - impl->onStyleError(); impl->onResourceError(std::make_exception_ptr(std::runtime_error(res.error->message))); } else if (res.notModified || res.noContent) { return; @@ -1098,7 +1102,7 @@ void Map::Impl::onStyleLoaded() { observer.onDidFinishLoadingStyle(); } -void Map::Impl::onStyleError() { +void Map::Impl::onStyleError(std::exception_ptr) { observer.onDidFailLoadingMap(); } diff --git a/src/mbgl/style/observer.hpp b/src/mbgl/style/observer.hpp index f28fdfc084..60432334e2 100644 --- a/src/mbgl/style/observer.hpp +++ b/src/mbgl/style/observer.hpp @@ -5,6 +5,8 @@ #include #include +#include + namespace mbgl { namespace style { @@ -13,7 +15,7 @@ class Observer : public GlyphAtlasObserver, public SourceObserver { public: virtual void onUpdate(Update) {} - virtual void onStyleError() {} + virtual void onStyleError(std::exception_ptr) {} virtual void onStyleLoaded() {} virtual void onResourceError(std::exception_ptr) {} }; diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index ac3d83b5cd..1db0807535 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -110,8 +111,9 @@ void Style::setJSON(const std::string& json) { auto error = parser.parse(json); if (error) { - Log::Error(Event::ParseStyle, "Failed to parse style: %s", util::toString(error).c_str()); - observer->onStyleError(); + std::string message = "Failed to parse style: " + util::toString(error); + Log::Error(Event::ParseStyle, message.c_str()); + observer->onStyleError(std::make_exception_ptr(util::StyleParseException(message))); observer->onResourceError(error); return; } -- cgit v1.2.1