Skip to content

Commit

Permalink
improve robustness and error feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
simschla committed Jun 11, 2020
1 parent a12343f commit 2bc0464
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
15 changes: 12 additions & 3 deletions lib/src/main/java/com/diffplug/spotless/npm/SimpleRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
Expand Down Expand Up @@ -67,7 +68,7 @@ String postJson(String endpoint, String rawJson) throws SimpleRestException {
int status = con.getResponseCode();

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

String response = readResponse(con);
Expand All @@ -77,8 +78,16 @@ String postJson(String endpoint, String rawJson) throws SimpleRestException {
}
}

private String readError(HttpURLConnection con) throws IOException {
return readInputStream(con.getErrorStream());
}

private String readResponse(HttpURLConnection con) throws IOException {
try (BufferedInputStream input = new BufferedInputStream(con.getInputStream())) {
return readInputStream(con.getInputStream());
}

private String readInputStream(InputStream inputStream) throws IOException {
try (BufferedInputStream input = new BufferedInputStream(inputStream)) {
return NpmResourceHelper.readUtf8StringFromInputStream(input);
}
}
Expand Down Expand Up @@ -121,7 +130,7 @@ public String getExceptionMessage() {

@Override
public String getMessage() {
return String.format("%s: %s (%s)", getStatusCode(), getResponseMessage(), getExceptionMessage());
return String.format("%s [HTTP %s] -- (%s)", getExceptionMessage(), getStatusCode(), getResponseMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,27 @@ app.post("/prettier/config-options", (req, res) => {
.resolveConfig(undefined, { config: prettier_config_path })
.then(options => {
var mergedConfigOptions = mergeConfigOptions(options, prettier_config_options);
res.set("Content-Type", "application/json")
res.json(mergedConfigOptions);
})
.catch(reason => res.status(501).send("Exception while resolving config_file_path: " + reason));
return;
}
res.set("Content-Type", "application/json")
res.json(prettier_config_options);
});

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

var formatted_file_content = prettier.format(format_data.file_content, format_data.config_options);
var formatted_file_content = "";
try {
console.log("format_data", format_data);
formatted_file_content = prettier.format(format_data.file_content, format_data.config_options);
} catch(err) {
res.status(501).send("Error while formatting: " + err);
return;
}
res.set("Content-Type", "text/plain");
res.send(formatted_file_content);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ app.post("/tsfmt/format", (req, res) => {
}
res.set("Content-Type", "text/plain");
res.send(resultMap.dest);
}).catch(reason => {
res.status(501).send(reason);
});
});

0 comments on commit 2bc0464

Please sign in to comment.