From cc3047caf6b2b3acb07f8a420b941510bec36548 Mon Sep 17 00:00:00 2001 From: Simon Gamma Date: Thu, 5 Jan 2023 07:00:51 +0100 Subject: [PATCH] eslint: reduce log-noise, cleanup error codes --- .../com/diffplug/spotless/npm/common-serve.js | 3 ++- .../com/diffplug/spotless/npm/eslint-serve.js | 24 ++++++++++++------- .../diffplug/spotless/npm/prettier-serve.js | 2 +- .../com/diffplug/spotless/npm/tsfmt-serve.js | 2 +- .../npm/PrettierFormatterStepTest.java | 2 +- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js index 3e031cedea..6a87a7449d 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js @@ -1,4 +1,5 @@ -// this file will be glued to the top of the specific xy-server.js file +// this file will be glued to the top of the specific xy-serve.js file +const debug_serve = false; const GracefulShutdownManager = require("@moebius/http-graceful-shutdown").GracefulShutdownManager; const express = require("express"); const app = express(); diff --git a/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js b/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js index 1f1b1fab5a..4c260def39 100644 --- a/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js +++ b/lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js @@ -9,14 +9,14 @@ app.post("/eslint/format", async (req, res) => { const ESLintOverrideConfigFile = format_data.eslint_override_config_file; if (!ESLintOverrideConfig && !ESLintOverrideConfigFile) { - res.status(501).send("Error while formatting: No config provided"); + res.status(400).send("Error while formatting: No config provided"); return; } const filePath = format_data.file_path; if (!filePath) { - res.status(501).send("Error while formatting: No file path provided"); + res.status(400).send("Error while formatting: No file path provided"); return; } @@ -41,8 +41,10 @@ app.post("/eslint/format", async (req, res) => { eval("ESLintOptions.overrideConfig = " + ESLintOverrideConfig); } - console.log("using options: " + JSON.stringify(ESLintOptions)); - console.log("format input: ", format_data.file_content); + if (debug_serve) { + console.log("using options: " + JSON.stringify(ESLintOptions)); + console.log("format input: ", format_data.file_content); + } const eslint = new ESLint(ESLintOptions); @@ -50,18 +52,22 @@ app.post("/eslint/format", async (req, res) => { const lintTextOptions = { filePath: filePath, } - console.log("lintTextOptions", lintTextOptions); + if (debug_serve) { + console.log("lintTextOptions", lintTextOptions); + } // LintResult[] // https://eslint.org/docs/latest/developer-guide/nodejs-api#-lintresult-type const results = await eslint.lintText(format_data.file_content, lintTextOptions); if (results.length !== 1) { - res.status(501).send("Error while formatting: Unexpected number of results: " + JSON.stringify(results)); + res.status(500).send("Error while formatting: Unexpected number of results: " + JSON.stringify(results)); return; } const result = results[0]; - console.log("result: " + JSON.stringify(result)); + if (debug_serve) { + console.log("result: " + JSON.stringify(result)); + } if (result.fatalErrorCount && result.fatalErrorCount > 0) { - res.status(501).send("Fatal error while formatting: " + JSON.stringify(result.messages)); + res.status(500).send("Fatal error while formatting: " + JSON.stringify(result.messages)); return; } const formatted = result.output || result.source || format_data.file_content; @@ -69,6 +75,6 @@ app.post("/eslint/format", async (req, res) => { res.send(formatted); } catch (err) { console.log("error", err); - res.status(501).send("Error while formatting: " + err); + res.status(500).send("Error while formatting: " + err); } }); 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 d4ce13bbbc..b60daaaa77 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 @@ -27,7 +27,7 @@ app.post("/prettier/format", (req, res) => { try { formatted_file_content = prettier.format(format_data.file_content, format_data.config_options); } catch(err) { - res.status(501).send("Error while formatting: " + err); + res.status(500).send("Error while formatting: " + err); return; } res.set("Content-Type", "text/plain"); 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 8ec25565ff..b9f20a1472 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 @@ -25,6 +25,6 @@ app.post("/tsfmt/format", (req, res) => { res.set("Content-Type", "text/plain"); res.send(resultMap.dest); }).catch(reason => { - res.status(501).send(reason); + res.status(500).send(reason); }); }); diff --git a/testlib/src/test/java/com/diffplug/spotless/npm/PrettierFormatterStepTest.java b/testlib/src/test/java/com/diffplug/spotless/npm/PrettierFormatterStepTest.java index 9db218efb5..acc756fa31 100644 --- a/testlib/src/test/java/com/diffplug/spotless/npm/PrettierFormatterStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/npm/PrettierFormatterStepTest.java @@ -116,7 +116,7 @@ void verifyPrettierErrorMessageIsRelayed() throws Exception { new PrettierConfig(null, ImmutableMap.of("parser", "postcss"))); try (StepHarnessWithFile stepHarness = StepHarnessWithFile.forStep(this, formatterStep)) { stepHarness.testResourceExceptionMsg("npm/prettier/filetypes/scss/scss.dirty").isEqualTo( - "Unexpected response status code at /prettier/format [HTTP 501] -- (Error while formatting: Error: Couldn't resolve parser \"postcss\")"); + "Unexpected response status code at /prettier/format [HTTP 500] -- (Error while formatting: Error: Couldn't resolve parser \"postcss\")"); } } }