Skip to content

Commit 2189d03

Browse files
committed
Make dispatchQueues errors more readable
1 parent 712720d commit 2189d03

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/DataLoader.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,10 @@ private function dispatchQueueBatch($batch)
191191
/** @var Promise $batchPromise */
192192
$batchPromise = $batchLoadFunction($keys);
193193

194-
if (! $batchPromise || ! \is_callable([$batchPromise, 'then'])) {
195-
return $this->handleFailedDispatch($batch, new DataLoaderException(
196-
self::class . ' must be constructed with a function which accepts ' .
197-
'an array of keys and returns a Promise which resolves to an array of values ' .
198-
\sprintf('not return a Promise: %s.', \gettype($batchPromise))
199-
));
194+
try {
195+
$this->validateBatchPromise($batchPromise);
196+
} catch (DataLoaderException $exception) {
197+
return $this->handleFailedDispatch($batch, $exception);
200198
}
201199

202200
$batchPromise->then(
@@ -262,7 +260,7 @@ private function handleFailedDispatch(array $batch, \Exception $error)
262260
}
263261

264262
/**
265-
* Validates the batch promises output.
263+
* Validates the batch promise's output.
266264
*
267265
* @param array $values Values from resolved promise.
268266
* @param array $keys Keys which the DataLoaders load was called with
@@ -286,4 +284,22 @@ private function validateBatchPromiseOutput($values, $keys)
286284
);
287285
}
288286
}
287+
288+
/**
289+
* Validates the batch promise returned from the batch load function.
290+
*
291+
* @param $batchPromise
292+
*
293+
* @throws DataLoaderException
294+
*/
295+
private function validateBatchPromise($batchPromise)
296+
{
297+
if (! $batchPromise || ! \is_callable([$batchPromise, 'then'])) {
298+
throw new DataLoaderException(
299+
self::class . ' must be constructed with a function which accepts ' .
300+
'an array of keys and returns a Promise which resolves to an array of values ' .
301+
\sprintf('the function returned %s.', \gettype($batchPromise))
302+
);
303+
}
304+
}
289305
}

tests/Unit/DataLoaderAbuseTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function batch_function_must_return_a_promise_not_null()
7474

7575
/** @var DataLoaderException $exception */
7676
$expectedExceptionMessage = 'leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys '
77-
. 'and returns a Promise which resolves to an array of values not return a Promise: NULL.';
77+
. 'and returns a Promise which resolves to an array of values the function returned NULL.';
7878
$this->assertInstanceOf(DataLoaderException::class, $exception);
7979
$this->assertSame($expectedExceptionMessage, $exception->getMessage());
8080
}
@@ -98,7 +98,7 @@ public function batch_function_must_return_a_promise_not_a_array()
9898

9999
/** @var DataLoaderException $exception */
100100
$expectedExceptionMessage = 'leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys '
101-
. 'and returns a Promise which resolves to an array of values not return a Promise: array.';
101+
. 'and returns a Promise which resolves to an array of values the function returned array.';
102102
$this->assertInstanceOf(DataLoaderException::class, $exception);
103103
$this->assertSame($expectedExceptionMessage, $exception->getMessage());
104104
}
@@ -122,7 +122,7 @@ public function batch_function_must_return_a_promise_not_a_value()
122122

123123
/** @var DataLoaderException $exception */
124124
$expectedExceptionMessage = 'leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys '
125-
. 'and returns a Promise which resolves to an array of values not return a Promise: integer.';
125+
. 'and returns a Promise which resolves to an array of values the function returned integer.';
126126
$this->assertInstanceOf(DataLoaderException::class, $exception);
127127
$this->assertSame($expectedExceptionMessage, $exception->getMessage());
128128
}
@@ -147,7 +147,7 @@ function ($keys) {
147147

148148
/** @var DataLoaderException $exception */
149149
$expectedExceptionMessage = 'leinonen\DataLoader\DataLoader must be constructed with a function which accepts an array of keys '
150-
. 'and returns a Promise which resolves to an array of values not return a Promise: object.';
150+
. 'and returns a Promise which resolves to an array of values the function returned object.';
151151
$this->assertInstanceOf(DataLoaderException::class, $exception);
152152
$this->assertSame($expectedExceptionMessage, $exception->getMessage());
153153
}

0 commit comments

Comments
 (0)