I know this thread is super old but I figured out a solution that worked for me and I want to help others that may have this problem. It was a sensor error. The hardware test gave me a code for a sensor error, although it didn't give out anything specific. So I did some research and found out that my ir sensor was the culprit. The sensor somehow communicates with a mechanism called the "reed switch" -- it reads the LCD magnets when the clamshell is closing. My laptop kept shutting down because the reed switch or the ir sensor kept giving the signal that the case was down. What I basically did was open up the back of the laptop and I took out the optical drive. Here are the instructions I followed:
MacBook Unibody Model A1278 Upper Case Replacement - iFixit
Step 33 will show you guys the ir sensor. There was some black stuff on the end of my sensor so I basically just cleaned it off with rubbing alcohol. I also saw that the capacitor, or battery, that is connected to the logic board, right above the trackpad, was corroded. It's called the PRam battery. My computer runs off the main battery already so I had no need for the corroded PRam thing. I took it off, too. I also took my ram chips off and put them back in (a tip that a mac technician told me to do). I did this all at once so the solution could've been any one of them. Most likely, however, it was the ir sensor. I should replace it, but I'm going to run what I have right now until I run it into the ground!
Also, if I would've taken this to apple, first they would probably laugh at me for bringing in such an old computer (from 2008), then they would've told me to replace the logic board or top case, and probably charge me 500 for it. My friend works as a "mac genius" and told me to scrap the computer for parts because it would cost too much to fix, and that it was due to a bad graphics card. Some genius he is. So don't take everything you see on these forums as absolute fixes or solutions from real experts. I figured out how to fix my problem in a way that has never been suggested before (the ir sensor issue). For anyone that is looking for a solution out there, just do your research, be patient, and don't panic and spend an arm and a leg for unnecessary parts! Good luck.
Also, taking the keyboard ribbon in and out will only work if the Reed Switch is fidgety. Check the ir sensor first, and if it's fine and clean, then you will have to replace the reed switch - it's embedded into the top case unfortunately

. So you will have to replace it if all else fails.