Error catch in search box

I have built an search box for a website to allow vistors to check their postcode to see if they can receive a delivery, all works fine except for when a visitor creates an error in the field (I have added a character limit and validation format for the field) to limit the errors

The field shows if an error has been created but I can’t figure out how to catch this error in the code to stop the default ‘Sorry’ message being displayed, I would like it to display the ‘Error’ message I have created.

the code is:

No-one will re-write all this code. When you show some CODE, then do it please in CODE-TAGS!

Thanks, sorry new to this!

import wixData from "wix-data"; 

export function PostcodeCheck_click(event) {
 let postcode = $w('#PostcodeSearch').value;
    wixData.query("Postcodes")
    .contains('title', postcode)
    .find()
    .then((results) => {
if (results.items.length > 0) { 
                $w('#PostcodeResults').expand();
                $w('#DeliveryOptions').show();
                $w('#PostcodeCheck').hide();
                $w('#Reset').show();

        }
if (results.items.length === 0) {
                $w('#PostcodeResults').expand();
                $w('#SorryBox').show();                
                $w('#Sorry').show();
                $w('#GetDirections').show();
                $w('#Reset').show();
            }
})
 
.catch( (err) => {
    console.log(err);
    $w('#Error').show();
    $w('#Reset').show();
                });

    filter($w('#PostcodeSearch').value);
}

function filter(title) {
    $w('#dataset1').setFilter(wixData.filter().contains('title', title));
}

export function Reset_click(event, $w) {
    $w('#PostcodeSearch').value = "";
    $w('#PostcodeSearch').resetValidityIndication();
    $w('#Reset').hide();
    $w('#PostcodeCheck').show();
    $w('#PostcodeResults').collapse();
}

but I can’t figure out how to catch this error in the code to stop the default ‘Sorry’ message being displayed, I would like it to display the ‘Error’ message I have created.
What do you mean, when you say—> default ‘Sorry’
What is the default sorry-message in your code?
This…?

.catch( (err) => {
    console.log(err);
    $w('#Error').show();
    $w('#Reset').show();
                });

or this…?

if (results.items.length === 0) {
                $w('#PostcodeResults').expand();
                $w('#SorryBox').show();                
                $w('#Sorry').show();
                $w('#GetDirections').show();
                $w('#Reset').show();
            }

Or is there a third option in the game?

The ‘Sorry’ message it this

if (results.items.length === 0) {
                $w('#PostcodeResults').expand();
                $w('#SorryBox').show();                
                $w('#Sorry').show();
                $w('#GetDirections').show();
                $w('#Reset').show();
            }

Which is displayed if there is no match in the database.

The error message is if they enter the wrong format in to the box and it doesn’t match the validation format, for example by adding in extra spaces, which I think is what happening from the feedback I have had from the client.

So that means, you don’t want to show your own message ( ‘Sorry’-message), when a validation-error occurs?
So then you have to look, what is the result-error-message in this case (when a validation-error occurs), whats the OUTPUT in CONSOLLE?

undefined ?
or something else?

Then you optimize your own error-code…

if (results.items.length === 0 && !==undefined) {
                $w('#PostcodeResults').expand();
                $w('#SorryBox').show();                
                $w('#Sorry').show();
                $w('#GetDirections').show();
                $w('#Reset').show();
            }