TNG / ArchUnit

A Java architecture test library, to specify and assert architecture rules in plain Java
http://archunit.org
Apache License 2.0
3.18k stars 288 forks source link

Support for Re-Throw and Wrapping of exception #1190

Closed vshenbag75 closed 10 months ago

vshenbag75 commented 10 months ago

I would like to check re-throw or wrapping of exception propagate the exception to the higher level.

Example:

try {
    // Some Code
} catch (CustomException1 e) {
    throw new CustomException2("Failed during some operation");  // <-- Violation
}

Expectation

try {
    // Some Code
} catch (CustomException1 e) {
    throw new CustomException2(e);  // <-- Valid
}

try {
    // Some Code
} catch (CustomException1 e) {
    throw e;  // <-- Valid
}

Any idea how to handle using ArchUnit?

hankem commented 10 months ago

I fear that ArchUnit currently (as of version 1.2.0) neither knows about throw statements nor that a statement is within a catch block, so I don't see a way how to write the test you're looking for.

Depending on your further contraints, there could be workarounds, though. Do your custom exceptions need constructors that don't take another exception? If not, you could write a rule preventing such constructors, and the violation wasn't possible anymore.