Make errors caught in run() more expressive

Created on 18 March 2024, 8 months ago
Updated 22 March 2024, 8 months ago

Problem/Motivation

Due to the try catch in run() the error being bubbled up is just short version of the original error so it makes it hard to see what and where

Call to a member function appendLog() on null

The uncaught error would tell you the file and line number of this error.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇺🇸United States swirt Florida

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @swirt
  • Assigned to abhishek_virasat
  • 🇮🇳India abhishek_virasat

    @swirt, Can you please provide more details about error. in which file error is directing ?

  • 🇺🇸United States swirt Florida

    Thanks for picking this up @abhishek_gupta1

    There errors are caught in the run() function here.

    I just now added a test script that intentionally has an error in it to the dev branch. So if you pull down the latest dev you should be able to run

      drush codit-batch-operations:run TestDo10ThingsWithError
    

    Then you should see output that looks like

    Processed item_4. 4/10.
    Processed item_5. 5/10.
    Codit Batch Operations:  Call to undefined function Drupal\codit_batch_operations\cbo_scripts\someFunctionThatDontExist()
    

    Notice the error has no information about where the error occurred.

    If I was not using a try - catch we would get output like this

    Error: Call to undefined function Drupal\codit_batch_operations\someFunctionThatDontExist() in Drupal\codit_batch_operations\cbo_scripts/TestDo10ThingsWithError->processOne() (line 59 of /var/www/html/docroot/modules/contrib/codit_batch_operations/src/cbo_scripts/TestDo10ThingsWithError.php)
    

    That is because the uncaught output is the error AND the stack trace. I don't really want the entire stack trace, just he first entry, which is the most informative.

    We should also see the improved error reporting when running

      drush codit-batch-operations:run TestDo10ThingsWithError  --allow-skip
    
  • Issue was unassigned.
  • 🇺🇸United States swirt Florida

    I think you might be able to use https://www.php.net/manual/en/exception.gettraceasstring.php
    but it might be hard just to pick out the first part of the string.

    You might need to use https://www.php.net/manual/en/exception.gettrace.php where each step is a separate element in the array so you could grab the first one, then concatenate out the useful parts.

    $trace = $th->getTrace()
    
    $trace[0] would look something like
     {
        ["file"]=>"/modules/contrib/codit_batch_operations/src/cbo_scripts/TestDo10ThingsWithError.php"
        ["line"]=> 59
        ["function"]=> "someFunctionThatDontExist"
        ["args"]=>   { the args for someFunctionThatDontExist which in this case is empty, but could be useful in other examples}
    

    We'd probably want to concat them together something like
    on line
    in file . arguments: print__r(, TRUE)

    Take a look when you get into it. We may want to include the the second level of the trace if it gives useful information.

  • 🇺🇸United States swirt Florida

    Too much? That's OK @abhishek_gupta1. I appreciate you taking a look.

    • swirt committed 2eabe8c1 on 1.0.x
      Issue #3431218 by swirt: Make errors caught in run() more expressive
      
  • Status changed to Fixed 8 months ago
  • 🇺🇸United States swirt Florida

    This is fixed and will go out with beta1

    Errors now look like this with --allow-skip

    Skipped item_6 => simple stuff
    item_6: Error: 0 Call to undefined function Drupal\codit_batch_operations\cbo_scripts\someFunctionThatDontExist()
    - Line 59 of /var/www/html/docroot/modules/contrib/codit_batch_operations/src/cbo_scripts/TestDo10ThingsWithError.php
    Skipped item_8 => simple stuff
    item_8: Error: 0 Call to undefined function Drupal\codit_batch_operations\cbo_scripts\someOtherMistake()
    - Line 63 of /var/www/html/docroot/modules/contrib/codit_batch_operations/src/cbo_scripts/TestDo10ThingsWithError.php
    

    or this without allow-skip which stops on the first error

    item_6 => simple stuff
    Error: 0 Call to undefined function Drupal\codit_batch_operations\cbo_scripts\someFunctionThatDontExist()
    - Line 59 of /var/www/html/docroot/modules/contrib/codit_batch_operations/src/cbo_scripts/TestDo10ThingsWithError.php
    
  • Status changed to Fixed 8 months ago
  • 🇺🇸United States swirt Florida
Production build 0.71.5 2024