From 0614eb8fa661c367d8a726b11a9cb89d93acf653 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Fri, 14 Oct 2016 18:50:14 +0900 Subject: [PATCH] Improve error messsage a bit. --- tiny_gltf_loader.h | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/tiny_gltf_loader.h b/tiny_gltf_loader.h index 43bc77b..3f05a34 100644 --- a/tiny_gltf_loader.h +++ b/tiny_gltf_loader.h @@ -346,8 +346,7 @@ class Scene { Asset asset; }; -enum SectionCheck -{ +enum SectionCheck { NO_REQUIRE = 0x00, REQUIRE_SCENE = 0x01, REQUIRE_SCENES = 0x02, @@ -937,14 +936,20 @@ static bool ParseNumberArrayProperty(std::vector *ret, std::string *err, return true; } -static bool ParseStringProperty(std::string *ret, std::string *err, - const picojson::object &o, - const std::string &property, bool required) { +static bool ParseStringProperty( + std::string *ret, std::string *err, const picojson::object &o, + const std::string &property, bool required, + const std::string &parent_node = std::string()) { picojson::object::const_iterator it = o.find(property); if (it == o.end()) { if (required) { if (err) { - (*err) += "'" + property + "' property is missing.\n"; + (*err) += "'" + property + "' property is missing"; + if (parent_node.empty()) { + (*err) += ".\n"; + } else { + (*err) += " in `" + parent_node + "'.\n"; + } } } return false; @@ -1480,7 +1485,8 @@ static bool ParseAccessor(Accessor *accessor, std::string *err, static bool ParsePrimitive(Primitive *primitive, std::string *err, const picojson::object &o) { - if (!ParseStringProperty(&primitive->material, err, o, "material", true)) { + if (!ParseStringProperty(&primitive->material, err, o, "material", true, + "mesh.primitive")) { return false; } @@ -1501,10 +1507,7 @@ static bool ParsePrimitive(Primitive *primitive, std::string *err, primitive->indices = ""; ParseStringProperty(&primitive->indices, err, o, "indices", false); - if (!ParseStringMapProperty(&primitive->attributes, err, o, "attributes", - true)) { - return false; - } + ParseStringMapProperty(&primitive->attributes, err, o, "attributes", false); return true; } @@ -1671,7 +1674,7 @@ static bool ParseShader(Shader *shader, std::string *err, } } else { // Load shader source from data uri - // TODO: Support ascii or utf-8 data uris. + // TODO(syoyo): Support ascii or utf-8 data uris. if (IsDataURI(uri)) { if (!DecodeDataURI(&shader->source, uri, 0, false)) { if (err) { @@ -2328,7 +2331,6 @@ bool TinyGLTFLoader::LoadASCIIFromFile(Scene *scene, std::string *err, static_cast(buf.size()), basedir, check_sections); - return ret; }