in case of errors, ``$rawResult`` will not be a valid Json string. IMO, this decoding should be moved after the error handling (which will also eliminate the need of using 2 variables for this:

```php
switch ($returnType) {
    case 'js':
        $result = $conn->socketSend($str);
        break;

    case 'json':
        $result = $conn->socketSend("stream.end(JSON.stringify({$str}))");
        break;

    default:
        throw new \InvalidArgumentException(sprintf('Invalid return type "%s", $returnType));
}

$errorPrefixLength = strlen(self::ERROR_PREFIX);

if (self::ERROR_PREFIX === substr($result, 0, $errorPrefixLength)) {
    $errorMessage = json_decode(substr($result, $errorPrefixLength), true);

    throw new DriverException('Executing of ' . $str . ' code failed with error: ' . $errorMessage);
}

if ('json' === $returnType) {
    return json_decode($result);
}

return $result;
```