From bd22f277c309224fd4123c4ff8b1d482cd7ac316 Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Thu, 21 Jun 2018 11:53:52 +0200
Subject: [PATCH] identifiers: Reuse ValidationError exception + update
 docstring

Close T1104
Close D346
---
 swh/model/cli.py                    |  4 ++--
 swh/model/identifiers.py            | 31 ++++++++++++++++-------------
 swh/model/tests/test_identifiers.py |  6 ++----
 3 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/swh/model/cli.py b/swh/model/cli.py
index 7d018d6d..5996d19a 100644
--- a/swh/model/cli.py
+++ b/swh/model/cli.py
@@ -8,8 +8,8 @@ import os
 import sys
 
 from swh.model import identifiers as pids
+from swh.model.exceptions import ValidationError
 from swh.model.from_disk import Content, Directory
-from swh.model.identifiers import SWHMalformedIdentifierException
 
 
 class PidParamType(click.ParamType):
@@ -19,7 +19,7 @@ class PidParamType(click.ParamType):
         try:
             pids.parse_persistent_identifier(value)
             return value  # return as string, as we need just that
-        except SWHMalformedIdentifierException as e:
+        except ValidationError as e:
             self.fail('%s is not a valid PID. %s.' % (value, e), param, ctx)
 
 
diff --git a/swh/model/identifiers.py b/swh/model/identifiers.py
index 6a15c9be..00471f35 100644
--- a/swh/model/identifiers.py
+++ b/swh/model/identifiers.py
@@ -663,19 +663,22 @@ PERSISTENT_IDENTIFIER_KEYS = [
 PERSISTENT_IDENTIFIER_PARTS_SEP = ';'
 
 
-class SWHMalformedIdentifierException(ValueError):
-    """Exception when a string representing an identifier is badly formatted.
-
-    """
-    pass
-
-
 def parse_persistent_identifier(persistent_id):
     """Parse swh's :ref:`persistent-identifiers` scheme.
 
     Args:
         persistent_id (str): A persistent identifier
 
+    Raises:
+        ValidationError (class) in case of:
+
+            missing mandatory values (4)
+            invalid namespace supplied
+            invalid version supplied
+            invalid type supplied
+            missing hash
+            invalid hash identifier supplied
+
     Returns:
         dict: dict with keys :
 
@@ -691,33 +694,33 @@ def parse_persistent_identifier(persistent_id):
     pid_data = persistent_id_parts.pop(0).split(':')
 
     if len(pid_data) != 4:
-        raise SWHMalformedIdentifierException(
+        raise ValidationError(
             'Wrong format: There should be 4 mandatory parameters')
 
     # Checking for parsing errors
     _ns, _version, _type, _id = pid_data
     if _ns != 'swh':
-        raise SWHMalformedIdentifierException(
+        raise ValidationError(
             'Wrong format: Supported namespace is \'swh\'')
 
     if _version != '1':
-        raise SWHMalformedIdentifierException(
+        raise ValidationError(
             'Wrong format: Supported version is 1')
 
     expected_types = PERSISTENT_IDENTIFIER_TYPES
     if _type not in expected_types:
-        raise SWHMalformedIdentifierException(
+        raise ValidationError(
             'Wrong format: Supported types are %s' % (
                 ', '.join(expected_types)))
 
     if not _id:
-        raise SWHMalformedIdentifierException(
+        raise ValidationError(
             'Wrong format: Identifier should be present')
 
     try:
         validate_sha1(_id)
     except ValidationError:
-        raise SWHMalformedIdentifierException(
+        raise ValidationError(
            'Wrong format: Identifier should be a valid hash')
 
     persistent_id_metadata = {}
@@ -727,6 +730,6 @@ def parse_persistent_identifier(persistent_id):
             persistent_id_metadata[key] = val
         except Exception:
             msg = 'Contextual data is badly formatted, form key=val expected'
-            raise SWHMalformedIdentifierException(msg)
+            raise ValidationError(msg)
     pid_data.append(persistent_id_metadata)
     return dict(zip(PERSISTENT_IDENTIFIER_KEYS, pid_data))
diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py
index 7b0720e9..7daf8e40 100644
--- a/swh/model/tests/test_identifiers.py
+++ b/swh/model/tests/test_identifiers.py
@@ -13,7 +13,7 @@ from swh.model import hashutil, identifiers
 
 from swh.model.exceptions import ValidationError
 from swh.model.identifiers import SNAPSHOT, RELEASE, REVISION, DIRECTORY
-from swh.model.identifiers import CONTENT
+from swh.model.identifiers import CONTENT, PERSISTENT_IDENTIFIER_TYPES
 
 
 class UtilityFunctionsIdentifier(unittest.TestCase):
@@ -876,8 +876,6 @@ class SnapshotIdentifier(unittest.TestCase):
             self.assertEquals(actual_result, expected_result)
 
     def test_parse_persistent_identifier_parsing_error(self):
-        from swh.model.identifiers import (SWHMalformedIdentifierException,
-                                           PERSISTENT_IDENTIFIER_TYPES)
         for pid, _error in [
                 ('swh:1:cnt',
                  'Wrong format: There should be 4 mandatory parameters'),
@@ -903,5 +901,5 @@ class SnapshotIdentifier(unittest.TestCase):
                  'Wrong format: Identifier should be a valid hash')
         ]:
             with self.assertRaisesRegex(
-                    SWHMalformedIdentifierException, _error):
+                    ValidationError, _error):
                 identifiers.parse_persistent_identifier(pid)
-- 
GitLab