split errors out to lib and middlewares