Skip to content

Commit bcc41aa

Browse files
Copilotpelikhan
andcommitted
Improve logging examples with safer error handling patterns
- Use optional chaining for apiKey.substring() to handle undefined - Replace type assertion with instanceof check for error handling - Add fallback values for safer logging examples Co-authored-by: pelikhan <[email protected]>
1 parent e6dd11d commit bcc41aa

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

AGENTS.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ This naming pattern makes it easy to filter logs by component when debugging.
158158
log('API key configured: %s', apiKey ? 'yes' : 'no')
159159

160160
// ✅ SAFE: Log a masked or truncated version
161-
log('API key (first 8 chars): %s...', apiKey.substring(0, 8))
161+
log('API key (first 8 chars): %s...', apiKey?.substring(0, 8) || 'not set')
162162

163163
// ❌ UNSAFE: Never log the full secret
164164
// log('API key: %s', apiKey) // DON'T DO THIS!
@@ -193,7 +193,10 @@ export async function myFunction(param: string): Promise<void> {
193193
// ... perform operation ...
194194
log('Operation completed successfully')
195195
} catch (error) {
196-
log('Operation failed: %s', (error as Error).message)
196+
log(
197+
'Operation failed: %s',
198+
error instanceof Error ? error.message : String(error)
199+
)
197200
throw error
198201
}
199202
}

0 commit comments

Comments
 (0)