lib: refactor error reporting