0

composer.json packages as below,

"monolog/monolog": "~1.25.4",
"newrelic/monolog-enricher": "^1.0",

Below file is throwing run time exception,

vendor/monolog/monolog/src/Monolog/Handler/Curl/Util.php

<?php

namespace Monolog\Handler\Curl;

class Util
{
    private static $retriableErrorCodes = array(
        CURLE_COULDNT_RESOLVE_HOST,
        CURLE_COULDNT_CONNECT,
        CURLE_HTTP_NOT_FOUND,
        CURLE_READ_ERROR,
        CURLE_OPERATION_TIMEOUTED,
        CURLE_HTTP_POST_ERROR,
        CURLE_SSL_CONNECT_ERROR,
    );

    /**
     * Executes a CURL request with optional retries and exception on failure
     *
     * @param  resource          $ch curl handler
     * @throws \RuntimeException
     */
    public static function execute($ch, $retries = 5, $closeAfterDone = true)
    {
        while ($retries--) {
            if (curl_exec($ch) === false) {
                $curlErrno = curl_errno($ch);

                if (false === in_array($curlErrno, self::$retriableErrorCodes, true) || !$retries) {
                    $curlError = curl_error($ch);

                    if ($closeAfterDone) {
                        curl_close($ch);
                    }

                    throw new \RuntimeException(sprintf('Curl error (code %s): %s', $curlErrno, $curlError));
                }

                continue;
            }

            if ($closeAfterDone) {
                curl_close($ch);
            }
            break;
        }
    }
}

if a runtime error occurs, we don't want to stop application execution to add a log.

How can I handle such a situation?

I tried to overwrite class vendor/monolog/monolog/src/Monolog/Handler/Curl/Util.php

add the below code in the composer.json file

"autoload": {
    "psr-4": {
       "Monolog\\Handler\\Curl\\" : "lib/MonologCurl",
    }
}

created the same Util.php class in lib/MonologCurl directory to use modified code

and run composer dump-autoload

it is throwing an error as below

Fatal error: Uncaught Error: Class 'Monolog\Handler\Curl\Util' not found in /test/instances/fd/local/vendor/newrelic/monolog-enricher/src/Handler.php:138 Stack trace: #0 /test/instances/fd/local/vendor/newrelic/monolog-enricher/src/api1/Handler.php(53): NewRelic\Monolog\Enricher\AbstractHandler->sendBatch('[{"message":"St...') #1 /test/instances/fd/local/vendor/monolog/monolog/src/Monolog/Handler/BufferHandler.php(92): NewRelic\Monolog\Enricher\Handler->handleBatch(Array) #2 /test/instances/fd/local/vendor/monolog/monolog/src/Monolog/Handler/BufferHandler.php(108): Monolog\Handler\BufferHandler->flush() #3 [internal function]: Monolog\Handler\BufferHandler->close() #4 {main} thrown in /test/instances/fd/local/vendor/newrelic/monolog-enricher/src/Handler.php on line 138

what should be best solution?

hetal gohel
  • 335
  • 9
  • 21
  • 1
    You should never edit third-party code unless you really know what you're doing. Your original problem is that it's throwing the "Curl error" exception, and you want to log that instead of the process stopping? Simply wrap whatever call you're doing that might lead to this situation in a `try / catch` block to catch that exception and log it. – Greg Schmidt Oct 27 '22 at 23:01

0 Answers0