With great caution (as we practiced for our three nights in Morocco), you can likely (no guarantees) avoid a similar situation.
We stayed in a highly rated, NYTimes recommended riad, but even then we were personally careful not to drink water that was not bottled/sealed, no fresh fruit that was not peeled, no salads, and only cooked vegetables. While we normally eats LOTS of fresh fruit and salad type foods, we played it very safe. When we had a quick light meal upon arrival, the bartender brought us complimentary frozen drinks (we thanked him and asked if we could take them to our room since we were so tired, arriving late), once we arrived in our room, down the drain they went (they were made from ice).
Did we need to be that cautious? Maybe not. But, it was what we chose to do.
A day later, we attended a cooking class through the riad (at an additional facility they own outside the medina). WOW. The cleanliness was incredible, and we did not pass on any of the foods (and we had no issues). The chef constantly reminded everyone to not use their knives used for chicken with the vegetables, and often those utensils were collected after use, and even cutting boards were changed during the class. Retrospectively we probably could have enjoyed any foods that had been offered at the riad itself, but we did not know the standards that were used.
As part of an excursion we had arranged ourselves, (as part of a hot air ballon trip, we visited a Berber house and we offered hot tea and local foods they had prepared. We discretely passed on most and enjoyed just the hot-from-the-stone-oven bread. When we entered the restroom facilities at the residence, let's just say that I decided I did not need to use them (pretty gross). I do not know if another sink was used for food, but maybe not...the place was small.
But, that said, there are people who visit Morocco who do not hold back on any foods/drink, and live to tell about it.
We had all advised immunizations and we always travel with Z-packs of antibiotics, a small bottle of prescribed pills for travelers intestinal issues (lucky have not had to use), and a couple of over-the-counter things in small quantities. But, we had been doing a bit of globe trotting those years, and my spouse is a cancer survivor, so we had to be very careful with his immune system (at that time).
But, mention of the market above is so very true....shocking (to us based on our standards) how much meat/fish, etc. sits out in the open without refrigeration. Flies and cats all over vegetables, etc. In Vietnam, we saw a rat run thru the market, and after that particular trip, it was the air pollution that seemed to take its toll on my spouse (in bed for a week with a really bad sinus infection of some sort) on that trip.
But, despite all the differences, we as travelers are richer for the experiences of understanding how much of the world lives. If our power goes out for a few hours after a storm, we realize the issue is definitely a First World Problem.
Take the trip, but really vet the places you stay and use super caution with what you eat. We love cooking classes, because we can enjoy great foods while also seeing how they are cleaned and prepared (along with how the utensils are cleaned).