aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/jdisc/state/JSONObjectWithLegibleException.java
blob: d22dd9d6f4b01315673a8f99de8053d67a0cac25 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.state;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collection;
import java.util.Map;

/**
 * A JSONObject that wraps the checked JSONException in a RuntimeException with a legible error message.
 *
 * @author gjoranv
 */
class JSONObjectWithLegibleException extends JSONObject {

    @Override
    public JSONObject put(String s, boolean b) {
        try {
            return super.put(s, b);
        } catch (JSONException e) {
            throw new RuntimeException(getErrorMessage(s, b, e), e);
        }
    }

    @Override
    public JSONObject put(String s, double v) {
        try {
            Double guardedVal = (((Double) v).isNaN() || ((Double) v).isInfinite()) ?
                    0.0 : v;
            return super.put(s, guardedVal);
        } catch (JSONException e) {
            throw new RuntimeException(getErrorMessage(s, v, e), e);
        }
    }

    @Override
    public JSONObject put(String s, int i) {
        try {
            return super.put(s, i);
        } catch (JSONException e) {
            throw new RuntimeException(getErrorMessage(s, i, e), e);
        }
    }

    @Override
    public JSONObject put(String s, long l) {
        try {
            return super.put(s, l);
        } catch (JSONException e) {
            throw new RuntimeException(getErrorMessage(s, l, e), e);
        }
    }

    @Override
    public JSONObject put(String s, Collection collection) {
        try {
            return super.put(s, collection);
        } catch (JSONException e) {
            throw new RuntimeException(getErrorMessage(s, collection, e), e);
        }
    }

    @Override
    public JSONObject put(String s, Map map) {
        try {
            return super.put(s, map);
        } catch (JSONException e) {
            throw new RuntimeException(getErrorMessage(s, map, e), e);
        }
    }

    @Override
    public JSONObject put(String s, Object o) {
        try {
            return super.put(s, o);
        } catch (JSONException e) {
            throw new RuntimeException(getErrorMessage(s, o, e), e);
        }
    }

    private String getErrorMessage(String key, Object value, JSONException e) {
        return "Trying to add invalid JSON object with key '" + key +
                "' and value '" + value + "' - " + e.getMessage();
    }

}