Skip to main content

BamlAbortError

Thrown when a BAML function call is cancelled via an abort controller. Indicates intentional cancellation rather than actual failure.

Class Definition​

export class BamlAbortError extends Error {
public readonly name: string = 'BamlAbortError'
public readonly reason?: any
public readonly detailed_message: string

constructor(message: string, reason?: any, detailed_message: string = '') {
super(message)
this.reason = reason
this.detailed_message = detailed_message
}
}

Properties​

PropertyTypeDescription
messagestringWhy the operation was aborted
reasonanyOptional context provided when calling abort()
namestringAlways "BamlAbortError"
detailed_messagestringAdditional debugging details

Detection​

import { BamlAbortError } from '@boundaryml/baml'

try {
const result = await b.FunctionName(input, { signal: controller.signal })
} catch (error) {
if (error instanceof BamlAbortError) {
console.log('Operation was cancelled')
console.log('Reason:', error.reason)
}
}

Common Patterns​

Distinguishing Cancellation Types​

try {
const result = await b.FunctionName(input, { signal: controller.signal })
} catch (error) {
if (error instanceof BamlAbortError) {
if (error.reason === 'user_cancelled') {
showMessage('You cancelled the operation')
} else if (error.reason === 'timeout') {
showMessage('Operation timed out')
} else {
showMessage('Operation was cancelled')
}
}
}

Cleanup After Cancellation​

const controller = new AbortController()
let cleanup = null

try {
cleanup = await setupResources()
const result = await b.FunctionName(input, { signal: controller.signal })
return result
} catch (error) {
if (error instanceof BamlAbortError) {
console.log('Cancelled, cleaning up...')
}
throw error
} finally {
if (cleanup) await cleanup()
}

Don't Retry Cancelled Operations​

// Good: Check error type before retry
if (error instanceof BamlAbortError) {
// Don't retry - intentionally cancelled
return
}

// Bad: Retry everything including cancellations
for (let i = 0; i < 3; i++) {
try {
return await operation()
} catch (error) {
continue // Might retry a cancelled operation!
}
}

Best Practices​

  1. Always handle abort errors explicitly - Don't treat them as generic failures
  2. Provide meaningful cancellation reasons - Use controller.abort('user_cancelled') instead of just controller.abort()
  3. Don't retry cancelled operations - Check error type before retry logic

See Also​