Error structure

A structure to have code-error pairs.

To keep the same pattern as Nest when throwing an exception, let's have the fields statusCode with the code of the status and error with the name of this status. Let's then create the file common -> util -> http-error.util. Here, we'll define status and error pairs to type the errors in a standard and safe way.

export const HttpError = {
  NOT_FOUND: {
    status: HttpStatus.NOT_FOUND,
    error: 'Not Found',
  },
} as const;

To only allow errors in this format, we can create an interface...

interface IHttpError {
  readonly status: HttpStatus;
  readonly error: string;
}

...to state that the HttpError must follow its contract.

satisfies Record<string, IHttpError>;

Going back to the filter, we can extract these two fields from the aproppriate error.

const { status, error } = HttpError.NOT_FOUND;

The next step is to extract the entity name from the message.

Last updated