Abstract API error checking to own function