feat: robust error handling and type safety

Add type-safe error helpers and remove 'as any' casts.

Changes:
- Add isAbortError() helper for checking abort signals
- Add getEventDetail() helper for CustomEvent extraction
- Fix variable naming in isRetryable() (isAborted vs isAbortError)
- Use proper instanceof checks instead of casting
- Add proper interface for normalized response data
- Type-safe tool execution error messages
This commit is contained in:
linked-danis
2026-03-12 14:07:45 +01:00
parent 734ec47d0b
commit f45f9d020e
5 changed files with 79 additions and 22 deletions
+27
View File
@@ -101,3 +101,30 @@ export function assert(condition: unknown, message?: string, silent?: boolean):
throw new Error(errorMessage)
}
}
/**
* Check if an error is an AbortError (from AbortController)
* Handles various forms: Error with name 'AbortError', or rawError property
*/
export function isAbortError(error: unknown): boolean {
if (error instanceof Error && error.name === 'AbortError') return true
if (
typeof error === 'object' &&
error !== null &&
'rawError' in error &&
(error as { rawError?: Error }).rawError?.name === 'AbortError'
)
return true
return false
}
/**
* Safely extract detail from CustomEvent
* @returns The detail object or null if not a CustomEvent
*/
export function getEventDetail<T>(event: Event): T | null {
if (event instanceof CustomEvent) {
return event.detail as T
}
return null
}