Repository: logto-io/logto
File: packages/core/src/middleware/koa-slonik-error-handler.ts (TypeScript)

Code before:
      }

      if (error instanceof InvalidInputError) {
        throw new RequestError({
          code: 'entity.invalid_input',
          status: 422,
        });
      }

      if (error instanceof UniqueIntegrityConstraintViolationError) {
        if (error.constraint === 'applications__protected_app_metadata_host') {
          throw new RequestError({
            code: 'application.protected_application_subdomain_exists',
            status: 422,
          });
        }

        if (error.constraint === 'applications__protected_app_metadata_custom_domain') {
          throw new RequestError({
            code: 'domain.hostname_already_exists',
            status: 422,
          });
        }

        if (error.constraint === 'application_secrets_pkey') {
          throw new RequestError({
            code: 'application.secret_name_exists',
            status: 422,
          });
        }

        if (error.constraint === 'personal_access_tokens_pkey') {
          throw new RequestError({
            code: 'user.personal_access_token_name_exists',
            status: 422,
          });
        }

        if (error.constraint === 'users__username') {
          throw new RequestError({
            code: 'user.username_already_in_use',
            status: 422,
          });
        }

        if (error.constraint === 'users__primary_email') {
          throw new RequestError({
            code: 'user.email_already_in_use',
            status: 422,
          });
        }

Review comment (suggestion):
no need for todo here as it triggers the eslint warning (better to reduce the number of warnings)

```suggestion
```

Code after:
export default function koaSlonikErrorHandler<StateT, ContextT>(): Middleware<StateT, ContextT> {
  return async (ctx, next) => {
    try {
      await next();
    } catch (error: unknown) {
      if (!(error instanceof SlonikError) && !(error instanceof DatabaseError)) {
        throw error;
      }

      if (error instanceof DatabaseError) {
        // https://www.postgresql.org/docs/14/errcodes-appendix.html
        // Can better infer SlonikError type.
        // eslint-disable-next-line unicorn/no-lonely-if
        if (error.code === '22001') {
          throw new RequestError(
            {
              code: 'entity.value_too_long',
              status: 422,
            },
            {
              message: error.message,
            }
          );
        }

        // TODO: add more DatabaseError code handling if needed.
      }

      if (error instanceof InvalidInputError) {
        throw new RequestError({
          code: 'entity.invalid_input',
          status: 422,
        });
      }

      if (error instanceof UniqueIntegrityConstraintViolationError) {
        if (error.constraint === 'applications__protected_app_metadata_host') {
          throw new RequestError({
            code: 'application.protected_application_subdomain_exists',
            status: 422,
          });
        }

        if (error.constraint === 'applications__protected_app_metadata_custom_domain') {
          throw new RequestError({
            code: 'domain.hostname_already_exists',
            status: 422,
          });
        }

        if (error.constraint === 'application_secrets_pkey') {