Error Handling

intermediate6 of 8

Learn about error handling in fp-ts either

Code Editor

06-error-handling.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('purchaseProduct', () => {
  const products = [
    { id: 1, name: 'Laptop', price: 1000, stock: 5 },
    { id: 2, name: 'Mouse', price: 25, stock: 0 },
    { id: 3, name: 'Keyboard', price: 75, stock: 10 },
  ]

  it('successfully purchases product', () => {
    const result = purchaseProduct(1, 1500, products)
    expect(result).toEqual({
      _tag: 'Right',
      right: { product: products[0], remainingBalance: 500 }
    })
  })

  it('returns error for non-existent product', () => {
    const result = purchaseProduct(99, 1000, products)
    expect(result).toEqual({ _tag: 'Left', left: 'Product not found' })
  })

  it('returns error for out of stock product', () => {
    const result = purchaseProduct(2, 1000, products)
    expect(result).toEqual({ _tag: 'Left', left: 'Out of stock' })
  })

  it('returns error for insufficient funds', () => {
    const result = purchaseProduct(1, 500, products)
    expect(result).toEqual({ _tag: 'Left', left: 'Insufficient funds' })
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises