Jump to content
Sign in to follow this  
NotionCommotion

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);
        }
    }

}

 

Share this post


Link to post
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);
    });
});

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

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.