blob: 783e9c68268a3133e17fc1a923361c1c271563a1 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.service.health;
import com.yahoo.yolean.Exceptions;
import java.util.Optional;
import java.util.OptionalInt;
/**
* The result of a health lookup.
*
* @author hakon
*/
public class HealthInfo {
public static final String UP_STATUS_CODE = "up";
private final Optional<Exception> exception;
private final OptionalInt httpStatusCode;
private final Optional<String> healthStatusCode;
static HealthInfo fromException(Exception exception) {
return new HealthInfo(Optional.of(exception), OptionalInt.empty(), Optional.empty());
}
static HealthInfo fromBadHttpStatusCode(int httpStatusCode) {
return new HealthInfo(Optional.empty(), OptionalInt.of(httpStatusCode), Optional.empty());
}
static HealthInfo fromHealthStatusCode(String healthStatusCode) {
return new HealthInfo(Optional.empty(), OptionalInt.empty(), Optional.of(healthStatusCode));
}
private HealthInfo(Optional<Exception> exception, OptionalInt httpStatusCode, Optional<String> healthStatusCode) {
this.exception = exception;
this.httpStatusCode = httpStatusCode;
this.healthStatusCode = healthStatusCode;
}
public boolean isHealthy() {
return healthStatusCode.map(UP_STATUS_CODE::equals).orElse(false);
}
public Optional<String> getErrorDescription() {
return isHealthy() ? Optional.empty() : Optional.of(toString());
}
@Override
public String toString() {
if (isHealthy()) {
return UP_STATUS_CODE;
} else if (healthStatusCode.isPresent()) {
return "Bad health status code '" + healthStatusCode.get() + "'";
} else if (exception.isPresent()) {
return "Exception: " + Exceptions.toMessageString(exception.get());
} else if (httpStatusCode.isPresent()) {
return "Bad HTTP response status code " + httpStatusCode.getAsInt();
} else {
return "No health info available";
}
}
}
|