<commit_before><?php namespace TooBasic\Rpc\Client;
use TooBasic\Rpc;
use TooBasic\Exception;

class Json
{
	protected $_url;
	protected $_transport;
	protected $_id = 1;
	protected $_isNotification = false;

	public function __construct($url, Rpc\Transport $transport)
	{
		$this->_url = $url;
		$this->_transport = $transport;
	}

	public function setNotification($n = true)
	{
		$this->_isNotification = (bool)$n;
	}

	public function __call($method, $params)
	{
		$requestId = $this->_isNotification ? null : $this->_id;
		$request = json_encode([
			'method' => $method,
			'params' => array_values($params),
			'id' => $requestId,
		]);

		$rawResponse = $this->_transport->request('POST', $this->_url, ['Content-type' => 'application/json'], $request);
		$response = json_decode($rawResponse);

		if (false == $response)
			throw new Exception('Could not decode response as json: `%s`', [$rawResponse]);

		if ($this->_isNotification)
			return true;

		if ($response->id != $requestId)
			throw new Exception('Unexpected responseId '. $response->id .', expected '. $requestId);

		if (!is_null($response->error))
			throw new Exception('Request error: '. $response->error->message);

		return $response->result;
	}
}<commit_msg>Rpc/Json - check for error before checking requestId (which wouldn't exist)<commit_after><?php namespace TooBasic\Rpc\Client;
use TooBasic\Rpc;
use TooBasic\Exception;

class Json
{
	protected $_url;
	protected $_transport;
	protected $_id = 1;
	protected $_isNotification = false;

	public function __construct($url, Rpc\Transport $transport)
	{
		$this->_url = $url;
		$this->_transport = $transport;
	}

	public function setNotification($n = true)
	{
		$this->_isNotification = (bool)$n;
	}

	public function __call($method, $params)
	{
		$requestId = $this->_isNotification ? null : $this->_id;
		$request = json_encode([
			'method' => $method,
			'params' => array_values($params),
			'id' => $requestId,
		]);

		$rawResponse = $this->_transport->request('POST', $this->_url, ['Content-type' => 'application/json'], $request);
		$response = json_decode($rawResponse);

		if (false == $response)
			throw new Exception('Could not decode response as json: `%s`', [$rawResponse]);

		if ($this->_isNotification)
			return true;

		if (!empty($response->error))
			throw new Exception('Request error: '. $response->error->message);

		if ($response->id != $requestId)
			throw new Exception('Unexpected responseId '. $response->id .', expected '. $requestId);

		return $response->result;
	}
}
{'fraction_non_alphanumeric': 0.1670616113744076, 'fraction_numerical': 0.0007898894154818325, 'mean_word_length': 4.591611479028698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2076887', 'n_tokens_mistral': 861, 'n_tokens_neox': 783, 'n_words': 197}