Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on "close" event #120

Closed
hemantjp opened this issue Jan 17, 2018 · 8 comments
Closed

Error on "close" event #120

hemantjp opened this issue Jan 17, 2018 · 8 comments

Comments

@hemantjp
Copy link

hemantjp commented Jan 17, 2018

i wanted to reject the promise based on timer to cancel the request.
Kindly advise

PHP Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must be an instance of Exception, none given, called in /vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php on line 123

public function call()
    {
        $this->request = $this->client->request($this->method, $this->url);
        $deferred = new Deferred();
        $this->request->on('response', function (\React\HttpClient\Response $response)use ($deferred) {
            $response->on('data', function ($chunk) use ($deferred){
                    $deferred->resolve($chunk);
            });
        });
        $this->request->on('error', function (\Exception $e) use ($deferred){
            $deferred->reject($e->getMessage());
        });

Error thrown here
        // $this->request->on('close', function (\Exception $e) use ($deferred){
        //    $deferred->reject($e->getMessage());
        // });
        
        $this->request->end();
        return $deferred->promise();
    }

    public function close(){
        $this->request->close();
    }
$mainLoop->addTimer(15.0, function () use ($requestEndpoints) {
        $requestEndpoints->close();
    });
@clue
Copy link
Member

clue commented Jan 17, 2018

HI @hemantjp, I'm not sure I quite follow what your issue is. In case you want to apply a timeout to a pending request, have you seen #104 (comment)?

If your problem persists, can you give a short gist to reproduce the problem you're seeing and/or provide a more detailed exception trace?

@hemantjp
Copy link
Author

@clue , i have already referred to #104 . The close event of the request is not getting the proper arguments. i have passed it an exception class but it is saying none supplied.
attached is almost the complete stacktrace.

could this be an issue because i am using php7?

Should i provide more details?

@clue
Copy link
Member

clue commented Jan 17, 2018

The close event of the request is not getting the proper arguments. i have passed it an exception class but it is saying none supplied.

The Request implements WritableStreamInterface, so its documentation also applies (https://github.com/reactphp/stream#writablestreaminterface): The close event does not receive any arguments! You should probably use the error event in this case, which will report an Exception as per the above documentation.

I hope this helps 👍

I believe this has been answered, so I'm closing this for now. Please come back with more details if this problem persists and we can reopen this 👍

@clue clue closed this as completed Jan 17, 2018
@hemantjp
Copy link
Author

@clue the error event does not get triggered when i forcibly close the request.
Any suggestion as to how to trigger the promise reject.
i need to handle it down stream on a forcible timeout event.

@clue
Copy link
Member

clue commented Jan 17, 2018

the error event does not get triggered when i forcibly close the request.
Any suggestion as to how to trigger the promise reject.

Yes, explicitly calling close() will only emit the close event, not the error event. This is expected behavior. What promise are you referring to here? This API does not use promises right now (#41).

@hemantjp
Copy link
Author

@clue Referring to the deferred object i have created to maintain promise like feel.

public function call()
    {
        $this->request = $this->client->request($this->method, $this->url);
/* this deferred object*/
        $deferred = new Deferred();
        $this->request->on('response', function (\React\HttpClient\Response $response)use ($deferred) {
            $response->on('data', function ($chunk) use ($deferred){
                    $deferred->resolve($chunk);
            });
        });
        $this->request->on('error', function (\Exception $e) use ($deferred){
            $deferred->reject($e->getMessage());
        });

Error thrown here
        // $this->request->on('close', function (\Exception $e) use ($deferred){
        //    $deferred->reject($e->getMessage());
        // });
        
        $this->request->end();
        return $deferred->promise();
    }

    public function close(){
        $this->request->close();
    }

@clue
Copy link
Member

clue commented Jan 17, 2018

Your API seems to suffer from a number of flaws, e.g. it looks like your API is subject to a temporal dependence. If you explicitly call your close() method, you could as well access your Deferred to explicitly reject() it. As an alternative, you may register to the close event and just reject() with a generic error there.

@hemantjp
Copy link
Author

@clue thank you sir. i was mislead thinking that close event needed an argument. it should have dawned on me it didnt need one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants