java-json-tools / json-schema-validator

A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
http://json-schema-validator.herokuapp.com/
Other
1.63k stars 400 forks source link

Improcess error hanling in getJsonSchema - never squash exceptions #372

Open Johnlon opened 3 years ago

Johnlon commented 3 years ago

https://github.com/java-json-tools/json-schema-validator/blob/d265c6b15ca47a820de4a9d2f3de3b884c0a4b68/src/main/java/com/github/fge/jsonschema/main/JsonSchemaFactory.java#L207

The "Cannot happen" happens often enough in actual use when the user passes in a bad value for the definition pointer. For example one without a leading "/".

The code should deal with use input more conveniently by making some up front sanit checks perhaps, or at the very least don't just squash the JSONPointerException that comes from Jackson code as exception squashing is pure evil (if you don't mind me sayting so).

I'm trying to use this in a validator where folk provide a configurator and this kind of error handling means I have to check things up front where this library or it's transitive shoujld be doing it elegantly (nice error messages) instead.

public JsonSchema getJsonSchema(final JsonNode schema, final String ptr)
        throws ProcessingException
    {
        BUNDLE.checkNotNull(schema, "nullSchema");
        CORE_BUNDLE.checkNotNull(ptr, "nullPointer");
        final JsonPointer pointer;
        try {
            pointer = new JsonPointer(ptr);
            return validator.buildJsonSchema(schema, pointer);
        } catch (JsonPointerException ignored) {
            // Cannot happen
        }
        throw new IllegalStateException("How did I get there??");
    }