Raise a TraitError Parameters ---------- obj : HasTraits or None The instance which owns the trait. If not object is given, then an object agnostic error will be raised. value : any The value that caused the error.
(
self,
obj: HasTraits | None,
value: t.Any,
error: Exception | None = None,
info: str | None = None,
)
| 769 | return self.info_text |
| 770 | |
| 771 | def error( |
| 772 | self, |
| 773 | obj: HasTraits | None, |
| 774 | value: t.Any, |
| 775 | error: Exception | None = None, |
| 776 | info: str | None = None, |
| 777 | ) -> t.NoReturn: |
| 778 | """Raise a TraitError |
| 779 | |
| 780 | Parameters |
| 781 | ---------- |
| 782 | obj : HasTraits or None |
| 783 | The instance which owns the trait. If not |
| 784 | object is given, then an object agnostic |
| 785 | error will be raised. |
| 786 | value : any |
| 787 | The value that caused the error. |
| 788 | error : Exception (default: None) |
| 789 | An error that was raised by a child trait. |
| 790 | The arguments of this exception should be |
| 791 | of the form ``(value, info, *traits)``. |
| 792 | Where the ``value`` and ``info`` are the |
| 793 | problem value, and string describing the |
| 794 | expected value. The ``traits`` are a series |
| 795 | of :class:`TraitType` instances that are |
| 796 | "children" of this one (the first being |
| 797 | the deepest). |
| 798 | info : str (default: None) |
| 799 | A description of the expected value. By |
| 800 | default this is inferred from this trait's |
| 801 | ``info`` method. |
| 802 | """ |
| 803 | if error is not None: |
| 804 | # handle nested error |
| 805 | error.args += (self,) |
| 806 | if self.name is not None: |
| 807 | # this is the root trait that must format the final message |
| 808 | chain = " of ".join(describe("a", t) for t in error.args[2:]) |
| 809 | if obj is not None: |
| 810 | error.args = ( |
| 811 | "The '{}' trait of {} instance contains {} which " |
| 812 | "expected {}, not {}.".format( |
| 813 | self.name, |
| 814 | describe("an", obj), |
| 815 | chain, |
| 816 | error.args[1], |
| 817 | describe("the", error.args[0]), |
| 818 | ), |
| 819 | ) |
| 820 | else: |
| 821 | error.args = ( |
| 822 | "The '{}' trait contains {} which " "expected {}, not {}.".format( |
| 823 | self.name, |
| 824 | chain, |
| 825 | error.args[1], |
| 826 | describe("the", error.args[0]), |
| 827 | ), |
| 828 | ) |