aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/configserver/ConfigServerException.java
blob: 94a565b29745f0aac25a7d2acaf5765b264e0d64 (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
// Copyright Yahoo. 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 java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * 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) {
        super(message);
        this.code = code;
        this.message = 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,
        NODE_ALLOCATION_FAILURE,
        REQUEST_TIMEOUT,
        UNKNOWN_VESPA_VERSION,
        PARENT_HOST_NOT_READY,
        CERTIFICATE_NOT_READY,
        LOAD_BALANCER_NOT_READY,
        INCOMPLETE_RESPONSE,
        CONFIG_NOT_CONVERGED,
        QUOTA_EXCEEDED
    }

    // Note: Used by code in internal repo
    public static ConfigServerException readException(byte[] body, String context) {
        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() : new String(body, UTF_8);
        return new ConfigServerException(code, message, context);
    }

}