ENH: factorize code and enhance error message

2 jobs from extractroi_modefit in 25 minutes and 21 seconds (queued for 4 seconds)
latest merge request
Status Job ID Name Coverage
  Build
passed #1127
native-build

00:23:21

passed #1128
shark-build

00:25:18