refactor error metric code