From 5291a05d2cdd8a81f54b2e62d8714edb59fcb99f Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 21 Feb 2025 15:14:02 +0100 Subject: [PATCH] fix: backend route fix for error handling + fixed package import --- backend/src/routes/generateqrcode.route.ts | 21 ++++++++++++++------- backend/tsconfig.json | 1 + 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/backend/src/routes/generateqrcode.route.ts b/backend/src/routes/generateqrcode.route.ts index b8c0c5b..9ff2e9b 100644 --- a/backend/src/routes/generateqrcode.route.ts +++ b/backend/src/routes/generateqrcode.route.ts @@ -1,8 +1,8 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; -import QRCode from "qrcode"; +import * as QRCode from "qrcode"; interface RequestBody { - text: string; + qrcodeContent: string; } export async function generateQRCode(app: FastifyInstance) { @@ -14,19 +14,26 @@ export async function generateQRCode(app: FastifyInstance) { ) => { const data = request.body; - if (!data) { - return reply.status(400).send({ error: "Missing data parameter" }); + if (!data.qrcodeContent) { + return reply.status(400).send({ error: "Missing text parameter" }); + } else if (data.qrcodeContent === "") { + return reply + .status(400) + .send({ error: "Text parameter cannot be empty" }); + } else if (data.qrcodeContent.length > 1000) { + return reply.status(400).send({ error: "Text parameter too long" }); } try { - const qrCodeDataUrl = QRCode.toDataURL(data.text); + const qrCodeUrl = await QRCode.toDataURL(data.qrcodeContent); + console.log("QR Code generated:", qrCodeUrl); return reply .header("Content-Type", "application/json") .status(200) - .send({ qrCode: qrCodeDataUrl }); + .send({ qrCode: qrCodeUrl }); } catch (error) { console.error("QR Code generation error:", error); - reply.status(500).send({ error: "Error generating QR code" }); + return reply.status(500).send({ error: "Error generating QR code!" }); } }, ); diff --git a/backend/tsconfig.json b/backend/tsconfig.json index 2ef8988..b0de5ac 100644 --- a/backend/tsconfig.json +++ b/backend/tsconfig.json @@ -5,6 +5,7 @@ "outDir": "./dist", "rootDir": ".", "strict": true, + "esModuleInterop": true, "allowSyntheticDefaultImports": true } }