api: return 404 if a resource is not found

This also checks for the error message that is returned, and fixes a
problem with the error encoding middleware that was doing double JSON
encoding.

Change-Id: Ieb39a991ddc9ecba0a7e71450a1e57ede18ccbe6
Fixes-Bug: #1218760
Fixes-Bug: #1208552
Merge "api: return 404 if a resource is not found"