Getting error when using wp_insert_post()

Just add this code

    $post_id = wp_insert_post($arg);
    if(!is_wp_error($post_id)){
      //the post is valid
    }else{
      //there was an error in the post insertion, 
      echo $post_id->get_error_message();
    }