prolic / HumusAmqpModule

AMQP module for Zend Framework 2 to integrate RabbitMQ
https://humusamqp.readthedocs.io
MIT License
31 stars 13 forks source link

RpcServer Response handling #39

Closed ZhaoVitapublic closed 8 years ago

ZhaoVitapublic commented 8 years ago

Hi, thanks for provider this great module. We have implemented several rpc-server and consumer project with this module. Recently, I got a situation, where i need to modify the success flag and message of response by a rpc-server, but not throw an exception within customized CallBack class. In my callback locate an input filter with validators. If the validation process failed, I want return an array of error messages and give an unsuccess flag to the rpc-client. Can you make a feature, that in callback we not only affacts the result, but also the success flag? Or do you have some idea, that I can do it better in the case above? Thanks in advance

prolic commented 8 years ago

the success flag is like in http a status code 200. even if let's say a form was invalid, you get a http status code 200 and a new page is rendered with form error messages. the success flag is really more like when an exception happened. so in your case i would return an array with success - true/false and the result or error messages. so your rpc result can look like this:

[
    'success' => true, // server result, meaning no exception
    'result' => [
        'success' => true, // your result was good
        'result' => 'real result'
]

or

[
    'success' => true, // server result, meaning no exception
    'result' => [
        'success' => false, // your result, meaning the data provided was invalid
        'result' => 'error message'
]

does that help? or do you still think there should be changed something?

ZhaoVitapublic commented 8 years ago

Thanks for the reply and suggestion. It is surely a solution of my problem. In the other hand, i get some information from http://www.jsonrpc.org/specification. There is some specification for suggestion of json format by rpc request. For example the structure of Error object like:

{
    "code": -32000,
    "message": "single sentence",
    "data": "A Primitive or Structured value that contains additional information about the error."
}

Are you interested to implement something in RpcServer class like fowlloing:

    public function handleDelivery(AMQPEnvelope $message, AMQPQueue $queue)
    {
        try {
            .........
            $params = compact('message', 'queue');
            $results = $this->getEventManager()->trigger('delivery', $this, $params);
            $result = $results->last();
            /****** give the possibility to define response *******/
            if ( $result instanceof RpcResponseInterface) {
                if ($result->getSuccess() == true) {
                    $response = json_encode([
                        'success' => true,
                        'result'  => $result->getResult(),
                    ]);
                } else {
                   $response = json_encode([
                        'success' => false,
                        'error'   => $result->getError(),
                   ]);
                }
            } else {
                 $response = json_encode(array('success' => true, 'result' => $result));
            }

            $this->sendReply($response, $message->getReplyTo(), $message->getCorrelationId());
        } catch (\Exception $e) {
           .......
        }
    }

with a RpcResponseInterface like:

    interface RpcResponseInterface {
        abstract function getSuccess();
        abstract function getResult();
        abstract function getError();
    }
prolic commented 8 years ago

I will consider this with my rewrite here: https://github.com/prolic/HumusAmqp

this will be framework-independent, php 7 only and with different drivers (ext-amqp, phpamqplib). This module here will then only register some services to zf2 module system.

ZhaoVitapublic commented 8 years ago

Thanks for the consideration :)

prolic commented 8 years ago

solved with https://github.com/prolic/HumusAmqp/issues/10