mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 13:11:12 -05:00
Improve error formatting in tests
- Replace [object Object] errors with readable messages by using robust error formatting across test suites - Introduce formatTestError helper and apply it in all catch blocks and error throws - Update approvalPipelineTests and related suites to utilize improved error extraction for better debugging
This commit is contained in:
59
src/lib/integrationTests/formatTestError.ts
Normal file
59
src/lib/integrationTests/formatTestError.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Test Error Formatting Utility
|
||||
*
|
||||
* Provides robust error formatting for test results to avoid "[object Object]" messages
|
||||
*/
|
||||
|
||||
/**
|
||||
* Format error for test result display
|
||||
* Handles Error objects, PostgresError objects, and plain objects
|
||||
*
|
||||
* @param error - Any error value thrown in a test
|
||||
* @returns Formatted, human-readable error string
|
||||
*/
|
||||
export function formatTestError(error: unknown): string {
|
||||
// Standard Error objects
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
// Object-like errors (PostgresError, custom error objects, etc.)
|
||||
if (typeof error === 'object' && error !== null) {
|
||||
const err = error as any;
|
||||
|
||||
// Try common error message properties
|
||||
if (err.message && typeof err.message === 'string') {
|
||||
return err.message;
|
||||
}
|
||||
|
||||
// Some errors nest the actual error in an 'error' property
|
||||
if (err.error) {
|
||||
return formatTestError(err.error);
|
||||
}
|
||||
|
||||
// Some APIs use 'msg' instead of 'message'
|
||||
if (err.msg && typeof err.msg === 'string') {
|
||||
return err.msg;
|
||||
}
|
||||
|
||||
// Supabase errors might have details
|
||||
if (err.details && typeof err.details === 'string') {
|
||||
return `${err.message || 'Error'}: ${err.details}`;
|
||||
}
|
||||
|
||||
// Last resort: stringify the entire object
|
||||
try {
|
||||
const stringified = JSON.stringify(error, null, 2);
|
||||
// If it's too long, truncate it
|
||||
return stringified.length > 500
|
||||
? stringified.substring(0, 500) + '... (truncated)'
|
||||
: stringified;
|
||||
} catch {
|
||||
// JSON.stringify can fail on circular references
|
||||
return String(error);
|
||||
}
|
||||
}
|
||||
|
||||
// Primitive values (strings, numbers, etc.)
|
||||
return String(error);
|
||||
}
|
||||
Reference in New Issue
Block a user