Vincent F
Vincent F

Reputation: 7341

archunit : validating that we never forget root Throwable when rethrowing an exception

Using Archunit, I would like to enforce that whenever we catch an exception and rethrow one, the original Throwable is always included in the constructor of the new exception.

the pseudo algorithm would be something like that :

this would also catch the cases where we wrap the original exception in a custom exception that doesn't have a constructor with a Throwable.We would then be forced to add the constructor, then fix the code to use it.

I see there's a possibility to get tryCatchBlocks, but I am not sure what to do after.. there's a accessesContainedInTryBlock attribute, but what I would need to look at is inside the catch block.

private fun avoidThrowWithoutParentExceptionInCatchBlocks(): ArchCondition<JavaMethod> {
  return object : ArchCondition<JavaMethod>("avoid 'throw' without passing parent exception in catch blocks") {
      override fun check(method: JavaMethod, events: ConditionEvents) {
          method.tryCatchBlocks.forEach { 
              //then what ??
          }
      }
  }
}

is it even possible ? or should I take another direction altogether ?

Upvotes: 0

Views: 34

Answers (0)

Related Questions