Skip to content

Commit a7437fb

Browse files
committed
Improve exception messages
1 parent dbaf667 commit a7437fb

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/Data.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function set(string $key, $value = null): void
8484
$currentValue[$currentKey] = [];
8585
}
8686
if (!is_array($currentValue[$currentKey])) {
87-
throw new DataException("Key path at $currentKey of $key cannot be indexed into (is not an array)");
87+
throw new DataException(sprintf('Key path "%s" within "%s" cannot be indexed into (is not an array)', $currentKey, self::formatPath($key)));
8888
}
8989
$currentValue =& $currentValue[$currentKey];
9090
}
@@ -166,7 +166,7 @@ public function getData(string $key): DataInterface
166166
return new Data($value);
167167
}
168168

169-
throw new DataException("Value at '$key' could not be represented as a DataInterface");
169+
throw new DataException(sprintf('Value at "%s" could not be represented as a DataInterface', self::formatPath($key)));
170170
}
171171

172172
/**
@@ -248,4 +248,20 @@ protected static function keyToPathArray(string $path): array
248248

249249
return \explode('.', $path);
250250
}
251+
252+
/**
253+
* @param string|string[] $path
254+
*
255+
* @return string
256+
*
257+
* @psalm-pure
258+
*/
259+
protected static function formatPath($path): string
260+
{
261+
if (is_string($path)) {
262+
$path = self::keyToPathArray($path);
263+
}
264+
265+
return implode(' » ', $path);
266+
}
251267
}

tests/DataTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function runSampleDataTests(DataInterface $data)
6565
$this->assertEquals($data->get('f/g/h/i', 'default-value-2'), 'default-value-2');
6666

6767
$this->expectException(InvalidPathException::class);
68+
$this->expectExceptionMessage('Path cannot be an empty string');
6869
$data->get('', 'broken');
6970
}
7071

@@ -93,7 +94,7 @@ public function testAppend()
9394
$this->assertEquals(['L'], $data->get('i.k.l'));
9495

9596
$this->expectException(InvalidPathException::class);
96-
97+
$this->expectExceptionMessage('Path cannot be an empty string');
9798
$data->append('', 'broken');
9899
}
99100

@@ -116,7 +117,7 @@ public function testSet()
116117
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data->get('d'));
117118

118119
$this->expectException(InvalidPathException::class);
119-
120+
$this->expectExceptionMessage('Path cannot be an empty string');
120121
$data->set('', 'broken');
121122
}
122123

@@ -127,7 +128,7 @@ public function testSetClobberStringInPath()
127128
$data->set('a.b.c', 'Should not be able to write to a.b.c.d.e');
128129

129130
$this->expectException(DataException::class);
130-
131+
$this->expectExceptionMessage('Key path "c" within "a » b » c » d » e" cannot be indexed into (is not an array)');
131132
$data->set('a.b.c.d.e', 'broken');
132133
}
133134

@@ -149,7 +150,7 @@ public function testRemove()
149150
$this->assertEquals('D2', $data->get('b.d.d2'));
150151

151152
$this->expectException(InvalidPathException::class);
152-
153+
$this->expectExceptionMessage('Path cannot be an empty string');
153154
$data->remove('', 'broken');
154155
}
155156

@@ -177,6 +178,7 @@ public function testHas()
177178
}
178179

179180
$this->expectException(InvalidPathException::class);
181+
$this->expectExceptionMessage('Path cannot be an empty string');
180182
$data->has('', 'broken');
181183
}
182184

@@ -200,8 +202,7 @@ public function testGetDataOnNonArrayValue()
200202
]);
201203

202204
$this->expectException(DataException::class);
203-
$this->expectExceptionMessageRegExp('/could not be represented as a DataInterface/');
204-
205+
$this->expectExceptionMessage('Value at "foo" could not be represented as a DataInterface');
205206
$data->getData('foo');
206207
}
207208

@@ -287,7 +288,7 @@ public function testOffsetSet()
287288
$this->assertEquals(['e' => ['f' => 'F', 'g' => 'G']], $data['d']);
288289

289290
$this->expectException(InvalidPathException::class);
290-
291+
$this->expectExceptionMessage('Path cannot be an empty string');
291292
$data->set('', 'broken');
292293
}
293294

@@ -309,7 +310,7 @@ public function testOffsetUnset()
309310
$this->assertEquals('D2', $data['b.d.d2']);
310311

311312
$this->expectException(InvalidPathException::class);
312-
313+
$this->expectExceptionMessage('Path cannot be an empty string');
313314
unset($data['']);
314315
}
315316
}

0 commit comments

Comments
 (0)