From c065498911725c6edf6c6f5fe30e14a5fadb186f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@inrae.fr>
Date: Mon, 6 Apr 2020 12:39:27 +0200
Subject: [PATCH] Rend bloquantes les erreurs de mapping.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Mais améliore les messages d'erreurs.
---
 src/php/ModelGenerator.php      |  4 ++--
 src/php/SerializationMapper.php | 24 ++++++++++++++++++------
 src/php/TypeFactory.php         |  2 +-
 3 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/php/ModelGenerator.php b/src/php/ModelGenerator.php
index 2a06132..94ed39e 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 1976153..09920f2 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 fa3fea9..995183a 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];
-- 
GitLab