Refactor error messages to remove hardcoded strings (#10729)

* Use custom error msg instead of hardcoded string: replaced remaining hardcoded error message strings.

* Use custom error msg instead of hardcoded string: fixing faulty Errors import.
This commit is contained in:
Raphael Mitsch 2022-05-02 13:38:46 +02:00 committed by GitHub
parent 0a503ce5e0
commit f5390e278a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 15 deletions

View File

@ -905,7 +905,15 @@ class Errors(metaclass=ErrorsWithCodes):
E1026 = ("Edit tree has an invalid format:\n{errors}")
E1027 = ("AlignmentArray only supports slicing with a step of 1.")
E1028 = ("AlignmentArray only supports indexing using an int or a slice.")
E1029 = ("Edit tree cannot be applied to form.")
E1030 = ("Edit tree identifier out of range.")
E1031 = ("Could not find gold transition - see logs above.")
E1032 = ("`{var}` should not be {forbidden}, but received {value}.")
E1033 = ("Dimension {name} invalid -- only nO, nF, nP")
E1034 = ("Node index {i} out of bounds ({length})")
E1035 = ("Token index {i} out of bounds ({length})")
E1036 = ("Cannot index into NoneNode")
# Deprecated model shortcuts, only used in errors and warnings
OLD_MODEL_SHORTCUTS = {

View File

@ -11,6 +11,7 @@ import numpy.random
from thinc.api import Model, CupyOps, NumpyOps
from .. import util
from ..errors import Errors
from ..typedefs cimport weight_t, class_t, hash_t
from ..pipeline._parser_internals.stateclass cimport StateClass
@ -411,7 +412,7 @@ cdef class precompute_hiddens:
elif name == "nO":
return self.nO
else:
raise ValueError(f"Dimension {name} invalid -- only nO, nF, nP")
raise ValueError(Errors.E1033.format(name=name))
def set_dim(self, name, value):
if name == "nF":
@ -421,7 +422,7 @@ cdef class precompute_hiddens:
elif name == "nO":
self.nO = value
else:
raise ValueError(f"Dimension {name} invalid -- only nO, nF, nP")
raise ValueError(Errors.E1033.format(name=name))
def __call__(self, X, bint is_train):
if is_train:

View File

@ -132,7 +132,7 @@ cdef class EditTrees:
could not be applied to the form.
"""
if tree_id >= self.trees.size():
raise IndexError("Edit tree identifier out of range")
raise IndexError(Errors.E1030)
lemma_pieces = []
try:
@ -154,7 +154,7 @@ cdef class EditTrees:
match_node = tree.inner.match_node
if match_node.prefix_len + match_node.suffix_len > len(form_part):
raise ValueError("Edit tree cannot be applied to form")
raise ValueError(Errors.E1029)
suffix_start = len(form_part) - match_node.suffix_len
@ -169,7 +169,7 @@ cdef class EditTrees:
if form_part == self.strings[tree.inner.subst_node.orig]:
lemma_pieces.append(self.strings[tree.inner.subst_node.subst])
else:
raise ValueError("Edit tree cannot be applied to form")
raise ValueError(Errors.E1029)
cpdef unicode tree_to_str(self, uint32_t tree_id):
"""Return the tree as a string. The tree tree string is formatted
@ -187,7 +187,7 @@ cdef class EditTrees:
"""
if tree_id >= self.trees.size():
raise IndexError("Edit tree identifier out of range")
raise IndexError(Errors.E1030)
cdef EditTreeC tree = self.trees[tree_id]
cdef SubstNodeC subst_node

View File

@ -824,7 +824,7 @@ cdef class ArcEager(TransitionSystem):
for i in range(self.n_moves):
print(self.get_class_name(i), is_valid[i], costs[i])
print("Gold sent starts?", is_sent_start(&gold_state, state.B(0)), is_sent_start(&gold_state, state.B(1)))
raise ValueError("Could not find gold transition - see logs above.")
raise ValueError(Errors.E1031)
def get_oracle_sequence_from_state(self, StateClass state, ArcEagerGold gold, _debug=None):
cdef int i

View File

@ -9,6 +9,8 @@ cimport cython
import weakref
from preshed.maps cimport map_get_unless_missing
from murmurhash.mrmr cimport hash64
from .. import Errors
from ..typedefs cimport hash_t
from ..strings import get_string_id
from ..structs cimport EdgeC, GraphC
@ -68,7 +70,7 @@ cdef class Node:
"""
cdef int length = graph.c.nodes.size()
if i >= length or -i >= length:
raise IndexError(f"Node index {i} out of bounds ({length})")
raise IndexError(Errors.E1034.format(i=i, length=length))
if i < 0:
i += length
self.graph = graph
@ -88,7 +90,7 @@ cdef class Node:
"""Get a token index from the node's set of tokens."""
length = self.graph.c.nodes[self.i].size()
if i >= length or -i >= length:
raise IndexError(f"Token index {i} out of bounds ({length})")
raise IndexError(Errors.E1035.format(i=i, length=length))
if i < 0:
i += length
return self.graph.c.nodes[self.i][i]
@ -306,7 +308,7 @@ cdef class NoneNode(Node):
self.i = -1
def __getitem__(self, int i):
raise IndexError("Cannot index into NoneNode.")
raise IndexError(Errors.E1036)
def __len__(self):
return 0

View File

@ -730,7 +730,7 @@ cdef class Span:
def __set__(self, int start):
if start < 0:
raise IndexError("TODO")
raise IndexError(Errors.E1032.format(var="start", forbidden="< 0", value=start))
self.c.start = start
property end:
@ -739,7 +739,7 @@ cdef class Span:
def __set__(self, int end):
if end < 0:
raise IndexError("TODO")
raise IndexError(Errors.E1032.format(var="end", forbidden="< 0", value=end))
self.c.end = end
property start_char:
@ -748,7 +748,7 @@ cdef class Span:
def __set__(self, int start_char):
if start_char < 0:
raise IndexError("TODO")
raise IndexError(Errors.E1032.format(var="start_char", forbidden="< 0", value=start_char))
self.c.start_char = start_char
property end_char:
@ -757,7 +757,7 @@ cdef class Span:
def __set__(self, int end_char):
if end_char < 0:
raise IndexError("TODO")
raise IndexError(Errors.E1032.format(var="end_char", forbidden="< 0", value=end_char))
self.c.end_char = end_char
property label: