I was trying to import a private key into my wallet, and kept getting a generic error message:
$ bitcoind importprivkey $x mywallet error: {"code":-4,"message":"Error adding key to wallet"} $
I thought maybe the key I was using was in the wrong format, or that my wallet already had the key imported. But it turned out I just needed to supply the wallet passphrase first:
$ bitcoind walletpassphrase "$y" 60 $ bitcoind importprivkey $x mywallet $
It would be better if the error message could be more specific: "Error adding key to locked encrypted wallet", for example.
If I try to add the key again, I get the same error message. This time I'd prefer to see "Key already exists in wallet".