Cannot use object of type WP_Error

What’s $genres? I don’t see it defined anywhere.

And wp_insert_term() may return an error, so make sure to check if it is an error. So instead of simply doing return wp_insert_term($name,$tax)["term_id"], you could do something like this:

$data = wp_insert_term( $name, $tax );
if ( ! is_wp_error( $data ) ) {
    return $data['term_id'];
}