---
description: First steps before getting into the core logic.
---

# Filter basic structure

Let's create it in the <mark style="color:purple;">database</mark> folder, for better semantics and organization.

```sh
nest g f database/exception-filters/not-found-exception
```

We now have a basic structure to begin with. The first thing to be noticed is the <mark style="color:blue;">`@Catch()`</mark> decorator over the filter. Here, we define what types of error it must catch in order to handle them. In our case, we want to catch the <mark style="color:blue;">`EntityNotFoundError`</mark>, so put it inside the parentheses. We shall also remove the generic as we won't be needing it, and set the type of the <mark style="color:blue;">`exception`</mark> parameter as this error. We should have the following result:

```typescript
@Catch(EntityNotFoundError)
export class NotFoundExceptionFilter implements ExceptionFilter {
  catch(exception: EntityNotFoundError, host: ArgumentsHost) {}
}
```

The <mark style="color:blue;">`catch()`</mark> method is invoked when the error/exception is catched. it has as parameters the catched <mark style="color:blue;">`exception`</mark> and the <mark style="color:blue;">`host`</mark>, which allows for accessing the <mark style="color:blue;">`response`</mark> to be sent. In here, we'll return a <mark style="color:blue;">`response`</mark> with the <mark style="color:blue;">`status`</mark> **NotFound** and an appropriate <mark style="color:blue;">`message`</mark>.

The first step is to obtain the <mark style="color:blue;">`response`</mark> from the <mark style="color:blue;">`host`</mark>.

```typescript
const response = host.switchToHttp().getResponse<Response>();
```

{% hint style="info" %}
The <mark style="color:blue;">`Response`</mark> type is from <mark style="color:blue;">`express`</mark>.
{% endhint %}

Then, we'll create a standard error structure to have errors similar to Nest.
