summaryrefslogtreecommitdiffstats
path: root/controller-api
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2021-04-26 11:56:44 +0200
committerJon Marius Venstad <venstad@gmail.com>2021-04-26 11:56:44 +0200
commit7bebb52b6d5091a1b44de4142fcfc6c93bfc5713 (patch)
treeabf401f1f9e345d0283f70e8fa2a9d0c36bf399b /controller-api
parentd5a79bc8f72a0c7b78341995c9d20bb9dbc50ff3 (diff)
Replace ConfigServerException
Diffstat (limited to 'controller-api')
-rw-r--r--controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/configserver/ConfigServerException.java57
1 files changed, 57 insertions, 0 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/configserver/ConfigServerException.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/configserver/ConfigServerException.java
new file mode 100644
index 00000000000..5e4d3345b7a
--- /dev/null
+++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/configserver/ConfigServerException.java
@@ -0,0 +1,57 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.hosted.controller.api.integration.configserver;
+
+import com.yahoo.slime.Inspector;
+import com.yahoo.slime.SlimeUtils;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+
+import java.util.stream.Stream;
+
+/**
+ * An exception due to server error, a bad request, or similar.
+ *
+ * @author jonmv
+ */
+public class ConfigServerException extends RuntimeException {
+
+ private final ErrorCode code;
+ private final String message;
+
+ public ConfigServerException(ErrorCode code, String message, String context) {
+ super(context + ": " + message);
+ this.code = code;
+ this.message = message;
+ }
+
+ public ErrorCode code() { return code; }
+
+ public String message() { return message; }
+
+ public enum ErrorCode {
+ APPLICATION_LOCK_FAILURE,
+ BAD_REQUEST,
+ ACTIVATION_CONFLICT,
+ INTERNAL_SERVER_ERROR,
+ INVALID_APPLICATION_PACKAGE,
+ METHOD_NOT_ALLOWED,
+ NOT_FOUND,
+ OUT_OF_CAPACITY,
+ REQUEST_TIMEOUT,
+ UNKNOWN_VESPA_VERSION,
+ PARENT_HOST_NOT_READY,
+ CERTIFICATE_NOT_READY,
+ LOAD_BALANCER_NOT_READY,
+ INCOMPLETE_RESPONSE
+ }
+
+ public static ConfigServerException readException(int statusCode, byte[] body, ClassicHttpRequest request) {
+ Inspector root = SlimeUtils.jsonToSlime(body).get();
+ String codeName = root.field("error-code").asString();
+ ErrorCode code = Stream.of(ErrorCode.values())
+ .filter(value -> value.name().equals(codeName))
+ .findAny().orElse(ErrorCode.INCOMPLETE_RESPONSE);
+ String message = root.field("message").valid() ? root.field("message").asString() : "(no message)";
+ return new ConfigServerException(code, message, request + " failed with status " + statusCode);
+ }
+
+}