blob: 4c1d42d75ee1de82f0b2e9eef512f97680c76b98 (
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
|
/*
* Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
*/
package ai.vespa.metricsproxy.metric.model.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.logging.Logger;
import static java.util.logging.Level.WARNING;
/**
* An exception to be thrown upon errors in json rendering, and that can deliver its message wrapped
* in an "error" json object.
*
* @author gjoranv
*/
public class JsonRenderingException extends RuntimeException {
private static Logger log = Logger.getLogger(JsonRenderingException.class.getName());
JsonRenderingException(String message) {
super(message);
}
/**
* Returns the message wrapped in an "error" json object. In the unlikely case that json rendering of the
* error message fails, a plain text string will be returned instead.
*/
public String getMessageAsJson() {
return wrap(getMessage());
}
private static String wrap(String message) {
try {
return new ObjectMapper().writeValueAsString(Map.of("error", message));
} catch (JsonProcessingException e) {
log.log(WARNING, "Could not encode error message to json:", e);
return "Could not encode error message to json, check the log for details.";
}
}
}
|