Mid 2012 MBP shuts down claiming no power with charge at 30% or 40% or more
Your battery is shot.

A battery's life is determined more by age than the number of cycles.  Yes, number of cycles do play a part, but the biggest culprit is age.  In short, you have a 4 year old battery.  A 4 year old battery that was never used is going to perform about as well as a 4 year old battery with 500 cycles.

MATH FINDS TOP CULPRIT AS LITHIUM-ION BATTERIES AGE

As far as what you are seeing, this is not an abnormal symptom.  Your MacBook Pro has no sensor to be able to determine how quickly a charge will dissipate within the cell.  All it can do is measure the voltage under load and calculate how much charge is left.  

As your battery ages, there is a build up of the solid electrolyte interphase (SEI).  This layer is what "transfers" the electrical charge from the chemical reaction.  As this layer builds, the performance degrades.  This layer continues to build from the time the battery is manufactured until the time it is recycled.  There's no stopping it.

In short,  your MBP is calculating that you have 30-40% of battery left; it's only a calculation as it cannot account for or measure the SEI layer buildup.

Just to provide you a reference, I am working on a Dell Precision M4300 laptop (installing FreeBSD).  The battery reports 100% and healthy.  If I unplug, I get about 30 seconds before the battery dissipates and the laptop turns off.  So, I have the same symptoms you do, except it's happening at 100% charge.  This laptop and battery are 8 years old so the behavior is not unexpected.  What would be unexpected is if the battery held any charge at this point.

The Good News

It's pretty easy to fix.  iFixit.com has an excellent guide:  Battery and Trackpad. Disassembling MacBook Pro 15" Retina 2012-2015 and the prices are relatively low (less than $45 on Amazon). 

Disclaimer:  I have personally used two of these exact model batteries on MBP's and both are still running strong.