Skip to content

Commit efb9422

Browse files
authored
improve error handling (#11)
* improve error handling * reformatting
1 parent 3187cf1 commit efb9422

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

docker/dev/php/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:7.4-cli-alpine3.12
1+
FROM php:7.4-cli-alpine3.15
22

33
ARG HOST_USER_ID
44
ARG HOST_USER

src/Exception/SchemaRegistryException.php

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class SchemaRegistryException extends \Exception
88
{
99
public const FILE_PATH_EXCEPTION_MESSAGE = 'Unable to get file path';
1010
public const FILE_NOT_READABLE_EXCEPTION_MESSAGE = 'Unable to read file: %s';
11+
public const FILE_INVALID = 'Unable to json_decode file properly: %s';
1112
}

src/Merger/SchemaMerger.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function exportSchema(
157157
bool $optimizeSubSchemaNamespaces = false
158158
): void {
159159
$rootSchemaDefinition = $this->transformExportSchemaDefinition(
160-
json_decode($rootSchemaTemplate->getSchemaDefinition(), true)
160+
json_decode($rootSchemaTemplate->getSchemaDefinition(), true, JSON_THROW_ON_ERROR)
161161
);
162162

163163
$prefix = '';
@@ -205,8 +205,8 @@ public function transformExportSchemaDefinition(array $schemaDefinition): array
205205
*/
206206
private function excludeNamespacesForEmbeddedSchema(string $definition, string $embeddedDefinition): string
207207
{
208-
$decodedRootDefinition = json_decode($definition, true);
209-
$decodedEmbeddedDefinition = json_decode($embeddedDefinition, true);
208+
$decodedRootDefinition = json_decode($definition, true, JSON_THROW_ON_ERROR);
209+
$decodedEmbeddedDefinition = json_decode($embeddedDefinition, true, JSON_THROW_ON_ERROR);
210210

211211
if (
212212
isset($decodedRootDefinition['namespace']) && isset($decodedEmbeddedDefinition['namespace']) &&

src/Registry/SchemaRegistry.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ private function registerSchemaFile(\SplFileInfo $fileInfo): void
129129
);
130130
}
131131

132-
$schemaData = json_decode($fileContent, true);
132+
$schemaData = json_decode($fileContent, true, JSON_THROW_ON_ERROR);
133+
134+
if (null === $schemaData) {
135+
throw new SchemaRegistryException(sprintf(SchemaRegistryException::FILE_INVALID, $fileName));
136+
}
137+
133138
$template = (new SchemaTemplate())
134139
->withFilename($fileInfo->getBasename())
135140
->withSchemaDefinition($fileContent)

src/Schema/SchemaTemplate.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function withFilename(string $filename): SchemaTemplateInterface
127127
*/
128128
public function isPrimitive(): bool
129129
{
130-
$fields = json_decode($this->getSchemaDefinition(), true);
130+
$fields = json_decode($this->getSchemaDefinition(), true, JSON_THROW_ON_ERROR);
131131

132132
if (true === isset($fields['type'])) {
133133
return array_key_exists($fields['type'], self::AVRO_PRIMITIVE_TYPES);

tests/Integration/Registry/SchemaRegistryTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,27 @@ public function testRegisterSchemaFileThatIsNotReadable()
120120
unlink('testfile');
121121
}
122122
}
123+
124+
public function testRegisterSchemaWithInvalidContent()
125+
{
126+
touch('testfile');
127+
128+
$fileInfo = new SplFileInfo('testfile');
129+
130+
$registry = new SchemaRegistry();
131+
132+
self::expectException(SchemaRegistryException::class);
133+
self::expectExceptionMessage(
134+
sprintf(SchemaRegistryException::FILE_INVALID, $fileInfo->getRealPath())
135+
);
136+
137+
$reflection = new ReflectionClass(SchemaRegistry::class);
138+
$method = $reflection->getMethod('registerSchemaFile');
139+
$method->setAccessible(true);
140+
try {
141+
$method->invokeArgs($registry, [$fileInfo]);
142+
} finally {
143+
unlink('testfile');
144+
}
145+
}
123146
}

0 commit comments

Comments
 (0)