Check return type of variable and return error if it is wrong