Skip to content

Commit 2bc0464

Browse files
committed
improve robustness and error feedback
1 parent a12343f commit 2bc0464

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

lib/src/main/java/com/diffplug/spotless/npm/SimpleRestClient.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.io.BufferedInputStream;
2121
import java.io.IOException;
22+
import java.io.InputStream;
2223
import java.io.OutputStream;
2324
import java.net.HttpURLConnection;
2425
import java.net.URL;
@@ -67,7 +68,7 @@ String postJson(String endpoint, String rawJson) throws SimpleRestException {
6768
int status = con.getResponseCode();
6869

6970
if (status != 200) {
70-
throw new SimpleRestResponseException(status, con.getResponseMessage(), "Unexpected response status code.");
71+
throw new SimpleRestResponseException(status, readError(con), "Unexpected response status code at " + endpoint);
7172
}
7273

7374
String response = readResponse(con);
@@ -77,8 +78,16 @@ String postJson(String endpoint, String rawJson) throws SimpleRestException {
7778
}
7879
}
7980

81+
private String readError(HttpURLConnection con) throws IOException {
82+
return readInputStream(con.getErrorStream());
83+
}
84+
8085
private String readResponse(HttpURLConnection con) throws IOException {
81-
try (BufferedInputStream input = new BufferedInputStream(con.getInputStream())) {
86+
return readInputStream(con.getInputStream());
87+
}
88+
89+
private String readInputStream(InputStream inputStream) throws IOException {
90+
try (BufferedInputStream input = new BufferedInputStream(inputStream)) {
8291
return NpmResourceHelper.readUtf8StringFromInputStream(input);
8392
}
8493
}
@@ -121,7 +130,7 @@ public String getExceptionMessage() {
121130

122131
@Override
123132
public String getMessage() {
124-
return String.format("%s: %s (%s)", getStatusCode(), getResponseMessage(), getExceptionMessage());
133+
return String.format("%s [HTTP %s] -- (%s)", getExceptionMessage(), getStatusCode(), getResponseMessage());
125134
}
126135
}
127136

lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,27 @@ app.post("/prettier/config-options", (req, res) => {
4040
.resolveConfig(undefined, { config: prettier_config_path })
4141
.then(options => {
4242
var mergedConfigOptions = mergeConfigOptions(options, prettier_config_options);
43+
res.set("Content-Type", "application/json")
4344
res.json(mergedConfigOptions);
4445
})
4546
.catch(reason => res.status(501).send("Exception while resolving config_file_path: " + reason));
4647
return;
4748
}
49+
res.set("Content-Type", "application/json")
4850
res.json(prettier_config_options);
4951
});
5052

5153
app.post("/prettier/format", (req, res) => {
5254
var format_data = req.body;
5355

54-
var formatted_file_content = prettier.format(format_data.file_content, format_data.config_options);
56+
var formatted_file_content = "";
57+
try {
58+
console.log("format_data", format_data);
59+
formatted_file_content = prettier.format(format_data.file_content, format_data.config_options);
60+
} catch(err) {
61+
res.status(501).send("Error while formatting: " + err);
62+
return;
63+
}
5564
res.set("Content-Type", "text/plain");
5665
res.send(formatted_file_content);
5766
});

lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js

+2
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,7 @@ app.post("/tsfmt/format", (req, res) => {
5555
}
5656
res.set("Content-Type", "text/plain");
5757
res.send(resultMap.dest);
58+
}).catch(reason => {
59+
res.status(501).send(reason);
5860
});
5961
});

0 commit comments

Comments
 (0)