From 2bc0464789e74cf2597b9dc64f436dfe39c60861 Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Thu, 11 Jun 2020 20:41:37 +0200 Subject: [PATCH] improve robustness and error feedback --- .../diffplug/spotless/npm/SimpleRestClient.java | 15 ++++++++++++--- .../com/diffplug/spotless/npm/prettier-serve.js | 11 ++++++++++- .../com/diffplug/spotless/npm/tsfmt-serve.js | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/npm/SimpleRestClient.java b/lib/src/main/java/com/diffplug/spotless/npm/SimpleRestClient.java index c1c8804cb0..5958135d09 100644 --- a/lib/src/main/java/com/diffplug/spotless/npm/SimpleRestClient.java +++ b/lib/src/main/java/com/diffplug/spotless/npm/SimpleRestClient.java @@ -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; @@ -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); @@ -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); } } @@ -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()); } } diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js index d9e2d7c937..28e591f2ac 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/prettier-serve.js @@ -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); }); diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js index 8048cfa78c..b25048d410 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js @@ -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); }); });