Build and fix error handling in api controller