Skip to content

Commit

Permalink
eslint: reduce log-noise, cleanup error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
simschla committed Jan 5, 2023
1 parent 4598319 commit 603da1f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
20 changes: 12 additions & 8 deletions lib/src/main/resources/com/diffplug/spotless/npm/eslint-serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);

Expand All @@ -55,20 +57,22 @@ app.post("/eslint/format", async (req, res) => {
// 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;
res.set("Content-Type", "text/plain");
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);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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\")");
}
}
}
Expand Down

0 comments on commit 603da1f

Please sign in to comment.