Consistently return JSON API errors

Also add tests. ;)