Skip to content

Inconsistent exception interface #428

@saaj

Description

@saaj

stix2 has decent exception hierarchy in exceptions.py. Yet, in many places it raises Python built-in exceptions like ValueError, AttributeError and others. Mixing own and Python built-in exceptions makes no clear API/contract. Calling code needs to wrap stix2's own exceptions and built-in exceptions immediately upon a stix2 call into own exceptions to indicate, for instance, there was an invalid SDO to the rest of the code. It also allows bugs in the package to go unnoticed longer, because these built-in exception will be conflated with the package's.

It is understood that the package is in beta. But having well-defined error API would simplify calling code.

Steps to reproduce

from stix2 import v21

v21.Indicator(**{
    "type": "indicator",
    "pattern": "[file:hashes.md5 = 'd610e0ecaaa34018b098e1a5639460e7']",
    "pattern_type": "stix",
    "valid_from": "2001-01-01T00:00:00.000Z",
    "valid_until": "2000-01-01T00:00:00.000Z",
})

Expected result

An instance of stix2.exceptions.STIXError raised.

Actual result

ValueError: indicator--... 'valid_until' must be greater than 'valid_from'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions