How to get error object returned by wp_create_user

In your example code, $user_id will be the error object. You can test if is_wp_error and return the error string via get_error_message:

$user_id = wp_create_user( $user_name, $random_password, $user_email );
if ( is_wp_error( $user_id ) )
   return $user_id->get_error_message();