mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 09:52:09 +01:00
Fix error handling to properly use formatError utility and improve environment detection
- Actually use the formatError function in the error handling code - Update formatError function to accept env parameter for Cloudflare Workers - Improve environment detection to use WORKER_ENV instead of process.env - Update tests to match new function signature - Add comprehensive test coverage for error formatting - Fix linting issues and ensure proper TypeScript types
This commit is contained in:
@ -9,6 +9,7 @@ export interface Env {
|
||||
DB: D1Database;
|
||||
NEXTAUTH_SECRET?: string;
|
||||
NEXTAUTH_URL?: string;
|
||||
WORKER_ENV?: string; // 'development' | 'production'
|
||||
}
|
||||
|
||||
export default {
|
||||
@ -210,8 +211,12 @@ export default {
|
||||
|
||||
} catch (error) {
|
||||
console.error('Worker error:', error); // Log full error details, including stack trace
|
||||
|
||||
// Use the formatError utility to properly format the error response
|
||||
const errorPayload = formatError(error, env);
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ message: 'An internal server error occurred. Please try again later.' }),
|
||||
JSON.stringify(errorPayload),
|
||||
{
|
||||
status: 500,
|
||||
headers: {
|
||||
|
||||
@ -1,13 +1,16 @@
|
||||
export function formatError(error: unknown): Record<string, unknown> {
|
||||
export function formatError(error: unknown, env?: { WORKER_ENV?: string }): Record<string, unknown> {
|
||||
const payload: Record<string, unknown> = {
|
||||
error: 'Internal Server Error',
|
||||
message: error instanceof Error ? error.message : 'Unknown error'
|
||||
};
|
||||
if (
|
||||
typeof process !== 'undefined' &&
|
||||
process.env?.NODE_ENV !== 'production'
|
||||
) {
|
||||
|
||||
// Only include stack trace in development environment
|
||||
// In Cloudflare Workers, check environment via env parameter
|
||||
const isDevelopment = env?.WORKER_ENV !== 'production';
|
||||
|
||||
if (isDevelopment) {
|
||||
payload.stack = error instanceof Error ? error.stack : undefined;
|
||||
}
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
@ -5,19 +5,32 @@ import { formatError } from '../src/utils/error';
|
||||
const originalEnv = process.env.NODE_ENV;
|
||||
|
||||
test('includes stack when not in production', () => {
|
||||
delete process.env.NODE_ENV;
|
||||
const err = new Error('boom');
|
||||
const payload = formatError(err);
|
||||
const payload = formatError(err, { WORKER_ENV: 'development' });
|
||||
assert.ok('stack' in payload);
|
||||
});
|
||||
|
||||
test('omits stack in production', () => {
|
||||
process.env.NODE_ENV = 'production';
|
||||
const err = new Error('boom');
|
||||
const payload = formatError(err);
|
||||
const payload = formatError(err, { WORKER_ENV: 'production' });
|
||||
assert.ok(!('stack' in payload));
|
||||
});
|
||||
|
||||
test('includes message for all environments', () => {
|
||||
const err = new Error('boom');
|
||||
const devPayload = formatError(err, { WORKER_ENV: 'development' });
|
||||
const prodPayload = formatError(err, { WORKER_ENV: 'production' });
|
||||
|
||||
assert.strictEqual(devPayload.message, 'boom');
|
||||
assert.strictEqual(prodPayload.message, 'boom');
|
||||
});
|
||||
|
||||
test('handles non-Error objects', () => {
|
||||
const payload = formatError('string error', { WORKER_ENV: 'development' });
|
||||
assert.strictEqual(payload.message, 'Unknown error');
|
||||
assert.strictEqual(payload.error, 'Internal Server Error');
|
||||
});
|
||||
|
||||
test.after(() => {
|
||||
if (originalEnv === undefined) delete process.env.NODE_ENV; else process.env.NODE_ENV = originalEnv;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user