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:
gpt-engineer-app[bot]
2025-11-10 17:03:25 +00:00
parent e0001961bf
commit ade1810a01
6 changed files with 92 additions and 27 deletions

View 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);
}