aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/JsonReaderException.java
blob: a7b53d4cc43214ced58d5d5770d35d66511b660d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json;

import com.yahoo.document.DocumentId;
import com.yahoo.document.Field;

/**
 * @author bjorncs
 */
public class JsonReaderException extends IllegalArgumentException {

    public final DocumentId docId;
    public final Field field;
    public final Throwable cause;

    public JsonReaderException(DocumentId docId, Field field, Throwable cause) {
        super(createErrorMessage(docId, field, cause), cause);
        this.docId = docId;
        this.field = field;
        this.cause = cause;
    }

    public JsonReaderException(Field field, Throwable cause) {
        super(createErrorMessage(null, field, cause), cause);
        this.docId = null;
        this.field = field;
        this.cause = cause;
    }

    public static JsonReaderException addDocId(JsonReaderException oldException, DocumentId docId) {
        return new JsonReaderException(docId, oldException.field, oldException.cause);
    }

    private static String createErrorMessage(DocumentId docId, Field field, Throwable cause) {
        return String.format("Error in document '%s' - could not parse field '%s' of type '%s': %s",
                             docId, field.getName(), field.getDataType().getName(), cause.getMessage());
    }

    public DocumentId getDocId() {
        return docId;
    }

    public Field getField() {
        return field;
    }

}