Skip to content

Commit e6f11e0

Browse files
committed
Emit errors as text response to the client
Also: Organize imports and refactor constant object.
1 parent dc9d562 commit e6f11e0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

pages/api/generate-css.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import postcss, { type CssSyntaxError } from "postcss";
1+
import postcss from "postcss";
22
import tailwindcss from "tailwindcss";
33
import autoprefixer from "autoprefixer";
4-
import { NextApiRequest, NextApiResponse } from "next";
54
import cssbeautify from "cssbeautify";
65

6+
import type { NextApiRequest, NextApiResponse } from "next";
7+
import type { ProcessOptions, CssSyntaxError } from "postcss";
8+
79
interface RequestBody {
810
classes: string;
911
}
@@ -15,6 +17,8 @@ const processor = postcss([
1517
autoprefixer,
1618
]);
1719

20+
const processOptions: ProcessOptions = { from: undefined };
21+
1822
const beautifyOptions: Parameters<typeof cssbeautify>[1] = {
1923
indent: " ",
2024
openbrace: "end-of-line",
@@ -35,7 +39,7 @@ export default async function handler(
3539
const css = `.generated { @apply ${classes.join(" ")}; }`;
3640

3741
let result = await processor
38-
.process(css, { from: undefined })
42+
.process(css, processOptions)
3943
.then((result) => result.css);
4044

4145
if (customClasses.length) {
@@ -62,13 +66,17 @@ export default async function handler(
6266

6367
//
6468
} else {
65-
res.status(500).send(error.toString());
69+
res
70+
.status(200)
71+
.send(error.reason || error.toString() || "500 Server Error");
6672
doneProcessing = true;
6773
}
6874

6975
//
7076
} else {
71-
res.status(500).send(error.toString());
77+
res
78+
.status(200)
79+
.send(error?.reason || error?.toString() || "500 Server Error");
7280
doneProcessing = true;
7381
}
7482
}

0 commit comments

Comments
 (0)