diff --git a/src/php/ModelGenerator.php b/src/php/ModelGenerator.php index 2a06132de47f16037acd779bb9758de55435d073..94ed39e804b2fe8e601231e11de13e8b438e96de 100644 --- a/src/php/ModelGenerator.php +++ b/src/php/ModelGenerator.php @@ -269,8 +269,8 @@ final class ModelGenerator if ($repo) { $repositories[$repo->getName()] = $repo; } - } catch (Exception $ex) { - printf("Error while mapping %s: %s\n%s\n", $class, $ex->getMessage(), $ex->getTraceAsString()); + } catch (\Throwable $ex) { + throw new DomainException(sprintf('error with resource `%s`: %s', $resourceMeta->getBaseName(), $ex->getMessage()), 0, $ex); } } diff --git a/src/php/SerializationMapper.php b/src/php/SerializationMapper.php index 1976153e77862d4210291c0b28b663f287223cc2..09920f2eb9dc957ae861a5e24ae37e9b511793ac 100644 --- a/src/php/SerializationMapper.php +++ b/src/php/SerializationMapper.php @@ -103,21 +103,33 @@ final class SerializationMapper implements TypeFactoryInterface { if (class_exists($name)) { $class = PHPClass::get($name); + if ($this->serialization->hasRepresentationOf($class)) { $repr = $this->serialization->getRepresentationOf($class); - return $this->defer($repr->getName()) - ->resolveWith( - function () use ($repr) { - return $this->mapRepresentation($repr); - } - ); + return $this->deferredMapping($name, $repr->getName(), function () use ($repr) { + return $this->mapRepresentation($repr); + }); } } return $this->typeFactory->get($name); } + private function deferredMapping(string $name, string $actualName, callable $mapper): Type + { + return $this->defer($actualName) + ->resolveWith( + function () use ($name, $mapper) { + try { + return $mapper(); + } catch (\Throwable $ex) { + throw new DomainException(sprintf('error with %s: %s', $name, $ex->getMessage()), 0, $ex); + } + } + ); + } + public function getResourceData(): array { $resource = $this->serialization->getRoot(); diff --git a/src/php/TypeFactory.php b/src/php/TypeFactory.php index fa3fea9bf2f2d61ced7865a9a7427e407e7e3592..995183a1c5b7a8aa19e392dc17aa51af2be914f0 100644 --- a/src/php/TypeFactory.php +++ b/src/php/TypeFactory.php @@ -56,7 +56,7 @@ final class TypeFactory implements TypeFactoryInterface, \IteratorAggregate } if (!isset($this->types[$name])) { - throw new TypeNotFoundException("unknown type ${name}"); + throw new TypeNotFoundException("type not defined: ${name}"); } return $this->types[$name];