Openai/691da364-3a14-800b-92eb-1ce91c62cf99


This conversation was exported from ChatGPT[1]

gpt-5-1 edit

User: You are an AI coding assistant working inside an IDE. edit

You are an AI coding assistant working inside an IDE. You have access only to the following retrieved project files:

--- PROJECT FILES --- File: utils/logger.ts export function logError(error: Error) {

 console.error('[ERROR]', error.message);

}

File: services/userService.ts import { logError } from '../utils/logger.ts'; export async function createUser(data) {

 try {
   validate(data)
 } catch (err) {
   logError(err);
   throw err;
 }

} --- END PROJECT FILES ---

YOUR TASK: Fix the issue where logError crashes when err is not an instance of Error. RESPONSE FORMAT: 1. Short explanation (2–4 sentences) 2. A unified diff patch with file paths RULES: - Modify ONLY the files shown. - Do NOT invent new files or functions unless the task requires it. - Follow the patterns and style used in the codebase. - Keep diffs minimal and safe.

Assistant: 1. edit

  1. Explanation

logError expects an Error instance, but callers may throw strings or custom objects. To prevent crashes, safely normalize the value before logging. The fix checks whether the argument is an Error and falls back to stringifying other types.

  1. Patch
--- a/utils/logger.ts
+++ b/utils/logger.ts
 export function logError(error: Error) {
*  console.error('[ERROR]', error.message);
+  const message =
+    error instanceof Error
+      ? error.message
+      : typeof error === 'string'
+        ? error
+        : JSON.stringify(error);
+
+  console.error('[ERROR]', message);
 }

References edit

  1. "Fix logError crash". ChatGPT. Retrieved 2025-11-20.