JS Prisma register user in database, crashing when username fails unique constraint in unwanted ways – not using error function designed

When creating a new user. I am testing the error functions. Creating a user works fine.
I am testing to see what happens when i register as a new user with a username already in use.
Instead of using my (!createdUser) function that sends an error message to the user.
The code skips and goes right to the error section of my try/catch.
Returning a 500 error. However this error completely crashes my database connection. The error log i have claim show it write the logs but they dont get added to the database either.

It is supposed to return a message to the user. Also now i think about it, is there a way i can have my response say username is already in user, to the user that is.

  console.log('Registering new user');

  const {
    email,
    password,
    username,
    firstName,
    lastName,
    city,
    country,
    gender,
    dob,
    profileImage,
    bio,
    agreedToTerms,
  } = req.body;

  const lowerCaseEmail = email.toLowerCase();
  const lowerCaseUsername = username.toLowerCase();
  const lowerCaseFirstName = firstName.toLowerCase();
  const lowerCaseLastName = lastName.toLowerCase();
  const lowerCaseCity = city.toLowerCase();
  const lowerCaseCountry = country.toLowerCase();

  try {
    if (
      !lowerCaseEmail ||
      !password ||
      !lowerCaseUsername ||
      !lowerCaseFirstName ||
      !lowerCaseLastName ||
      !lowerCaseCity ||
      !gender ||
      !dob ||
      !lowerCaseCountry ||
      !agreedToTerms
    ) {
      //
      const missingField = new MissingFieldEvent(
        null,
        'Registration: Missing Field/s event'
      );
      myEmitterErrors.emit('error', missingField);
      return sendMessageResponse(res, missingField.code, missingField.message);
    }

    const foundUser = await findUserByEmail(lowerCaseEmail);
    if (foundUser) {
      return sendDataResponse(res, 400, { email: EVENT_MESSAGES.emailInUse });
    }

    const hashedPassword = await bcrypt.hash(password, hashRate);

    const createdUser = await createUser(
      lowerCaseEmail,
      hashedPassword,
      lowerCaseFirstName,
      lowerCaseLastName,
      lowerCaseCountry,
      lowerCaseCity,
      lowerCaseUsername,
      gender,
      dob,
      agreedToTerms,
      profileImage,
      bio,
    );

      console.log('ZZZZZZ')
      
    if (!createdUser) {
      console.log('AAAA')
      const notCreated = new BadRequestEvent(
        req.user,
        EVENT_MESSAGES.badRequest,
        EVENT_MESSAGES.createUserFail
      );
      myEmitterErrors.emit('error', notCreated);
      return sendMessageResponse(res, notCreated.code, notCreated.message);
    }

    console.log('XX');
  } catch (err) {
    // Error
    const serverError = new RegistrationServerErrorEvent(
      `Register user Server error`
    );
    myEmitterErrors.emit('error', serverError);
    sendMessageResponse(res, serverError.code, serverError.message);
    throw err;
  }
};


// My reg user function in primsa
export const createUser = (
  lowerCaseEmail,
  hashedPassword,
  lowerCaseFirstName,
  lowerCaseLastName,
  lowerCaseCountry,
  lowerCaseCity,
  lowerCaseUsername,
  gender,
  dob,
  agreedToTerms,
  profileImage,
  bio
) =>
  dbClient.user.create({
    data: {
      email: lowerCaseEmail,
      password: hashedPassword,
      agreedToTerms: agreedToTerms,
      profile: {
        create: {
          firstName: lowerCaseFirstName,
          lastName: lowerCaseLastName,
          country: lowerCaseCountry,
          city: lowerCaseCity,
          gender: gender,
          dob: dob,
          profileImage: profileImage,
          bio: bio,
          username: lowerCaseUsername,
        }
      }
    },
  });

my terminal says this

Registering new user
ERROR EVENT RegistrationServerErrorEvent {
  user: 'Failed to register',
  topic: undefined,
  code: 500,
  message: 'Internal Server Error'
}
TTTTT RegistrationServerErrorEvent {
  user: 'Failed to register',
  topic: undefined,
  code: 500,
  message: 'Internal Server Error'
}
POST /users/register 500 1220.477 ms - 52
PrismaClientKnownRequestError:
Invalid `prisma.user.create()` invocation:


Unique constraint failed on the fields: (`username`)
    at pn.handleRequestError (C:UsersTomDocumentscodejavascriptworlds-smartest-frontendservernode_modules@prismaclientruntimelibrary.js:176:6477)
    at pn.handleAndLogRequestError (C:UsersTomDocumentscodejavascriptworlds-smartest-frontendservernode_modules@prismaclientruntimelibrary.js:176:5907)       
    at pn.request (C:UsersTomDocumentscodejavascriptworlds-smartest-frontendservernode_modules@prismaclientruntimelibrary.js:176:5786)
    at async t._request (C:UsersTomDocumentscodejavascriptworlds-smartest-frontendservernode_modules@prismaclientruntimelibrary.js:179:10484)
    at async registerNewUser (file:///C:/Users/Tom/Documents/code/javascript/worlds-smartest-frontend/server/src/controllers/users.js:168:25) {
  code: 'P2002',
  clientVersion: '4.13.0',
  meta: { target: [ 'username' ] }

So it doesnt want to finish my error even tho it prints out. But it wont follow my !created function

Im not sure what to do. I have not encounter this issue before.