spiral-modules / php-grpc

:electric_plug: Fast and furious GRPC server for PHP applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exceptions details decoding

hotrush opened this issue · comments

Hello, i have a dummy question about exceptions details.

For example I have several proto files:

auth.proto

syntax = "proto3";

option php_namespace = "Pkg\\Sdk\\Auth\\Service";
option php_metadata_namespace = "Pkg\\Sdk\\Auth\\GPBMetadata";

package pkg;

service Auth {
    rpc CreateUser(CreateUserRequest) returns (CreatedUserResult) {}
}

message CreateUserRequest {
    string phone = 1;
    string password = 2;
}

message CreatedUserResult {
    string id = 1;
}

and errors.proto

syntax = "proto3";

option php_namespace = "Pkg\\Sdk\\Errors\\Service";
option php_metadata_namespace = "Pkg\\Sdk\\Errors\\GPBMetadata";

package pkg;

message ValidationDetails {
    message FieldViolation {
        string field = 1;
        string message = 2;
    }
    repeated FieldViolation field_violations = 1;
}

In service implementation i throw an exception:

throw new GRPC\Exception\GRPCException(
    'Bad Request',
     GRPC\StatusCode::INVALID_ARGUMENT,
     [
         new ValidationDetails([
             'field_violations' => [
                 new ValidationDetails\FieldViolation([
                    'field' => 'phone',
                    'message' => 'Phone already in use',
                ])
            ]
        ])
    ]
);

And when i'm calling this service with generated client i expect that on error i will receive decoded exceptions array, but getting some piece of broken string:

stdClass object 
(
·    metadata = array(1)
·    [
·    ·    ['grpc-status-details-bin'] = array(1)
·    ·    [
·    ·    ·    [0] = string(97) ����Bad Request�P
-type.googleapis.com/pkg.ValidationDetails��
�
�phone��Phone already in use
·    ·    ]
·    ]
·    code = integer(1) 3
·    details = string(11) Bad Request
)

Any luck to get it decoded into php object or it doesn't work in that way. Found something similar in google's repo https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto

With next code i received ValidationDetails object, but getFieldViolations returns empty container...

        $any = new Any();
        $any->setValue($result->getStatus()->metadata['grpc-status-details-bin'][0]);
        $any->setTypeUrl('type.googleapis.com/pkg.ValidationDetails');
        $err = $any->unpack();
        dump($err->getFieldViolations()->count()); // 0