From e159c80b0c02e8f048c62f11df0ed51e0e9572f4 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 28 Jul 2024 20:08:53 -0700 Subject: [PATCH] improve error generation and handling --- masque/file/gdsii.py | 4 ++-- masque/file/oasis.py | 2 +- masque/pattern.py | 8 ++++---- masque/shapes/path.py | 4 ++-- masque/shapes/text.py | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/masque/file/gdsii.py b/masque/file/gdsii.py index d37a634..a28979d 100644 --- a/masque/file/gdsii.py +++ b/masque/file/gdsii.py @@ -409,8 +409,8 @@ def _annotations_to_properties(annotations: annotations_t, max_len: int = 126) - for key, vals in annotations.items(): try: i = int(key) - except ValueError: - raise PatternError(f'Annotation key {key} is not convertable to an integer') + except ValueError as err: + raise PatternError(f'Annotation key {key} is not convertable to an integer') from err if not (0 < i < 126): raise PatternError(f'Annotation key {key} converts to {i} (must be in the range [1,125])') diff --git a/masque/file/oasis.py b/masque/file/oasis.py index 10064b9..ad08623 100644 --- a/masque/file/oasis.py +++ b/masque/file/oasis.py @@ -298,7 +298,7 @@ def read( cap_start = path_cap_map[element.get_extension_start()[0]] cap_end = path_cap_map[element.get_extension_end()[0]] if cap_start != cap_end: - raise Exception('masque does not support multiple cap types on a single path.') # TODO handle multiple cap types + raise PatternError('masque does not support multiple cap types on a single path.') # TODO handle multiple cap types cap = cap_start path_args: dict[str, Any] = {} diff --git a/masque/pattern.py b/masque/pattern.py index 637b953..0c3b6cb 100644 --- a/masque/pattern.py +++ b/masque/pattern.py @@ -1016,10 +1016,10 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable): try: from matplotlib import pyplot # type: ignore import matplotlib.collections # type: ignore - except ImportError as err: - logger.error('Pattern.visualize() depends on matplotlib!') - logger.error('Make sure to install masque with the [visualize] option to pull in the needed dependencies.') - raise err + except ImportError: + logger.exception('Pattern.visualize() depends on matplotlib!\n' + + 'Make sure to install masque with the [visualize] option to pull in the needed dependencies.') + raise if self.has_refs() and library is None: raise PatternError('Must provide a library when visualizing a pattern with refs') diff --git a/masque/shapes/path.py b/masque/shapes/path.py index 67f750e..bfaf14f 100644 --- a/masque/shapes/path.py +++ b/masque/shapes/path.py @@ -105,11 +105,11 @@ class Path(Shape): custom_caps = (PathCap.SquareCustom,) if self.cap in custom_caps: if vals is None: - raise Exception('Tried to set cap extensions to None on path with custom cap type') + raise PatternError('Tried to set cap extensions to None on path with custom cap type') self._cap_extensions = numpy.array(vals, dtype=float) else: if vals is not None: - raise Exception('Tried to set custom cap extensions on path with non-custom cap type') + raise PatternError('Tried to set custom cap extensions on path with non-custom cap type') self._cap_extensions = vals # vertices property diff --git a/masque/shapes/text.py b/masque/shapes/text.py index 5453c51..fa0038b 100644 --- a/masque/shapes/text.py +++ b/masque/shapes/text.py @@ -221,7 +221,7 @@ def get_char_as_polygons( 'advance' distance (distance from the start of this glyph to the start of the next one) """ if len(char) != 1: - raise Exception('get_char_as_polygons called with non-char') + raise PatternError('get_char_as_polygons called with non-char') face = Face(font_path) face.set_char_size(resolution)