Skip to content

Commit 3d66be2

Browse files
authored
1.2.6 Better failure error reporting. (#27)
1 parent f3c29d3 commit 3d66be2

File tree

10 files changed

+219
-62
lines changed

10 files changed

+219
-62
lines changed

.github/workflows/build-main.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,10 @@ jobs:
2222
with:
2323
java-version: '8'
2424
distribution: 'adopt'
25-
- name: Setup GO
26-
uses: actions/setup-go@v3
27-
with:
28-
go-version: '1.19.9'
2925
- name: Install Nats Server
3026
run: |
31-
cd $GITHUB_WORKSPACE
32-
git clone https://github.com/nats-io/nats-server.git
33-
cd nats-server
34-
go get
35-
go build main.go
36-
mkdir -p ~/.local/bin
37-
cp main ~/.local/bin/nats-server
38-
cd ..
39-
rm -rf nats-server
27+
curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@main | PREFIX=. sh
28+
sudo mv nats-server /usr/local/bin
4029
nats-server -v
4130
- name: Check out code
4231
uses: actions/checkout@v3

.github/workflows/build-pr.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,10 @@ jobs:
1616
with:
1717
java-version: '8'
1818
distribution: 'adopt'
19-
- name: Setup GO
20-
uses: actions/setup-go@v3
21-
with:
22-
go-version: '1.19.9'
2319
- name: Install Nats Server
2420
run: |
25-
cd $GITHUB_WORKSPACE
26-
git clone https://github.com/nats-io/nats-server.git
27-
cd nats-server
28-
go get
29-
go build main.go
30-
mkdir -p ~/.local/bin
31-
cp main ~/.local/bin/nats-server
32-
cd ..
33-
rm -rf nats-server
21+
curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@main | PREFIX=. sh
22+
sudo mv nats-server /usr/local/bin
3423
nats-server -v
3524
- name: Check out code
3625
uses: actions/checkout@v3

.github/workflows/build-release.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,10 @@ jobs:
2222
with:
2323
java-version: '8'
2424
distribution: 'adopt'
25-
- name: Setup GO
26-
uses: actions/setup-go@v3
27-
with:
28-
go-version: '1.19.9'
2925
- name: Install Nats Server
3026
run: |
31-
cd $GITHUB_WORKSPACE
32-
git clone https://github.com/nats-io/nats-server.git
33-
cd nats-server
34-
go get
35-
go build main.go
36-
mkdir -p ~/.local/bin
37-
cp main ~/.local/bin/nats-server
38-
cd ..
39-
rm -rf nats-server
27+
curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@main | PREFIX=. sh
28+
sudo mv nats-server /usr/local/bin
4029
nats-server -v
4130
- name: Check out code
4231
uses: actions/checkout@v3

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
id 'signing'
1212
}
1313

14-
def jarVersion = "1.2.5"
14+
def jarVersion = "1.2.6"
1515
group = 'io.nats'
1616

1717
def isMerge = System.getenv("BUILD_EVENT") == "push"

src/main/java/nats/io/ConsoleOutput.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void info(Supplier<String> msgSupplier) {
6868

6969
@Override
7070
public void info(String msg) {
71-
if (shouldShow(INFO)) {
71+
if (shouldShow(Level.INFO)) {
7272
System.out.println(format(INFO, msg));
7373
}
7474
}
@@ -83,7 +83,7 @@ public Logger getLogger() {
8383
return null;
8484
}
8585

86-
private boolean shouldShow(Level testLevel) {
87-
return level.intValue() <= testLevel.intValue();
86+
protected boolean shouldShow(Level testLevel) {
87+
return level.intValue() <= testLevel.intValue() && testLevel != OFF;
8888
}
8989
}

src/main/java/nats/io/NatsOutputLogger.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.lang.invoke.MethodType;
2222
import java.lang.reflect.Field;
2323
import java.nio.charset.StandardCharsets;
24+
import java.util.ArrayList;
25+
import java.util.List;
2426
import java.util.logging.Logger;
2527

2628
/**
@@ -35,21 +37,36 @@
3537
final class NatsOutputLogger implements Runnable {
3638
private final Output output;
3739
private final BufferedReader reader;
40+
private final List<String> startupLines;
41+
private boolean inStartupPhase;
3842

3943
private NatsOutputLogger(Output output, Process process) {
4044
this.output = output;
4145
this.reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
46+
startupLines = new ArrayList<>();
47+
inStartupPhase = true;
4248
}
4349

44-
private void logLine(String line) {
50+
public void endStartupPhase() {
51+
inStartupPhase = false;
52+
}
53+
54+
public List<String> getStartupLines() {
55+
return startupLines;
56+
}
57+
58+
public void logInfo(String line) {
4559
output.info(() -> line);
60+
if (inStartupPhase) {
61+
startupLines.add(line);
62+
}
4663
}
4764

4865
@Override
4966
public void run() {
5067
try {
5168
try {
52-
reader.lines().forEach(this::logLine);
69+
reader.lines().forEach(this::logInfo);
5370
} catch (final UncheckedIOException e) {
5471
output.warning(() -> "while reading output " + e);
5572
}
@@ -62,12 +79,14 @@ public void run() {
6279
}
6380
}
6481

65-
static void logOutput(final Output output, final Process process, final String processName) {
82+
static NatsOutputLogger logOutput(final Output output, final Process process, final String processName) {
6683
final String threadName = (isBlank(processName) ? "unknown" : processName) + ":" + processId(process);
67-
final Thread t = new Thread(new NatsOutputLogger(output, process));
84+
NatsOutputLogger nol = new NatsOutputLogger(output, process);
85+
final Thread t = new Thread(nol);
6886
t.setName(threadName);
6987
t.setDaemon(true);
7088
t.start();
89+
return nol;
7190
}
7291

7392
private static String processId(Process process) {

src/main/java/nats/io/NatsServerRunner.java

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.InetSocketAddress;
1919
import java.net.SocketAddress;
2020
import java.nio.channels.SocketChannel;
21+
import java.nio.file.Files;
2122
import java.nio.file.Path;
2223
import java.nio.file.Paths;
2324
import java.util.ArrayList;
@@ -35,6 +36,8 @@
3536
* Server Runner
3637
*/
3738
public class NatsServerRunner implements AutoCloseable {
39+
public static final String ERROR_NOTE_PART_1 = "Make sure that the nats-server is installed and in your PATH.";
40+
public static final String ERROR_NOTE_PART_2 = "See https://github.com/nats-io/nats-server for information on installation";
3841
public static long DEFAULT_PROCESS_CHECK_WAIT = 100;
3942
public static int DEFAULT_PROCESS_CHECK_TRIES = 10;
4043
public static long DEFAULT_RUN_CHECK_WAIT = 100;
@@ -310,8 +313,16 @@ protected NatsServerRunner(Builder b) throws IOException {
310313
_executablePath = b.executablePath == null ? getResolvedServerPath() : b.executablePath.toString();
311314
_port = b.port == null || b.port <= 0 ? nextPort() : b.port;
312315

313-
_displayOut = b.output == null ? DefaultOutputSupplier.get() : b.output;
314-
_displayOut.setLevel(b.outputLevel == null ? DefaultOutputLevel : b.outputLevel);
316+
if (b.output == null) {
317+
_displayOut = DefaultOutputSupplier.get();
318+
_displayOut.setLevel(DefaultOutputLevel);
319+
}
320+
else {
321+
_displayOut = b.output;
322+
if (b.outputLevel != null) {
323+
_displayOut.setLevel(b.outputLevel);
324+
}
325+
}
315326

316327
long procCheckWait = b.processCheckWait == null ? DEFAULT_PROCESS_CHECK_WAIT : b.processCheckWait;
317328
int procCheckTries = b.processCheckTries == null ? DEFAULT_PROCESS_CHECK_TRIES : b.processCheckTries;
@@ -361,6 +372,7 @@ protected NatsServerRunner(Builder b) throws IOException {
361372

362373
_cmdLine = String.join(" ", cmd);
363374

375+
NatsOutputLogger nol = null;
364376
try {
365377
ProcessBuilder pb = new ProcessBuilder(cmd);
366378
pb.redirectErrorStream(true);
@@ -369,8 +381,7 @@ protected NatsServerRunner(Builder b) throws IOException {
369381
_displayOut.info("%%% Starting [" + _cmdLine + "] with redirected IO");
370382

371383
process = pb.start();
372-
373-
NatsOutputLogger.logOutput(_displayOut, process, DEFAULT_NATS_SERVER);
384+
nol = NatsOutputLogger.logOutput(_displayOut, process, DEFAULT_NATS_SERVER);
374385

375386
int tries = procCheckTries;
376387
do {
@@ -401,15 +412,51 @@ protected NatsServerRunner(Builder b) throws IOException {
401412
}
402413

403414
_displayOut.info("%%% Started [" + _cmdLine + "]");
415+
nol.endStartupPhase();
404416
}
405417
catch (IOException ex) {
406-
_displayOut.info("%%% Failed to start [" + _cmdLine + "] with message:");
407-
_displayOut.info("\t" + ex.getMessage());
408-
_displayOut.info("%%% Make sure that the nats-server is installed and in your PATH.");
409-
_displayOut.info("%%% See https://github.com/nats-io/nats-server for information on installation");
418+
_displayOut.error("%%% Failed to run [" + _cmdLine + "]");
419+
String exMsg = ex.getMessage();
420+
if (exMsg != null) {
421+
_displayOut.error(" " + ex.getMessage());
422+
}
423+
_displayOut.error("%%% " + ERROR_NOTE_PART_1);
424+
_displayOut.error("%%% " + ERROR_NOTE_PART_2);
425+
StringBuilder exMessage = new StringBuilder("Failed to run [").append(_cmdLine).append("]");
426+
if (b.fullErrorReportOnStartup) {
427+
if (nol != null) {
428+
for (String line : nol.getStartupLines()) {
429+
exMessage.append(System.lineSeparator()).append(line);
430+
}
431+
}
432+
if (_cmdLine.contains(CONFIG_FILE_OPTION_NAME)) {
433+
String configPath = _configFile.getAbsolutePath();
434+
String configSep = getConfigSep(configPath);
435+
exMessage.append(System.lineSeparator()).append(configSep);
436+
exMessage.append(System.lineSeparator()).append(configPath);
437+
exMessage.append(System.lineSeparator()).append(configSep);
438+
try {
439+
List<String> lines = Files.readAllLines(_configFile.toPath());
440+
for (String line : lines) {
441+
exMessage.append(System.lineSeparator()).append(line);
442+
}
443+
}
444+
catch (Exception ignore) {
445+
}
446+
exMessage.append(System.lineSeparator()).append(configSep);
447+
}
448+
}
449+
throw new IllegalStateException(exMessage.toString());
450+
}
451+
}
410452

411-
throw new IllegalStateException("Failed to run [" + _cmdLine + "]");
453+
private String getConfigSep(String configPath) {
454+
StringBuilder sep = new StringBuilder("------------------------------");
455+
int len = configPath.length();
456+
while (sep.length() < len) {
457+
sep.append(sep);
412458
}
459+
return sep.substring(0, len);
413460
}
414461

415462
// ----------------------------------------------------------------------------------------------------
@@ -552,6 +599,7 @@ public static class Builder {
552599
Integer processCheckTries;
553600
Long connectCheckWait;
554601
Integer connectCheckTries;
602+
boolean fullErrorReportOnStartup = true;
555603

556604
public Builder port(Integer port) {
557605
this.port = port;
@@ -653,6 +701,11 @@ public Builder connectCheckTries(Integer connectCheckTries) {
653701
return this;
654702
}
655703

704+
public Builder fullErrorReportOnStartup(boolean expandedError) {
705+
this.fullErrorReportOnStartup = expandedError;
706+
return this;
707+
}
708+
656709
public Builder runnerOptions(NatsServerRunnerOptions nsro) {
657710
port(nsro.port())
658711
.debugLevel(nsro.debugLevel())

src/test/java/nats/io/NatsServerRunnerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,18 @@ public void testShutdownCoverage() throws Exception {
263263
Thread.sleep(1000);
264264
runner.shutdown();
265265
}
266+
267+
@Test
268+
public void testBadConfig() throws Exception {
269+
try (NatsServerRunner runner = NatsServerRunner.builder()
270+
.configFilePath(SOURCE_CONFIG_FILE_PATH + "bad.conf")
271+
.output(new ConsoleOutput())
272+
.build())
273+
{
274+
}
275+
catch (Exception e) {
276+
assertTrue(e.getMessage().contains("nats-server: Parse error on line 2"));
277+
System.out.println(e);
278+
}
279+
}
266280
}

0 commit comments

Comments
 (0)