---
description: The error data depends on the error code provided by the database.
---

# Creating error data

We should identify the <mark style="color:blue;">`code`</mark> given by Postgres to set an appropriate <mark style="color:blue;">`httpError`</mark> and also a <mark style="color:blue;">`description`</mark> of the situation. Let's return to the <mark style="color:blue;">`HttpError`</mark> to add one more possible error.

```typescript
CONFLICT: {
  status: HttpStatus.CONFLICT,
  error: 'Conflict',
},
```

Back in the filter, let's create some constants to help us with the specific error <mark style="color:blue;">`codes`</mark> and corresponding <mark style="color:blue;">`descriptions`</mark>. Also, there is one postgres error <mark style="color:blue;">`code`</mark> that can represent two of the three cases we're handling. Therefore, when it appears, we'll have to check the error <mark style="color:blue;">`message`</mark> itself in order to understand what's the case.

```typescript
private readonly DatabaseErrorCode = {
  ASSOCIATION_NOT_FOUND_OR_NOT_NULL_VIOLATION: '23503',
  UNIQUE_VIOLATION: '23505',
} as const satisfies Record<string, string>;

private readonly MessageSnippet = {
  ASSOCIATION_NOT_FOUND: 'is not present',
  NOT_NULL_VIOLATION: 'is still referenced',
} as const satisfies Record<string, string>;

private readonly Description = {
  ASSOCIATION_NOT_FOUND: 'Associated entity not found',
  NOT_NULL_VIOLATION: 'Cannot delete due to NOT NULL constraint',
  UNIQUE_VIOLATION: 'Unique constraint violation',
} as const satisfies Record<string, string>;
```

The next step is to create a method that will receive the error <mark style="color:blue;">`code`</mark> and the <mark style="color:blue;">`message`</mark>, and then return an <mark style="color:blue;">`httpError`</mark> and a <mark style="color:blue;">`description`</mark>. It may seem like a lot of code but this is simply a <mark style="color:blue;">switch</mark>, nothing too complicated.  An inner switch is used to check the message's contents, if necessary. We don't use the <mark style="color:blue;">default</mark> because, if an unspecified case occurs, we want the <mark style="color:blue;">`httpError`</mark> to be <mark style="color:blue;">undefined</mark>. We'll use this to know the <mark style="color:blue;">`BaseExceptionFilter`</mark> should be invoked.

```typescript
private createErrorData(code: string, message: string) {
  let httpError: any;
  let description: string;

  switch (code) {
      case this.DatabaseErrorCode.ASSOCIATION_NOT_FOUND_OR_NOT_NULL_VIOLATION:
        switch (true) {
          case message.includes(this.MessageSnippet.ASSOCIATION_NOT_FOUND):
            httpError = HttpError.NOT_FOUND;
            description = this.Description.ASSOCIATION_NOT_FOUND;
            break;

          case message.includes(this.MessageSnippet.NOT_NULL_VIOLATION):
            httpError = HttpError.CONFLICT;
            description = this.Description.NOT_NULL_VIOLATION;
            break;
        }
        break;

      case this.DatabaseErrorCode.UNIQUE_VIOLATION:
        httpError = HttpError.CONFLICT;
        description = this.Description.UNIQUE_VIOLATION;
        break;
    }

  return { httpError, description };
}
```

You may have noticed something unpleasant: the <mark style="color:blue;">`httpError`</mark> has the type <mark style="color:red;">any</mark>. There is an interesting way to have a better type here. Back in the <mark style="color:purple;">http-error.util</mark> file, a type may be created to represent one of the fields of <mark style="color:blue;">`HttpError`</mark> like this:

```typescript
export type HttpError = (typeof HttpError)[keyof typeof HttpError];
```

Now, to explain the code above: first, we use <mark style="color:blue;">`typeof HttpError`</mark> to represent its type. But, as we want a type for one of its fields, instead of itself, we use square brackets to indicate we want the type of a field. Well, we don't want a specific field, but any field. To achieve this, we use <mark style="color:blue;">`keyof`</mark> in combination with <mark style="color:blue;">`typeof HttpError`</mark> once again, which results in the union of all the fields' names of the <mark style="color:blue;">`HttpError`</mark>.

This type can now be applied. Even though the constant and the type have the same name, the IDE can distinguish when one or the other is being used.

```typescript
let httpError: HttpError;
```

Finally, we just need to perform last steps, mostly known to us.
