ENH Improve YamlReader exception message (#9731)

Fixes #9690
This commit is contained in:
wernerkrauss 2020-10-12 11:38:13 +02:00 committed by GitHub
parent e7d52d3f91
commit 941df19e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View File

@ -39,7 +39,7 @@ class YamlReader implements Reader
// Normalise messages // Normalise messages
return $this->normaliseMessages($yaml[$locale]); return $this->normaliseMessages($yaml[$locale]);
} catch (ParseException $exception) { } catch (ParseException $exception) {
throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s"', $path), 0, $exception); throw new InvalidResourceException(sprintf('Error parsing YAML, invalid file "%s". Message: %s', $path, $exception->getMessage()), 0, $exception);
} }
} }

View File

@ -4,6 +4,7 @@ namespace SilverStripe\i18n\Tests;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\Messages\YamlReader; use SilverStripe\i18n\Messages\YamlReader;
use Symfony\Component\Translation\Exception\InvalidResourceException;
class YamlReaderTest extends SapphireTest class YamlReaderTest extends SapphireTest
{ {
@ -38,4 +39,15 @@ class YamlReaderTest extends SapphireTest
]; ];
$this->assertEquals($expected, $output); $this->assertEquals($expected, $output);
} }
public function testThrowsMeaningfulExceptionWhenYmlIsCorrupted()
{
$path = __DIR__ . '/i18nTest/_fakewebroot/i18ntestmodule/lang/en_corrupt.yml';
$this->expectException(InvalidResourceException::class);
$regex_path = str_replace('.', '\.', $path);
$this->expectExceptionMessageRegExp('@^Error parsing YAML, invalid file "' . $regex_path . '"\. Message: ([\w ].*) line 5 @');
$reader = new YamlReader();
$reader->read('en', $path);
}
} }

View File

@ -0,0 +1,5 @@
en:
NONAMESPACE: Include Entity without Namespace
Invalid: Foo
About: 'About us'
- Invalid # this should throw "A colon cannot be used in an unquoted mapping value at line 5"