Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect exception type? #20

Closed
user454322 opened this issue Feb 10, 2014 · 1 comment
Closed

Incorrect exception type? #20

user454322 opened this issue Feb 10, 2014 · 1 comment
Labels

Comments

@user454322
Copy link

I like this library....but I found that:

JsonObject.readFrom(String) 's javadoc says that it throws an UnsupportedOperationException but it actually throws a com.eclipsesource.json.ParseException

Description

In JsonObject either the javadoc or the code is wrong.

the method's javadoc readFrom(String) says

   * @throws UnsupportedOperationException
   *           if the input does not contain a JSON object

However, if the input doesn't contain a JSON object it throws a com.eclipsesource.json.ParseException instead.

lines 126-139 in
https://github.com/ralfstx/minimal-json/blob/abf82149104ff3c70142ff1062e406cbd490da45/com.eclipsesource.json/src/main/java/com/eclipsesource/json/JsonObject.java

Seems like the javadoc and the actual exception being thrown don't match in other places.
Probably related to #8

How to reproduce it:

Running this unit test

package com.eclipsesource.json;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class JsonObjectReadFromTest {

    @Test(expected = java.lang.UnsupportedOperationException.class)
    public void JsonObjectReadFrom() {
        JsonObject jsonObject = JsonObject
                .readFrom("This is not a JSON object");

    }

    @Test
    public void parseExceptionIsUnsupportedOperationException() {
        Exception pex = new ParseException("", 0, 0, 0);
        assertTrue("ParseException is not an UnsupportedOperationException ",
                pex instanceof UnsupportedOperationException);
    }

}

The result is

java.lang.Exception: Unexpected exception, expected<java.lang.UnsupportedOperationException> but was<com.eclipsesource.json.ParseException>
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
....
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
    ... 15 more
@ralfstx ralfstx added the bug label May 31, 2015
@ralfstx
Copy link
Owner

ralfstx commented May 31, 2015

Throwing a ParseException is correct, because the provided string is invalid JSON. If you rewrite your test to read a valid JSON string, i.e. include quotes, then an UnsupportedOperationException is thrown:

@Test(expected = UnsupportedOperationException.class)
public void JsonObjectReadFrom() {
  JsonObject.readFrom("\"This is not a JSON object\"");
}

Question is if something like a "JsonTypeException" wouldn't be a better choice (see #8).

@ralfstx ralfstx closed this as completed May 31, 2015
ralfstx added a commit that referenced this issue May 31, 2015
Ensure that the correct exceptions are thrown for illegal JSON and
unexpected JSON types in JsonObject/JsonArray readFrom methods.

#20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants