UcasRichard / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

Please set the "cause" field for MarkedYAMLException #5

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

class Bean {
  String a = null;
  public Bean() {}
  public String getA() { return a; }
  public void setA(String s) { a = s.intern(); }
}

If we dump just new Bean() without setting a, and then try to load it, we
get the cryptic error message

Can't construct a java object for tag:[TAG];
exception=java.lang.reflect.InvocationTargetException

This is because in constructor.Constructor.java, in
ConstructYamlObject.construct(), you catch (Exception e), but you discard
all the information in this exception, instead only adding e.getMessage()
to the new exception's message. 

You should make the constructor of MarkedYAMLException accept the standard
"Throwable cause" argument of all Throwables, then we can call
printStackTrace() on that exception to get a more meaningful error message,
something like this:

    [junit] org.yaml.snakeyaml.error.YAMLException:
java.lang.reflect.InvocationTargetException
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor.constructMappingNode2ndStep(Construct
or.java:394)
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor.constructMappingNode(Constructor.java
:341)
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor.access$200(Constructor.java:33)
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constru
ctor.java:108)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.callConstructor(BaseConstructor.j
ava:117)
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor.callConstructor(Constructor.java:157)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.j
ava:107)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstr
uctor.java:148)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor
.java:142)
    [junit]     at
org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlSeq.construct(SafeCo
nstructor.java:404)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.callConstructor(BaseConstructor.j
ava:117)
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor.callConstructor(Constructor.java:157)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.j
ava:107)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping2ndStep(BaseConst
ructor.java:189)
    [junit]     at
org.yaml.snakeyaml.constructor.SafeConstructor.constructMapping2ndStep(SafeConst
ructor.java:104)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructMapping(BaseConstructor.
java:170)
    [junit]     at
org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlMap.construct(SafeCo
nstructor.java:423)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.callConstructor(BaseConstructor.j
ava:117)
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor.callConstructor(Constructor.java:157)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.j
ava:107)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor
.java:74)
    [junit]     at
org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.jav
a:68)
    [junit]     at org.yaml.snakeyaml.Loader.load(Loader.java:39)
    [junit]     at org.yaml.snakeyaml.Yaml.load(Yaml.java:164)
    [junit]     at plugins.Interdex.serl.YamlArchiver.pull(YamlArchiver.java:79)
    [junit]     at
plugins.Interdex.index.TokenEntryTest.testBasic(TokenEntryTest.java:57)
    [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [junit]     at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    [junit]     at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:43)
    [junit]     at java.lang.reflect.Method.invoke(Method.java:616)
    [junit]     at junit.framework.TestCase.runTest(TestCase.java:164)
    [junit]     at junit.framework.TestCase.runBare(TestCase.java:130)
    [junit]     at junit.framework.TestResult$1.protect(TestResult.java:106)
    [junit]     at junit.framework.TestResult.runProtected(TestResult.java:124)
    [junit]     at junit.framework.TestResult.run(TestResult.java:109)
    [junit]     at junit.framework.TestCase.run(TestCase.java:120)
    [junit]     at junit.framework.TestSuite.runTest(TestSuite.java:230)
    [junit]     at junit.framework.TestSuite.run(TestSuite.java:225)
    [junit]     at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:91)
    [junit]     at
junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
    [junit]     at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner
.java:421)
    [junit]     at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRun
ner.java:912)
    [junit]     at
org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunne
r.java:766)
    [junit] Caused by: java.lang.reflect.InvocationTargetException
    [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [junit]     at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    [junit]     at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:43)
    [junit]     at java.lang.reflect.Method.invoke(Method.java:616)
    [junit]     at
org.yaml.snakeyaml.introspector.MethodProperty.set(MethodProperty.java:20)
    [junit]     at
org.yaml.snakeyaml.constructor.Constructor.constructMappingNode2ndStep(Construct
or.java:392)
    [junit]     ... 42 more
    [junit] Caused by: java.lang.NullPointerException
    [junit]     at Bean.setA(Bean.java:5)
    [junit]     ... 48 more

which helps the user track down what's causing the error.

As applied to the example code, this error trace now helps us to track down
what's causing YAML to throw the exception - since we don't setA() before
dumping, a is set to null, and when the YAML constructor comes to loading
it, it will call setA(null), giving a NullPointerException when we try to
do null.intern().

Original issue reported on code.google.com by infinity0x@gmail.com on 9 Jul 2009 at 8:28

GoogleCodeExporter commented 9 years ago
Done

Original comment by py4fun@gmail.com on 10 Jul 2009 at 7:59

GoogleCodeExporter commented 9 years ago

Original comment by aso...@gmail.com on 6 Aug 2009 at 10:01