---
description: The process already performed in the previous filter.
---

# Basic setup

There will be a difference in the "signature" of this exception filter. Instead of implementing the <mark style="color:blue;">`ExceptionFilter`</mark> interface, it will extend the <mark style="color:blue;">`BaseExceptionFilter`</mark> class. We do this because we'll handle only those cases already mentioned, and if another type of database error occurs, it will be thrown back to the <mark style="color:blue;">`BaseExceptionFilter`</mark>, and we'll then get the default behavior again.

And, as the error to be catched is the <mark style="color:blue;">`QueryFailedError`</mark>, this will be the filter's signature:

```typescript
@Catch(QueryFailedError)
export class DatabaseExceptionFilter extends BaseExceptionFilter {
  catch(exception: QueryFailedError, host: ArgumentsHost) {}
}
```

However, before starting its development, we need to make another change in this signature. The <mark style="color:blue;">`QueryFailedError`</mark> type is too generic and does not contain the fields to be extracted. We'll have to get three fields from the postgres error, they are:

* <mark style="color:blue;">`code`</mark> - The error code from Postgres
* <mark style="color:blue;">`detail`</mark> - Similar to an error message
* <mark style="color:blue;">`table`</mark> - The database table where the error occurred

So, to avoid using the type <mark style="color:red;">any</mark>, which obviously should be avoided whenever possible, let's create an interface to represent these fields. Do so in the file <mark style="color:purple;">database</mark>/<mark style="color:purple;">interfaces</mark>/<mark style="color:purple;">database-error.interface</mark>.

```typescript
export interface DatabaseError {
  readonly code: string;
  readonly detail: string;
  readonly table: string;
}
```

Now, we can state that the <mark style="color:blue;">`exception`</mark> catched by this filter has the type of this interface.

```typescript
exception: DatabaseError
```

The first step is identical to the previous filter. That is, to obtain a reference to the <mark style="color:blue;">`response`</mark>.

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

After that, extract the fields from the <mark style="color:blue;">`exception`</mark>.

```typescript
const { code, detail, table } = exception;
```

The next situation to solve is similar to what we went through in the previous filter: extract the data from the <mark style="color:blue;">`message`</mark>. We want the <mark style="color:blue;">`name`</mark> and the <mark style="color:blue;">`value`</mark> of the field that caused the error. They appear in the <mark style="color:blue;">`message`</mark> in the following format: <mark style="color:blue;">**Key (name)=(value)**</mark>. We can then use these regexes:

```typescript
private readonly FIELD_NAME_REGEX = /(?<=Key \()\w+/;
private readonly FIELD_VALUE_REGEX = /(?<=\)=\().*?(?=\))/;
```

Then, the method to extract the data from the <mark style="color:blue;">`message`</mark>.

```typescript
private extractMessageData(message: string) {
  const fieldName = extractFromText(message, this.FIELD_NAME_REGEX);
  const fieldValue = extractFromText(message, this.FIELD_VALUE_REGEX);
  return { fieldName, fieldValue };
}
```

Now, we should use the <mark style="color:blue;">`code`</mark> provided by Postgres to create appropriate error data.
