Jump to content

Logging errors in React socket server


Recommended Posts

I have the following socket server.

<?php

class Controller
    public function doSomething($conn):void {
        $foo=new SomeClassWhichDoesntExist();
    }
}    

require 'vendor/autoload.php';
$server = new \React\Socket\TcpServer($settings['url'].':'.$settings['port'], \React\EventLoop\Factory::create());
$server->on('connection', function (\React\Socket\ConnectionInterface $rawSocket) {
    $conn = new LengthPrefixStream($rawSocket);
    $conn->on('data', function($data) use ($conn){
        $controller->doSomething($conn);
    });
    $rawSocket->on('error', function($error, $conn) {exit('error';});
    $rawSocket->on('close', function() use ($conn) {exit('close';});
    $conn->on('error', function($error, $conn) {exit('error';});
    $conn->on('close', function() use ($conn) {exit('close';});
});
$server->on('error', function (Exception $e) {exit('server error';});

Turns out Controller::doSomething() is trying to create a new SomeClassWhichDoesntExist, but since it doesn't exist, composer doesn't require it and then PHP throws an error.  But the error is caught by FulfilledPromise::then(), and then appears to eventually be ignored and an exception is never thrown, and the loop continues.  In the past, I've always been able to catch these errors, but must have changed something.  Any thoughts what might have changed to prevent errors from being caught?

namespace React\Promise;
class FulfilledPromise implements ExtendedPromiseInterface, CancellablePromiseInterface
{
    public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
    {
        if (null === $onFulfilled) {
            return $this;
        }

        try {
            return resolve($onFulfilled($this->value));
        } catch (\Throwable $exception) {
            return new RejectedPromise($exception); //<===== Caught here
        } catch (\Exception $exception) {
            return new RejectedPromise($exception);
        }
    }

}

 

Link to comment
Share on other sites

Don't know why, but I found a clue.  If an error occurs in $controller->doSomethingOnData($conn);, then I catch the errors, however, $controller->doSomethingOnConnection($conn); goes uncaught. Fortunately, I don't do much in doSomethingOnConnection().

 

$server->on('connection', function (\React\Socket\ConnectionInterface $rawSocket) {
    $controller->doSomethingOnConnection($conn);
    $conn = new LengthPrefixStream($rawSocket);
    $conn->on('data', function($data) use ($conn){
        $controller->doSomethingOnData($conn);
    });
});

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.