diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-22 15:10:24 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-23 12:50:42 -0800 |
commit | 16de579d7cfc2960793cbcb5e95741f22ab73768 (patch) | |
tree | b4c3b7651f605e3d3dd61b469f61036bd2c4dcc3 /src/mbgl/text/glyph_pbf.cpp | |
parent | 7bd4745cf10c504a4899a37016e87bce45e51472 (diff) | |
download | qtlocation-mapboxgl-16de579d7cfc2960793cbcb5e95741f22ab73768.tar.gz |
[core] Rationalize error handling for resource loading
* Standardize on std::exception_ptr as the error representation
(fixes #2854).
* Don't format textual strings at the error source; pass on the
constituent data via observer method parameters instead.
* Use the null object pattern to simplify observer notification code.
* Further refactoring for ResourceLoading tests.
Diffstat (limited to 'src/mbgl/text/glyph_pbf.cpp')
-rw-r--r-- | src/mbgl/text/glyph_pbf.cpp | 46 |
1 files changed, 11 insertions, 35 deletions
diff --git a/src/mbgl/text/glyph_pbf.cpp b/src/mbgl/text/glyph_pbf.cpp index c14f52de7a..0c0626c0de 100644 --- a/src/mbgl/text/glyph_pbf.cpp +++ b/src/mbgl/text/glyph_pbf.cpp @@ -12,8 +12,6 @@ #include <mbgl/util/token.hpp> #include <mbgl/util/url.hpp> -#include <sstream> - namespace { void parseGlyphPBF(mbgl::FontStack& stack, const std::string& data) { @@ -65,8 +63,10 @@ namespace mbgl { GlyphPBF::GlyphPBF(GlyphStore* store, const std::string& fontStack, - const GlyphRange& glyphRange) - : parsed(false) { + const GlyphRange& glyphRange, + GlyphStore::Observer* observer_) + : parsed(false), + observer(observer_) { // Load the glyph set URL std::string url = util::replaceTokens(store->getURL(), [&](const std::string &name) -> std::string { if (name == "fontstack") return util::percentEncode(fontStack); @@ -74,7 +74,7 @@ GlyphPBF::GlyphPBF(GlyphStore* store, return ""; }); - auto requestCallback = [this, store, fontStack, url](Response res) { + auto requestCallback = [this, store, fontStack, glyphRange](Response res) { if (res.stale) { // Only handle fresh responses. return; @@ -82,12 +82,10 @@ GlyphPBF::GlyphPBF(GlyphStore* store, req = nullptr; if (res.error) { - std::stringstream message; - message << "Failed to load [" << url << "]: " << res.error->message; - emitGlyphPBFLoadingFailed(message.str()); + observer->onGlyphsError(fontStack, glyphRange, std::make_exception_ptr(std::runtime_error(res.error->message))); } else { data = res.data; - parse(store, fontStack, url); + parse(store, fontStack, glyphRange); } }; @@ -97,7 +95,7 @@ GlyphPBF::GlyphPBF(GlyphStore* store, GlyphPBF::~GlyphPBF() = default; -void GlyphPBF::parse(GlyphStore* store, const std::string& fontStack, const std::string& url) { +void GlyphPBF::parse(GlyphStore* store, const std::string& fontStack, const GlyphRange& glyphRange) { assert(data); if (data->empty()) { // If there is no data, this means we either haven't @@ -107,35 +105,13 @@ void GlyphPBF::parse(GlyphStore* store, const std::string& fontStack, const std: try { parseGlyphPBF(**store->getFontStack(fontStack), *data); - } catch (const std::exception& ex) { - std::stringstream message; - message << "Failed to parse [" << url << "]: " << ex.what(); - emitGlyphPBFLoadingFailed(message.str()); + } catch (...) { + observer->onGlyphsError(fontStack, glyphRange, std::current_exception()); return; } parsed = true; - - emitGlyphPBFLoaded(); -} - -void GlyphPBF::setObserver(Observer* observer_) { - observer = observer_; -} - -void GlyphPBF::emitGlyphPBFLoaded() { - if (observer) { - observer->onGlyphPBFLoaded(); - } -} - -void GlyphPBF::emitGlyphPBFLoadingFailed(const std::string& message) { - if (!observer) { - return; - } - - auto error = std::make_exception_ptr(util::GlyphRangeLoadingException(message)); - observer->onGlyphPBFLoadingFailed(error); + observer->onGlyphsLoaded(fontStack, glyphRange); } } // namespace mbgl |