• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / nats.java / #1925

20 Mar 2025 03:46PM UTC coverage: 95.662% (-0.3%) from 95.95%
#1925

push

github

web-flow
Merge pull request #1239 from nats-io/main-2-11

Main for server v2.11

179 of 219 new or added lines in 15 files covered. (81.74%)

2 existing lines in 2 files now uncovered.

11600 of 12126 relevant lines covered (95.66%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.3
/src/main/java/io/nats/client/support/Status.java
1
// Copyright 2020 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at:
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package io.nats.client.support;
15

16
import java.util.HashMap;
17
import java.util.Map;
18

19
public class Status {
20

21
    public static final String FLOW_CONTROL_TEXT = "FlowControl Request";
22
    public static final String HEARTBEAT_TEXT = "Idle Heartbeat";
23
    public static final String NO_RESPONDERS_TEXT = "No Responders Available For Request";
24
    public static final String EOB_TEXT = "EOB";
25
    public static final int FLOW_OR_HEARTBEAT_STATUS_CODE = 100;
26
    public static final int NO_RESPONDERS_CODE = 503;
27
    public static final int BAD_REQUEST_CODE = 400;
28
    public static final int NOT_FOUND_CODE = 404;
29
    public static final int BAD_JS_REQUEST_CODE = 408;
30
    public static final int REQUEST_TIMEOUT_CODE = BAD_JS_REQUEST_CODE; // only left in for b/w compat
31
    public static final int CONFLICT_CODE = 409;
32
    public static final int EOB_CODE = 204;
33

34
    public static String BAD_REQUEST                    = "Bad Request"; // 400
1✔
35
    public static String NO_MESSAGES                    = "No Messages"; // 404
1✔
36
    public static String CONSUMER_DELETED               = "Consumer Deleted"; // 409
1✔
37
    public static String CONSUMER_IS_PUSH_BASED         = "Consumer is push based"; // 409
1✔
38

39
    public static String MESSAGE_SIZE_EXCEEDS_MAX_BYTES = "Message Size Exceeds MaxBytes"; // 409
1✔
40
    public static String EXCEEDED_MAX_WAITING           = "Exceeded MaxWaiting"; // 409
1✔
41
    public static String EXCEEDED_MAX_REQUEST_BATCH     = "Exceeded MaxRequestBatch"; // 409
1✔
42
    public static String EXCEEDED_MAX_REQUEST_EXPIRES   = "Exceeded MaxRequestExpires"; // 409
1✔
43
    public static String EXCEEDED_MAX_REQUEST_MAX_BYTES = "Exceeded MaxRequestMaxBytes"; // 409
1✔
44

45
    public static String BATCH_COMPLETED                = "Batch Completed"; // 409 informational
1✔
46
    public static String SERVER_SHUTDOWN                = "Server Shutdown"; // 409 informational with headers
1✔
47
    public static String LEADERSHIP_CHANGE              = "Leadership Change"; // 409
1✔
48

49
    public static final Status EOB = new Status(EOB_CODE, EOB_TEXT);
1✔
50
    public static final Status TIMEOUT_OR_NO_MESSAGES = new Status(NOT_FOUND_CODE, "Timeout or No Messages");
1✔
51

52
    private final int code;
53
    private final String message;
54

55
    private static final Map<Integer, String> CODE_TO_TEXT;
56

57
    static {
58
        CODE_TO_TEXT = new HashMap<>();
1✔
59
        CODE_TO_TEXT.put(NO_RESPONDERS_CODE, NO_RESPONDERS_TEXT);
1✔
60
    }
1✔
61

62
    public Status(int code, String message) {
1✔
63
        this.code = code;
1✔
64
        this.message = message == null ? makeMessage(code) : message ;
1✔
65
    }
1✔
66

67
    public Status(Token codeToken, Token messageToken) {
68
        this(extractCode(codeToken), extractMessage(messageToken));
1✔
69
    }
1✔
70

71
    public int getCode() {
72
        return code;
1✔
73
    }
74

75
    public String getMessage() {
76
        return message;
1✔
77
    }
78

79
    public String getMessageWithCode() {
80
        return code + " " + message;
1✔
81
    }
82

83
    private static String extractMessage(Token messageToken) {
84
        return messageToken.hasValue() ? messageToken.getValue() : null;
1✔
85
    }
86

87
    private static int extractCode(Token codeToken) {
88
        try {
89
            return Integer.parseInt(codeToken.getValue());
1✔
90
        }
91
        catch (Exception e) {
1✔
92
            throw new IllegalArgumentException(NatsConstants.INVALID_HEADER_STATUS_CODE);
1✔
93
        }
94
    }
95

96
    private String makeMessage(int code) {
97
        String message = CODE_TO_TEXT.get(code);
1✔
98
        return message == null ? "Server Status Message: " + code : message;
1✔
99
    }
100

101
    @Override
102
    public String toString() {
103
        return "Status{" +
1✔
104
                "code=" + code +
105
                ", message='" + message + '\'' +
106
                '}';
107
    }
108

109
    public boolean isFlowControl() {
110
        return code == FLOW_OR_HEARTBEAT_STATUS_CODE && message.equals(FLOW_CONTROL_TEXT);
1✔
111
    }
112

113
    public boolean isHeartbeat() {
114
        return code == FLOW_OR_HEARTBEAT_STATUS_CODE && message.equals(HEARTBEAT_TEXT);
1✔
115
    }
116

117
    public boolean isNoResponders() {
118
        return code == NO_RESPONDERS_CODE && message.equals(NO_RESPONDERS_TEXT);
1✔
119
    }
120

121
    public boolean isEob() {
NEW
122
        return code == EOB_CODE && message.equals(EOB_TEXT);
×
123
    }
124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc