From b26ca6522306609709637566814502f117e5244d Mon Sep 17 00:00:00 2001 From: checktheroads Date: Thu, 29 Aug 2019 07:10:40 -0700 Subject: [PATCH] WIP: improve app-wide error handling --- hyperglass/exceptions.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/hyperglass/exceptions.py b/hyperglass/exceptions.py index 76169c9..98fe799 100644 --- a/hyperglass/exceptions.py +++ b/hyperglass/exceptions.py @@ -119,9 +119,23 @@ class RestError(HyperglassError): class InputInvalid(HyperglassError): """Raised when input validation fails""" - message: str = "" - status: int = code.invalid - keywords: List[str] = [] + def __init__(self, **kwargs): + self._kwargs = kwargs + self._query_type = self._kwargs.get("query_type") + if self._query_type in ("bgp_route", "ping", "traceroute"): + self.query_type: str = "IP Address" + elif self._query_type == "bgp_aspath": + self.query_type: str = "AS Path" + elif self._query_type == "bgp_community": + self.query_type: str = "Community" + self.target: str = str(kwargs.get("target"), None) + self.message = f"{self.target} is an invalid {self.query_type}." + self.status: int = code.invalid + self.keywords: List[str] = [] + super().__init__(self.message, self.status, self.keywords) + + def __str__(self): + return self.message class InputNotAllowed(HyperglassError):