[ 
https://issues.apache.org/jira/browse/AVRO-2113?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16424080#comment-16424080
 ] 

ASF GitHub Bot commented on AVRO-2113:
--------------------------------------

thiru-apache closed pull request #305: AVRO-2113 Fixed error reporting in C++ 
schema parsing
URL: https://github.com/apache/avro/pull/305
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/CHANGES.txt b/CHANGES.txt
index faea72cfd..41d0a2ef2 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -113,6 +113,8 @@ Trunk (not yet released)
 
     AVRO-2133: Log rat failures (gabor)
 
+    AVRO-2113: Improve unexpected type error message (thiru)
+
   BUG FIXES
 
     AVRO-1741: Python3: Fix error when codec is not in the header.
diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt
index be3921521..8919fc38a 100644
--- a/lang/c++/CMakeLists.txt
+++ b/lang/c++/CMakeLists.txt
@@ -20,6 +20,8 @@ cmake_minimum_required (VERSION 2.6)
 
 set (CMAKE_LEGACY_CYGWIN_WIN32 0)
 
+cmake_policy (SET CMP0042 NEW)
+
 if (NOT DEFINED CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS)
     set (CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_NO_WARNINGS ON)
 endif()
diff --git a/lang/c++/impl/Compiler.cc b/lang/c++/impl/Compiler.cc
index be5fe3f86..1252a717f 100644
--- a/lang/c++/impl/Compiler.cc
+++ b/lang/c++/impl/Compiler.cc
@@ -156,7 +156,9 @@ static void assertType(const Entity& e, EntityType et)
 {
     if (e.type() != et) {
         throw Exception(boost::format("Unexpected type for default value: "
-            "Expected %1%, but found %2%") % et % e.type());
+            "Expected %1%, but found %2% in line %3%") %
+                json::typeToString(et) % json::typeToString(e.type()) %
+                e.line());
     }
 }
 
@@ -205,8 +207,8 @@ static string nameof(const NodePtr& n)
     }
 }
 
-static GenericDatum makeGenericDatum(NodePtr n, const Entity& e,
-    const SymbolTable& st)
+static GenericDatum makeGenericDatum(NodePtr n,
+        const Entity& e, const SymbolTable& st)
 {
     Type t = n->type();
     if (t == AVRO_SYMBOLIC) {
diff --git a/lang/c++/impl/json/JsonDom.cc b/lang/c++/impl/json/JsonDom.cc
index 3f52f363b..f8174dbc7 100644
--- a/lang/c++/impl/json/JsonDom.cc
+++ b/lang/c++/impl/json/JsonDom.cc
@@ -31,7 +31,7 @@ using boost::format;
 
 namespace avro {
 namespace json {
-static const char* typeToString(EntityType t)
+const char* typeToString(EntityType t)
 {
     switch (t) {
     case etNull: return "null";
@@ -50,31 +50,33 @@ Entity readEntity(JsonParser& p)
     switch (p.peek()) {
     case JsonParser::tkNull:
         p.advance();
-        return Entity();
+        return Entity(p.line());
     case JsonParser::tkBool:
         p.advance();
-        return Entity(p.boolValue());
+        return Entity(p.boolValue(), p.line());
     case JsonParser::tkLong:
         p.advance();
-        return Entity(p.longValue());
+        return Entity(p.longValue(), p.line());
     case JsonParser::tkDouble:
         p.advance();
-        return Entity(p.doubleValue());
+        return Entity(p.doubleValue(), p.line());
     case JsonParser::tkString:
         p.advance();
-        return Entity(boost::make_shared<String>(p.stringValue()));
+        return Entity(boost::make_shared<String>(p.stringValue()), p.line());
     case JsonParser::tkArrayStart:
         {
+            size_t l = p.line();
             p.advance();
             boost::shared_ptr<Array> v = boost::make_shared<Array>();
             while (p.peek() != JsonParser::tkArrayEnd) {
                 v->push_back(readEntity(p));
             }
             p.advance();
-            return Entity(v);
+            return Entity(v, l);
         }
     case JsonParser::tkObjectStart:
         {
+            size_t l = p.line();
             p.advance();
             boost::shared_ptr<Object> v = boost::make_shared<Object>();
             while (p.peek() != JsonParser::tkObjectEnd) {
@@ -84,7 +86,7 @@ Entity readEntity(JsonParser& p)
                 v->insert(std::make_pair(k, n));
             }
             p.advance();
-            return Entity(v);
+            return Entity(v, l);
         }
     default:
         throw std::domain_error(JsonParser::toString(p.peek()));
diff --git a/lang/c++/impl/json/JsonDom.hh b/lang/c++/impl/json/JsonDom.hh
index 7de4fbcde..bde3b8f2c 100644
--- a/lang/c++/impl/json/JsonDom.hh
+++ b/lang/c++/impl/json/JsonDom.hh
@@ -59,21 +59,27 @@ enum EntityType {
     etObject
 };
 
+const char* typeToString(EntityType t);
+
 class AVRO_DECL Entity {
     EntityType type_;
     boost::any value_;
+    const size_t line_;
+
     void ensureType(EntityType) const;
 public:
-    Entity() : type_(etNull) { }
-    Entity(Bool v) : type_(etBool), value_(v) { }
-    Entity(Long v) : type_(etLong), value_(v) { }
-    Entity(Double v) : type_(etDouble), value_(v) { }
-    Entity(const boost::shared_ptr<String>& v) : type_(etString), value_(v) { }
-    Entity(const boost::shared_ptr<Array>& v) : type_(etArray), value_(v) { }
-    Entity(const boost::shared_ptr<Object>& v) : type_(etObject), value_(v) { }
+    Entity(size_t line = 0) : type_(etNull), line_(line) { }
+    Entity(Bool v, size_t line = 0) : type_(etBool), value_(v), line_(line) { }
+    Entity(Long v, size_t line = 0) : type_(etLong), value_(v), line_(line) { }
+    Entity(Double v, size_t line = 0) : type_(etDouble), value_(v), 
line_(line) { }
+    Entity(const boost::shared_ptr<String>& v, size_t line = 0) : 
type_(etString), value_(v), line_(line) { }
+    Entity(const boost::shared_ptr<Array>& v, size_t line = 0) : 
type_(etArray), value_(v), line_(line) { }
+    Entity(const boost::shared_ptr<Object>& v, size_t line = 0) : 
type_(etObject), value_(v), line_(line) { }
     
     EntityType type() const { return type_; }
 
+    size_t line() const { return line_; }
+
     Bool boolValue() const {
         ensureType(etBool);
         return boost::any_cast<Bool>(value_);
diff --git a/lang/c++/impl/json/JsonIO.cc b/lang/c++/impl/json/JsonIO.cc
index be5cc2f8b..175db96c0 100644
--- a/lang/c++/impl/json/JsonIO.cc
+++ b/lang/c++/impl/json/JsonIO.cc
@@ -40,6 +40,9 @@ char JsonParser::next()
 {
     char ch = hasNext ? nextChar : ' ';
     while (isspace(ch)) {
+        if (ch == '\n') {
+            line_++;
+        }
         ch = in_.read();
     }
     hasNext = false;
diff --git a/lang/c++/impl/json/JsonIO.hh b/lang/c++/impl/json/JsonIO.hh
index c87f73a26..186fd0c13 100644
--- a/lang/c++/impl/json/JsonIO.hh
+++ b/lang/c++/impl/json/JsonIO.hh
@@ -52,6 +52,8 @@ public:
         tkObjectEnd
     };
 
+    size_t line() const { return line_; }
+
 private:
     enum State {
         stValue,    // Expect a data type
@@ -73,6 +75,7 @@ private:
     int64_t lv;
     double dv;
     std::string sv;
+    size_t line_;
 
     Token doAdvance();
     Token tryLiteral(const char exp[], size_t n, Token tk);
@@ -82,7 +85,7 @@ private:
     char next();
 
 public:
-    JsonParser() : curState(stValue), hasNext(false), peeked(false) { }
+    JsonParser() : curState(stValue), hasNext(false), peeked(false), line_(1) 
{ }
 
     void init(InputStream& is) {
         in_.reset(is);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Improve unexpected type error message
> -------------------------------------
>
>                 Key: AVRO-2113
>                 URL: https://issues.apache.org/jira/browse/AVRO-2113
>             Project: Avro
>          Issue Type: Improvement
>          Components: c++
>            Reporter: Victor Mota
>            Assignee: Thiruvalluvan M. G.
>            Priority: Minor
>             Fix For: 1.8.3
>
>
> Currently the error message for default type mismatch is not very user 
> friendly:
> https://github.com/apache/avro/blob/17f2d75132021fafeca29edbdcade40df960fdc9/lang/c%2B%2B/impl/Compiler.cc#L158
> ie. "Unexpected type for default value: Expected 3, but found 2". Specifying 
> the field where this is happening and what the types mismatched are in human 
> readable format (ie. string, etc) would benefit the user a lot in debugging.
> Here is an example of a user having issues understanding the error: 
> https://issuetracker.google.com/issues/70351564.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to