diff options
Diffstat (limited to 'container-core/src/main/java/com/yahoo/container/handler/metrics/ErrorResponse.java')
-rw-r--r-- | container-core/src/main/java/com/yahoo/container/handler/metrics/ErrorResponse.java | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/handler/metrics/ErrorResponse.java b/container-core/src/main/java/com/yahoo/container/handler/metrics/ErrorResponse.java new file mode 100644 index 00000000000..321f7b3994a --- /dev/null +++ b/container-core/src/main/java/com/yahoo/container/handler/metrics/ErrorResponse.java @@ -0,0 +1,32 @@ +// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.handler.metrics; + +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; + +/** + * @author gjoranv + */ +public class ErrorResponse extends JsonResponse { + private static Logger log = Logger.getLogger(ErrorResponse.class.getName()); + + private static ObjectMapper objectMapper = new ObjectMapper(); + + public ErrorResponse(int code, String message) { + super(code, asErrorJson(message)); + } + + static String asErrorJson(String message) { + try { + return 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."; + } + } +} |