---
title: "Error handling"
description: "Learn how to handle errors in Elysia, including custom error handling, error codes, and best practices for managing exceptions in your web applications."
url: "https://next.elysiajs.com/tutorial/patterns/error-handling/"
---

# Error handling

[error](/essential/life-cycle/#error-error-handling) is called when an **error is thrown**.

It accepts context similar to a handler, with an `error` property containing the thrown error. Use `instanceof` to check its class.

```typescript
import { Elysia, NotFound } from 'elysia'

new Elysia()
  .onError(({ error, status }) => {
    if(error instanceof NotFound)
      return 'uhe~ are you lost?'

    return status(418, "My bad! But I'm cute so you'll forgive me, right?")
  })
  .get('/', () => 'ok')
  .listen(3000)
```

You can return a [status](/essential/handler/#status) to override the default error status.

## Custom error

Register an error class with `.onError(ErrorClass, handler)` to handle that type:

```typescript
import { Elysia } from 'elysia'

class NicheError extends Error {
  constructor(message: string) {
    super(message)
  }
}

new Elysia()
  .onError(NicheError, ({ error, status }) => {
    // Typed as NicheError
    console.log(error)

    return status(418, "We have no idea how you got here")
  })
  .get('/', () => {
        throw new NicheError('Custom error message')
  })
  .listen(3000)
```

The handler receives an error typed as `NicheError`, so its properties are available with autocomplete.

### Error status code

You can also provide a custom status code by adding a **status** property to class:

```typescript
import { Elysia } from 'elysia'

class NicheError extends Error {
  status = 418

  constructor(message: string) {
    super(message)
  }
}
```

Elysia will use this status code if the error is thrown, see [Custom status code](/patterns/error-handling/#custom-status-code).

### Error response

Return a response from the class handler to choose the body and status:

```typescript
import { Elysia } from 'elysia'

class NicheError extends Error {
  status = 418
}

new Elysia()
  .onError(NicheError, ({ error, status }) =>
    status(error.status, { message: error.message })
  )
  .get('/', () => {
    throw new NicheError('Custom error message')
  })
```

See [Class error handlers](/patterns/error-handling/#third-party-errors).

## Assignment

Let’s try to extend Elysia’s context.
