Show error name when raising UserSideException

This commit is contained in:
Ivan Kravets
2023-06-21 13:47:45 +03:00
parent 326ebcf593
commit e25b170b34
4 changed files with 27 additions and 18 deletions

View File

@ -107,7 +107,7 @@ def main(argv=None):
except Exception as exc: # pylint: disable=broad-except except Exception as exc: # pylint: disable=broad-except
if not isinstance(exc, exception.ReturnErrorCode): if not isinstance(exc, exception.ReturnErrorCode):
maintenance.on_platformio_exception(exc) maintenance.on_platformio_exception(exc)
error_str = "Error: " error_str = f"{exc.__class__.__name__}: "
if isinstance(exc, exception.PlatformioException): if isinstance(exc, exception.PlatformioException):
error_str += str(exc) error_str += str(exc)
else: else:

View File

@ -24,7 +24,7 @@ import semantic_version
from platformio import fs from platformio import fs
from platformio.compat import get_object_members, hashlib_encode_data, string_types from platformio.compat import get_object_members, hashlib_encode_data, string_types
from platformio.package.manifest.parser import ManifestFileType from platformio.package.manifest.parser import ManifestFileType
from platformio.package.version import cast_version_to_semver from platformio.package.version import SemanticVersionError, cast_version_to_semver
from platformio.util import items_in_list from platformio.util import items_in_list
@ -175,7 +175,7 @@ class PackageSpec: # pylint: disable=too-many-instance-attributes
if requirements: if requirements:
try: try:
self.requirements = requirements self.requirements = requirements
except ValueError as exc: except SemanticVersionError as exc:
if not self.name or self.uri or self.raw: if not self.name or self.uri or self.raw:
raise exc raise exc
self.raw = "%s=%s" % (self.name, requirements) self.raw = "%s=%s" % (self.name, requirements)
@ -224,11 +224,14 @@ class PackageSpec: # pylint: disable=too-many-instance-attributes
if not value: if not value:
self._requirements = None self._requirements = None
return return
self._requirements = ( try:
value self._requirements = (
if isinstance(value, semantic_version.SimpleSpec) value
else semantic_version.SimpleSpec(str(value)) if isinstance(value, semantic_version.SimpleSpec)
) else semantic_version.SimpleSpec(str(value))
)
except ValueError as exc:
raise SemanticVersionError(exc) from exc
def humanize(self): def humanize(self):
result = "" result = ""

View File

@ -16,6 +16,12 @@ import re
import semantic_version import semantic_version
from platformio.exception import UserSideException
class SemanticVersionError(UserSideException):
pass
def cast_version_to_semver(value, force=True, raise_exception=False): def cast_version_to_semver(value, force=True, raise_exception=False):
assert value assert value
@ -29,7 +35,7 @@ def cast_version_to_semver(value, force=True, raise_exception=False):
except ValueError: except ValueError:
pass pass
if raise_exception: if raise_exception:
raise ValueError("Invalid SemVer version %s" % value) raise SemanticVersionError("Invalid SemVer version %s" % value)
# parse commit hash # parse commit hash
if re.match(r"^[\da-f]+$", value, flags=re.I): if re.match(r"^[\da-f]+$", value, flags=re.I):
return semantic_version.Version("0.0.0+sha." + value) return semantic_version.Version("0.0.0+sha." + value)

View File

@ -12,14 +12,14 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from platformio.exception import PlatformioException, UserSideException from platformio.exception import UserSideException
class ProjectError(PlatformioException): class ProjectError(UserSideException):
pass pass
class NotPlatformIOProjectError(ProjectError, UserSideException): class NotPlatformIOProjectError(ProjectError):
MESSAGE = ( MESSAGE = (
"Not a PlatformIO project. `platformio.ini` file has not been " "Not a PlatformIO project. `platformio.ini` file has not been "
"found in current working directory ({0}). To initialize new project " "found in current working directory ({0}). To initialize new project "
@ -27,28 +27,28 @@ class NotPlatformIOProjectError(ProjectError, UserSideException):
) )
class InvalidProjectConfError(ProjectError, UserSideException): class InvalidProjectConfError(ProjectError):
MESSAGE = "Invalid '{0}' (project configuration file): '{1}'" MESSAGE = "Invalid '{0}' (project configuration file): '{1}'"
class UndefinedEnvPlatformError(ProjectError, UserSideException): class UndefinedEnvPlatformError(ProjectError):
MESSAGE = "Please specify platform for '{0}' environment" MESSAGE = "Please specify platform for '{0}' environment"
class ProjectEnvsNotAvailableError(ProjectError, UserSideException): class ProjectEnvsNotAvailableError(ProjectError):
MESSAGE = "Please setup environments in `platformio.ini` file" MESSAGE = "Please setup environments in `platformio.ini` file"
class UnknownEnvNamesError(ProjectError, UserSideException): class UnknownEnvNamesError(ProjectError):
MESSAGE = "Unknown environment names '{0}'. Valid names are '{1}'" MESSAGE = "Unknown environment names '{0}'. Valid names are '{1}'"
class InvalidEnvNameError(ProjectError, UserSideException): class InvalidEnvNameError(ProjectError):
MESSAGE = ( MESSAGE = (
"Invalid environment name '{0}'. The name can contain " "Invalid environment name '{0}'. The name can contain "
"alphanumeric, underscore, and hyphen characters (a-z, 0-9, -, _)" "alphanumeric, underscore, and hyphen characters (a-z, 0-9, -, _)"
) )
class ProjectOptionValueError(ProjectError, UserSideException): class ProjectOptionValueError(ProjectError):
MESSAGE = "{0} for option `{1}` in section [{2}]" MESSAGE = "{0} for option `{1}` in section [{2}]"