Welcome to the club. My Gen3 G27 shoots brass every which direction too, luckily it hasn't malfunctioned though. From what I can tell, Glock didn't machine the slide correctly, the cut-out for the extractor is too far forward toward the muzzle so the extractor isn't holding the spent casing to the breech face. The spent casing instead drops to the level of the magazine before the ejector hits, and when the ejector hits, it pivots right and the case mouth digs into the slide just below the ejection port opening, then the casing pivots upward and the case rim comes out from under the extractor claw early. Then the casing bounces around in the ejection port any number of ways before finally clearing the ejection port or getting caught in it as the slide returns forward.
Glock is addressing the issue in Gen4's with a new ejector which raises the case mouth of the spent casing earlier during ejection. However recent production Gen3's have the same problem and Glock won't install the new ejector into Gen3's. Most people who've had this problem with a Gen3 have had to pay to ship the gun to Glock, Glock sends it back to them shortly saying "Tested, in spec" and the gun wasn't fixed. So not only was the gun not fixed, the customers threw $60+ in the trash. smitty704 returned his Gen3 G19 twice and finally convinced the Warranty Department Manager to replace it with a Gen4 G19 which has the new ejector, his new Gen4 works flawlessly. He and another person originally sent their Gen3 G19's back, Glock replaced the extractor, the guns ejected fine for ~700 rounds then started throwing brass in their faces again.
Glock is slipping these days. I own two Glocks, one of them was made in 2004 and it functions flawlessly. The other, my G27, was made in 2009 and with the way it ejects and mangles the brass I just can't trust it. So far my experience with Glock has been 1 hit/1 miss. I don't think it's coincidence that most of the problems with Glock pistols have been reported only in recent years.
As to why they won't use the new ejector in Gen3's, what I think is that they don't mind fixing the problem in a new product for the sake of the reputation of that product, the Gen4. But they don't want to replace the same part in every existing pistol of previous Generations because it'll cost too much. A lot of the changes Glock has made over the past few years, particularly the cheaper processes used for making extractors, locking blocks, and firing pins, show that they're willing to sacrifice quality for the smallest fraction of a penny in savings. They're not about to replace a part in every gun they've ever made. One person on another forum put the new ejector into a Gen3 trigger housing to use in his Gen3 G34 which has the erratic ejection problem, not only did the new ejector work in the Gen3, it fixed the ejection problem.
The thing is though, they don't need to replace the ejector in every Glock ever made. Most Glocks work fine with the old ejector, it's only the ones with slides that weren't machined correctly that need the new ejector. Maybe Glock doesn't want to admit by selectively replacing the ejector in such a fashion that their quality control and manufacturing tolerances aren't what customers would expect from a pistol you're supposed to trust your life with.