Answers.Body: <p>TL;DR: Warnings are signs that something's not quite right, but are also <em>usually</em> ignorable.</p>
  <p>Here it looks to me like any of the following might possibly be happening:</p>
  <ul>
  <li><p>There's a bug in core or in an extension or some custom code that's generating an error described by an array, where it should be using a string. It looks like the function <code>CRM_Core_Error::isAPIError()</code> allows the error to be either a string, or a special case for receiving errors from the API where the error var must be an array with a key of <code>'code'</code> which is missing in your case, hence the error.</p>
  </li>
  <li><p>There is some debugging code in place which is generating output when it shouldn't (sometimes in development people might resort to &quot;print 'what is going on here, then? Here is some data to help me figure it out...'&quot; type code - this should never end up on production sites.</p>
  </li>
  <li><p>You have XDEBUG enabled and it is the version that contains a known bug whereby it outputs <em>something</em> such that the web server thinks that the headers are sent already, so new headers can't then be added.</p>
  </li>
  </ul>
  <p>This probably can't be debugged in a public forum, without the actual data and environment. You could experiment with different data sets to see if it <em>always</em> happens - e.g. try importing a single row of clean-looking data, does that generate the error? If so, try importing a reduced number of columns, does that still generate the error? This way you can find if there's some specific thing that's causing the problem, which will help pinpoint the real issue.</p>
  <p>If you have the requisite skills (or, failing that, a maveric attitude), you could try editing your <code>civicrm/CRM/Core/Error.php</code> file around line 1017 as follows...</p>
  <p>I assume it currently looks like:</p>
  <pre><code>public static function isAPIError($error, $type = CRM_Core_Error::FATAL_ERROR) {
    if (is_array($error) &amp;&amp; !empty($error['is_error'])) {                         
      $code = $error['error_message']['code'];                                    
      if ($code == $type) {                                                       
        return TRUE;                                                              
      }                                                                           
    }                                                                             
    return FALSE;                                                                 
  }                                                                               
  </code></pre>
  <p>You could replace that with</p>
  <pre><code>public static function isAPIError($error, $type = CRM_Core_Error::FATAL_ERROR) {
    if (is_array($error) &amp;&amp; !empty($error['is_error'])) {                         
      $code = $error['error_message']['code'] ?? NULL;
      if ($code === NULL) {
        try { throw new \Exception(); }                                                                                
        catch (\Exception $e) {                                                                                        
          Civi::log()-&gt;error(&quot;Caught invalid error parameter: &quot; . json_encode($error) . &quot;\n&quot; . $e-&gt;getTraceAsString());
        }                                                                                                              
        return FALSE;                                                                                                  
      }
      if ($code == $type) {                                                       
        return TRUE;                                                              
      }                                                                           
    }                                                                             
    return FALSE;                                                                 
  }                                                                               
  </code></pre>
  <p>This will mean</p>
  <ol>
  <li>You won't see the error messages any more. (at least not the first one)</li>
  <li>Where the error messages would have been seen at the browser, they will now be in your CiviCRM log file, which will show exactly what the offending data was, and possibly which code is causing it. This information could be passed to a developer for further investigation.</li>
  </ol>
  <p>Do note though, that editing code files is <strong>risky</strong> - can completely crash your site - so you <strong>absolutely must make a backup of the original code file before changing it</strong> and ideally you'd do this on an offline development version; ideally you'd be a developer :-)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-04T09:08:41.793Z
Answers.LastActivityDate: 2021-10-04T09:21:43.773Z
Answers.LastEditDate: 2021-10-04T09:21:43.773Z
Answers.UserDisplayName: artfulrobot
Body: <p>After importing a CSV of contacts, I'm getting a blank screen with the error message below.  Typically, I get a report of how many contacts were skipped due to duplicates.  It appears the data is being imported though this makes me nervous to import a larger data set.  Any ideas whats happening?</p>
  <p>Warning: Illegal string offset 'code' in /home/DOMAINNAME/www/www/wp-content/plugins/civicrm/civicrm/CRM/Core/Error.php on line 1019</p>
  <p>Warning: Cannot modify header information - headers already sent by (output started at /home/DOMAINNAME/www/www/wp-content/plugins/civicrm/civicrm/CRM/Core/Error.php:1019) in /home/DOMAINNAME/www/www/wp-content/plugins/civicrm/civicrm/CRM/Utils/System/Base.php on line 987</p>
  
Tags: <import><php><data-import>
Title: Error when importing contacts - "Warning: Illegal string offset 'code'"
ThreadText: # Error when importing contacts - "Warning: Illegal string offset 'code'"
  
  (Asked by: username_0 on 2021-10-02)
  
  After importing a CSV of contacts, I'm getting a blank screen with the error message below. Typically, I get a report of how many contacts were skipped due to duplicates. It appears the data is being imported though this makes me nervous to import a larger data set. Any ideas whats happening?
  
  Warning: Illegal string offset 'code' in /home/DOMAINNAME/www/www/wp-content/plugins/civicrm/civicrm/CRM/Core/Error.php on line 1019
  
  Warning: Cannot modify header information - headers already sent by (output started at /home/DOMAINNAME/www/www/wp-content/plugins/civicrm/civicrm/CRM/Core/Error.php:1019) in /home/DOMAINNAME/www/www/wp-content/plugins/civicrm/civicrm/CRM/Utils/System/Base.php on line 987
  
  **Tags:** import, php, data-import
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-10-04. Score: 0
  
  TL;DR: Warnings are signs that something's not quite right, but are also *usually* ignorable.
  
  Here it looks to me like any of the following might possibly be happening:
  
  * There's a bug in core or in an extension or some custom code that's generating an error described by an array, where it should be using a string. It looks like the function `CRM_Core_Error::isAPIError()` allows the error to be either a string, or a special case for receiving errors from the API where the error var must be an array with a key of `'code'` which is missing in your case, hence the error.
  * There is some debugging code in place which is generating output when it shouldn't (sometimes in development people might resort to "print 'what is going on here, then? Here is some data to help me figure it out...'" type code - this should never end up on production sites.
  * You have XDEBUG enabled and it is the version that contains a known bug whereby it outputs *something* such that the web server thinks that the headers are sent already, so new headers can't then be added.
  
  This probably can't be debugged in a public forum, without the actual data and environment. You could experiment with different data sets to see if it *always* happens - e.g. try importing a single row of clean-looking data, does that generate the error? If so, try importing a reduced number of columns, does that still generate the error? This way you can find if there's some specific thing that's causing the problem, which will help pinpoint the real issue.
  
  If you have the requisite skills (or, failing that, a maveric attitude), you could try editing your `civicrm/CRM/Core/Error.php` file around line 1017 as follows...
  
  I assume it currently looks like:
  
  ```
  public static function isAPIError($error, $type = CRM_Core_Error::FATAL_ERROR) {
    if (is_array($error) && !empty($error['is_error'])) {                         
      $code = $error['error_message']['code'];                                    
      if ($code == $type) {                                                       
        return TRUE;                                                              
      }                                                                           
    }                                                                             
    return FALSE;                                                                 
  }                                                                               
  
  ```
  
  You could replace that with
  
  ```
  public static function isAPIError($error, $type = CRM_Core_Error::FATAL_ERROR) {
    if (is_array($error) && !empty($error['is_error'])) {                         
      $code = $error['error_message']['code'] ?? NULL;
      if ($code === NULL) {
        try { throw new \Exception(); }                                                                                
        catch (\Exception $e) {                                                                                        
          Civi::log()->error("Caught invalid error parameter: " . json_encode($error) . "\n" . $e->getTraceAsString());
        }                                                                                                              
        return FALSE;                                                                                                  
      }
      if ($code == $type) {                                                       
        return TRUE;                                                              
      }                                                                           
    }                                                                             
    return FALSE;                                                                 
  }                                                                               
  
  ```
  
  This will mean
  
  1. You won't see the error messages any more. (at least not the first one)
  2. Where the error messages would have been seen at the browser, they will now be in your CiviCRM log file, which will show exactly what the offending data was, and possibly which code is causing it. This information could be passed to a developer for further investigation.
  
  Do note though, that editing code files is **risky** - can completely crash your site - so you **absolutely must make a backup of the original code file before changing it** and ideally you'd do this on an offline development version; ideally you'd be a developer :-)
  
  
  ---
  
  