Files
peardock/test/errorHandler.test.js
T
2026-07-10 20:04:47 -04:00

55 lines
1.7 KiB
JavaScript

import test from 'brittle';
import { AppError, ErrorType, createErrorResponse, sanitizeErrorMessage } from '../utils/errorHandler.js';
test('AppError - creates error with type and code', (t) => {
const error = new AppError('Test error', ErrorType.VALIDATION, 'TEST_CODE');
t.is(error.message, 'Test error');
t.is(error.type, ErrorType.VALIDATION);
t.is(error.code, 'TEST_CODE');
t.ok(error.timestamp);
});
test('AppError - toJSON serialization', (t) => {
const error = new AppError('Test error', ErrorType.NETWORK);
const json = error.toJSON();
t.is(json.error, 'Test error');
t.is(json.type, ErrorType.NETWORK);
t.ok(json.timestamp);
});
test('createErrorResponse - AppError', (t) => {
const error = new AppError('Test error', ErrorType.VALIDATION, 'TEST_CODE');
const response = createErrorResponse(error);
t.is(response.error, 'Test error');
t.is(response.code, 'TEST_CODE');
t.is(response.type, ErrorType.VALIDATION);
});
test('createErrorResponse - standard Error', (t) => {
const error = new Error('Standard error');
error.code = 'ETIMEDOUT';
const response = createErrorResponse(error);
t.is(response.error, 'Standard error');
t.is(response.code, 'ETIMEDOUT');
t.is(response.type, ErrorType.NETWORK);
});
test('sanitizeErrorMessage - removes sensitive info', (t) => {
const message = 'Error accessing /var/run/docker.sock';
const sanitized = sanitizeErrorMessage(message);
t.not(sanitized.includes('/var/run/docker.sock'));
t.ok(sanitized.includes('[REDACTED]'));
});
test('sanitizeErrorMessage - truncates long messages', (t) => {
const longMessage = 'a'.repeat(300);
const sanitized = sanitizeErrorMessage(longMessage);
t.ok(sanitized.length <= 200);
t.ok(sanitized.endsWith('...'));
});