Skip to content

Commit 79c7951

Browse files
committed
Update screenshot handling with improved error resilience and URL formatting
1 parent 5c55a8c commit 79c7951

File tree

8 files changed

+231
-41
lines changed

8 files changed

+231
-41
lines changed

.env.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ NODE_ENV=<development|production>
55
# Authentication
66
AUTH_USERNAME=<your_secure_username>
77
AUTH_PASSWORD=<your_secure_password>
8+
9+
# Puppeteer Configuration
810
CHROME_PATH=<path_to_chrome_executable>

app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import express from 'express';
44
import cors from 'cors';
55
import helmet from 'helmet';
66
import morgan from 'morgan';
7+
import path from 'path';
8+
import { fileURLToPath } from 'url';
79

810
// Import centralized routes
911
import routes from './routes.js';
@@ -18,6 +20,9 @@ import { swaggerDocs, swaggerUi } from './swagger.js';
1820
import controllerRouteNotFoundHandler from './controllers/route-not-found-handler.js';
1921
import controllerErrorHandler from './controllers/error-handler.js';
2022

23+
// Get directory name in ES module context
24+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
25+
2126
// Initialize Express application
2227
const app = express();
2328

@@ -50,6 +55,9 @@ app.use(morgan(morganFormat, { skip: skipOption }));
5055
// Sets a limit to handle large payloads
5156
app.use(express.json({ limit: API_CONFIG.JSON_LIMIT }));
5257

58+
// Serve static files from tmp directory
59+
app.use('/tmp', express.static(path.join(__dirname, 'tmp')));
60+
5361
// Set up Swagger documentation
5462
app.use(
5563
'/docs',

controllers/error-handler.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,40 @@
1010
* @param {Response} res - Express response object
1111
* @param {Function} next - Express next middleware function
1212
*/
13-
export default (error, req, res, next) => {
13+
14+
export default (error, _req, res, _next) => {
1415
// Log the complete error to server console for debugging
1516
console.error(error);
1617

1718
// Create standardized error response structure
1819
const result = {
1920
success: false,
20-
data: null,
21-
};
22-
23-
// Prepare detailed error information container
24-
const data = {
25-
message: null,
26-
stack: null,
21+
data: {
22+
message: null,
23+
stack: null
24+
}
2725
};
2826

2927
// Format error message based on error type
3028
if (error.name === 'ValidationError') {
3129
// Handle Joi validation errors with more details
32-
data.message = formatValidationError(error);
30+
result.data.message = formatValidationError(error);
3331
} else if (error.message) {
3432
// For general errors with messages
35-
data.message = formatErrorMessage(error.message);
33+
result.data.message = formatErrorMessage(error.message);
3634
} else {
3735
// Default message if error doesn't contain one
38-
data.message = 'Internal Server Error';
36+
result.data.message = 'Internal Server Error';
3937
}
4038

4139
// Include stack trace only in development environment
4240
if (process.env.NODE_ENV === 'development' && error.stack) {
43-
data.stack = formatStackTrace(error.stack);
41+
result.data.stack = formatStackTrace(error.stack);
4442
}
4543

46-
// Only include data field if we have meaningful information to share
47-
if (data.message || data.stack) {
48-
result.data = data;
44+
// Add screenshot path to data object (if exists)
45+
if (error.screenshot) {
46+
result.data.screenshot = error.screenshot;
4947
}
5048

5149
// Send response with appropriate status code

controllers/health.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import process from 'process';
1313

1414
// Custom helpers
1515
import checkPuppeteerHealth from '../helpers/puppeteer-health.js';
16+
import browserSemaphore from '../helpers/browser-semaphore.js';
1617

1718
/**
1819
* Health check controller function - GET /health endpoint handler
@@ -23,6 +24,9 @@ import checkPuppeteerHealth from '../helpers/puppeteer-health.js';
2324
*/
2425
export default async function (req, res, next) {
2526
try {
27+
// Acquire browser semaphore lock
28+
await browserSemaphore.acquire();
29+
2630
// Get system info
2731
const uptime = process.uptime();
2832

@@ -70,6 +74,9 @@ export default async function (req, res, next) {
7074
res.json(result);
7175
} catch (error) {
7276
next(error); // Pass the error to the error handler
77+
} finally {
78+
// Release browser semaphore lock
79+
browserSemaphore.release();
7380
}
7481
}
7582

0 commit comments

Comments
 (0)