diff options
author | gjoranv <gv@oath.com> | 2018-10-31 16:45:35 +0100 |
---|---|---|
committer | gjoranv <gv@oath.com> | 2018-10-31 16:45:35 +0100 |
commit | 0413fee8be0a9df1a6a284205ed9b8269fc11af2 (patch) | |
tree | 33df4e92fb789aefaefd0ef9291db019cafacff1 /container-core | |
parent | 4c4723275523afc296c0b5c6346ef454580ce953 (diff) |
Move JSONObjectWithLegibleException to a separate file.
Diffstat (limited to 'container-core')
3 files changed, 86 insertions, 73 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/JSONObjectWithLegibleException.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/JSONObjectWithLegibleException.java new file mode 100644 index 00000000000..dc1bfb89197 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/JSONObjectWithLegibleException.java @@ -0,0 +1,86 @@ +// 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(); + } +} diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java index 42db1156f04..4859222d69a 100644 --- a/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/MetricsPacketsHandler.java @@ -4,7 +4,6 @@ package com.yahoo.container.jdisc.state; import com.google.inject.Inject; import com.yahoo.collections.Tuple2; import com.yahoo.component.provider.ComponentRegistry; -import com.yahoo.container.jdisc.state.StateHandler.JSONObjectWithLegibleException; import com.yahoo.jdisc.Request; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.Timer; diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/state/StateHandler.java b/container-core/src/main/java/com/yahoo/container/jdisc/state/StateHandler.java index da88a338049..0d858b66055 100644 --- a/container-core/src/main/java/com/yahoo/container/jdisc/state/StateHandler.java +++ b/container-core/src/main/java/com/yahoo/container/jdisc/state/StateHandler.java @@ -336,76 +336,4 @@ public class StateHandler extends AbstractRequestHandler { } } - static 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(); - } - } } |