Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: improve error handling

Marc Seitzbb55bc2988fa
diff --git a/lib/files/get-file.ts b/lib/files/get-file.ts
index 48eb6d49..5248e646 100644
--- a/lib/files/get-file.ts
+++ b/lib/files/get-file.ts
@@ -41,8 +41,15 @@ const getFileFromS3 = async (key: string) => {
);
if (!response.ok) {
- const error = await response.json();
- throw new Error(error.message);
+ try {
+ const error = await response.json();
+ throw new Error(
+ error.message || `Request failed with status ${response.status}`,
+ );
+ } catch (parseError) {
+ // Handle cases where the response isn't valid JSON
+ throw new Error(`Request failed with status ${response.status}`);
+ }
}
const { url } = (await response.json()) as { url: string };
diff --git a/pages/api/file/s3/get-presigned-get-url.ts b/pages/api/file/s3/get-presigned-get-url.ts
index c7f1e8dd..43735efb 100644
--- a/pages/api/file/s3/get-presigned-get-url.ts
+++ b/pages/api/file/s3/get-presigned-get-url.ts
@@ -20,9 +20,21 @@ export default async function handler(
// Extract the API Key from the Authorization header
const authHeader = req.headers.authorization;
+ if (!authHeader || !authHeader.startsWith("Bearer ")) {
+ return res
+ .status(401)
+ .json({ message: "Unauthorized: Invalid Authorization header" });
+ }
const token = authHeader?.split(" ")[1]; // Assuming the format is "Bearer [token]"
// Check if the API Key matches
+ if (!process.env.INTERNAL_API_KEY) {
+ log({
+ message: "INTERNAL_API_KEY environment variable is not set",
+ type: "error",
+ });
+ return res.status(500).json({ message: "Server configuration error" });
+ }
if (token !== process.env.INTERNAL_API_KEY) {
res.status(401).json({ message: "Unauthorized" });
return;